diff options
Diffstat (limited to 'node_modules/html-minifier/src/htmlminifier.js')
-rw-r--r-- | node_modules/html-minifier/src/htmlminifier.js | 425 |
1 files changed, 225 insertions, 200 deletions
diff --git a/node_modules/html-minifier/src/htmlminifier.js b/node_modules/html-minifier/src/htmlminifier.js index 6910e15dd..ecf06eba1 100644 --- a/node_modules/html-minifier/src/htmlminifier.js +++ b/node_modules/html-minifier/src/htmlminifier.js @@ -8,20 +8,13 @@ var TokenChain = require('./tokenchain'); var UglifyJS = require('uglify-js'); var utils = require('./utils'); -var trimWhitespace = String.prototype.trim ? function(str) { - if (typeof str !== 'string') { - return str; - } - return str.trim(); -} : function(str) { - if (typeof str !== 'string') { - return str; - } - return str.replace(/^\s+/, '').replace(/\s+$/, ''); -}; +function trimWhitespace(str) { + return str && str.replace(/^[ \n\r\t\f]+/, '').replace(/[ \n\r\t\f]+$/, ''); +} function collapseWhitespaceAll(str) { - return str && str.replace(/\s+/g, function(spaces) { + // Non-breaking space is specifically handled inside the replacer function here: + return str && str.replace(/[ \n\r\t\f\xA0]+/g, function(spaces) { return spaces === '\t' ? '\t' : spaces.replace(/(^|\xA0+)[^\xA0]+/g, '$1 '); }); } @@ -30,17 +23,18 @@ function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll) { var lineBreakBefore = '', lineBreakAfter = ''; if (options.preserveLineBreaks) { - str = str.replace(/^\s*?[\n\r]\s*/, function() { + str = str.replace(/^[ \n\r\t\f]*?[\n\r][ \n\r\t\f]*/, function() { lineBreakBefore = '\n'; return ''; - }).replace(/\s*?[\n\r]\s*$/, function() { + }).replace(/[ \n\r\t\f]*?[\n\r][ \n\r\t\f]*$/, function() { lineBreakAfter = '\n'; return ''; }); } if (trimLeft) { - str = str.replace(/^\s+/, function(spaces) { + // Non-breaking space is specifically handled inside the replacer function here: + str = str.replace(/^[ \n\r\t\f\xA0]+/, function(spaces) { var conservative = !lineBreakBefore && options.conservativeCollapse; if (conservative && spaces === '\t') { return '\t'; @@ -50,7 +44,8 @@ function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll) { } if (trimRight) { - str = str.replace(/\s+$/, function(spaces) { + // Non-breaking space is specifically handled inside the replacer function here: + str = str.replace(/[ \n\r\t\f\xA0]+$/, function(spaces) { var conservative = !lineBreakAfter && options.conservativeCollapse; if (conservative && spaces === '\t') { return '\t'; @@ -69,9 +64,9 @@ function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll) { var createMapFromString = utils.createMapFromString; // non-empty tags that will maintain whitespace around them -var inlineTags = createMapFromString('a,abbr,acronym,b,bdi,bdo,big,button,cite,code,del,dfn,em,font,i,ins,kbd,mark,math,nobr,q,rt,rp,s,samp,small,span,strike,strong,sub,sup,svg,time,tt,u,var'); +var inlineTags = createMapFromString('a,abbr,acronym,b,bdi,bdo,big,button,cite,code,del,dfn,em,font,i,ins,kbd,label,mark,math,nobr,object,q,rp,rt,rtc,ruby,s,samp,select,small,span,strike,strong,sub,sup,svg,textarea,time,tt,u,var'); // non-empty tags that will maintain whitespace within them -var inlineTextTags = createMapFromString('a,abbr,acronym,b,big,del,em,font,i,ins,kbd,mark,nobr,s,samp,small,span,strike,strong,sub,sup,time,tt,u,var'); +var inlineTextTags = createMapFromString('a,abbr,acronym,b,big,del,em,font,i,ins,kbd,mark,nobr,rp,s,samp,small,span,strike,strong,sub,sup,time,tt,u,var'); // self-closing tags that will maintain whitespace around them var selfClosingInlineTags = createMapFromString('comment,img,input,wbr'); @@ -114,7 +109,7 @@ function isEventAttribute(attrName, options) { } function canRemoveAttributeQuotes(value) { - // http://mathiasbynens.be/notes/unquoted-attribute-values + // https://mathiasbynens.be/notes/unquoted-attribute-values return /^[^ \t\n\f\r"'`=<>]+$/.test(value); } @@ -260,7 +255,7 @@ function isSrcset(attrName, tag) { } function cleanAttributeValue(tag, attrName, attrValue, options, attrs) { - if (attrValue && isEventAttribute(attrName, options)) { + if (isEventAttribute(attrName, options)) { attrValue = trimWhitespace(attrValue).replace(/^javascript:\s*/i, ''); return options.minifyJS(attrValue, true); } @@ -285,9 +280,9 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs) { attrValue = trimWhitespace(attrValue); if (attrValue) { if (/;$/.test(attrValue) && !/&#?[0-9a-zA-Z]+;$/.test(attrValue)) { - attrValue = attrValue.replace(/\s*;$/, ''); + attrValue = attrValue.replace(/\s*;$/, ';'); } - attrValue = unwrapInlineCSS(options.minifyCSS(wrapInlineCSS(attrValue))); + attrValue = options.minifyCSS(attrValue, 'inline'); } return attrValue; } @@ -316,7 +311,7 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs) { return (+numString).toString(); }); } - else if (attrValue && options.customAttrCollapse && options.customAttrCollapse.test(attrName)) { + else if (options.customAttrCollapse && options.customAttrCollapse.test(attrName)) { attrValue = attrValue.replace(/\n+|\r+|\s{2,}/g, ''); } else if (tag === 'script' && attrName === 'type') { @@ -324,7 +319,7 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs) { } else if (isMediaQuery(tag, attrs, attrName)) { attrValue = trimWhitespace(attrValue); - return unwrapMediaQuery(options.minifyCSS(wrapMediaQuery(attrValue))); + return options.minifyCSS(attrValue, 'media'); } return attrValue; } @@ -340,23 +335,33 @@ function isMetaViewport(tag, attrs) { } } -// Wrap CSS declarations for CleanCSS > 3.x -// See https://github.com/jakubpawlowicz/clean-css/issues/418 -function wrapInlineCSS(text) { - return '*{' + text + '}'; -} - -function unwrapInlineCSS(text) { - var matches = text.match(/^\*\{([\s\S]*)\}$/); - return matches ? matches[1] : text; +function ignoreCSS(id) { + return '/* clean-css ignore:start */' + id + '/* clean-css ignore:end */'; } -function wrapMediaQuery(text) { - return '@media ' + text + '{a{top:0}}'; +// Wrap CSS declarations for CleanCSS > 3.x +// See https://github.com/jakubpawlowicz/clean-css/issues/418 +function wrapCSS(text, type) { + switch (type) { + case 'inline': + return '*{' + text + '}'; + case 'media': + return '@media ' + text + '{a{top:0}}'; + default: + return text; + } } -function unwrapMediaQuery(text) { - var matches = text.match(/^@media ([\s\S]*?)\s*{[\s\S]*}$/); +function unwrapCSS(text, type) { + var matches; + switch (type) { + case 'inline': + matches = text.match(/^\*\{([\s\S]*)\}$/); + break; + case 'media': + matches = text.match(/^@media ([\s\S]*?)\s*{[\s\S]*}$/); + break; + } return matches ? matches[1] : text; } @@ -379,7 +384,7 @@ function processScript(text, options, currentAttrs) { // Tag omission rules from https://html.spec.whatwg.org/multipage/syntax.html#optional-tags // with the following deviations: // - retain <body> if followed by <noscript> -// - </rb>, </rt>, </rtc>, </rp> & </tfoot> follow http://www.w3.org/TR/html5/syntax.html#optional-tags +// - </rb>, </rt>, </rtc>, </rp> & </tfoot> follow https://www.w3.org/TR/html5/syntax.html#optional-tags // - retain all tags which are adjacent to non-standard HTML tags var optionalStartTags = createMapFromString('html,head,body,colgroup,tbody'); var optionalEndTags = createMapFromString('html,head,body,li,dt,dd,p,rb,rt,rtc,rp,optgroup,option,colgroup,caption,thead,tbody,tfoot,tr,td,th'); @@ -397,7 +402,7 @@ var topLevelTags = createMapFromString('html,head,body'); var compactTags = createMapFromString('html,body'); var looseTags = createMapFromString('head,colgroup,caption'); var trailingTags = createMapFromString('dt,thead'); -var htmlTags = createMapFromString('a,abbr,acronym,address,applet,area,article,aside,audio,b,base,basefont,bdi,bdo,bgsound,big,blink,blockquote,body,br,button,canvas,caption,center,cite,code,col,colgroup,command,content,data,datalist,dd,del,details,dfn,dialog,dir,div,dl,dt,element,em,embed,fieldset,figcaption,figure,font,footer,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,image,img,input,ins,isindex,kbd,keygen,label,legend,li,link,listing,main,map,mark,marquee,menu,menuitem,meta,meter,multicol,nav,nobr,noembed,noframes,noscript,object,ol,optgroup,option,output,p,param,picture,plaintext,pre,progress,q,rp,rt,rtc,ruby,s,samp,script,section,select,shadow,small,source,spacer,span,strike,strong,style,sub,summary,sup,table,tbody,td,template,textarea,tfoot,th,thead,time,title,tr,track,tt,u,ul,var,video,wbr,xmp'); +var htmlTags = createMapFromString('a,abbr,acronym,address,applet,area,article,aside,audio,b,base,basefont,bdi,bdo,bgsound,big,blink,blockquote,body,br,button,canvas,caption,center,cite,code,col,colgroup,command,content,data,datalist,dd,del,details,dfn,dialog,dir,div,dl,dt,element,em,embed,fieldset,figcaption,figure,font,footer,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,image,img,input,ins,isindex,kbd,keygen,label,legend,li,link,listing,main,map,mark,marquee,menu,menuitem,meta,meter,multicol,nav,nobr,noembed,noframes,noscript,object,ol,optgroup,option,output,p,param,picture,plaintext,pre,progress,q,rb,rp,rt,rtc,ruby,s,samp,script,section,select,shadow,small,source,spacer,span,strike,strong,style,sub,summary,sup,table,tbody,td,template,textarea,tfoot,th,thead,time,title,tr,track,tt,u,ul,var,video,wbr,xmp'); function canRemoveParentTag(optionalStartTag, tag) { switch (optionalStartTag) { @@ -524,7 +529,7 @@ function canTrimWhitespace(tag) { } function normalizeAttr(attr, attrs, tag, options) { - var attrName = options.caseSensitive ? attr.name : attr.name.toLowerCase(), + var attrName = options.name(attr.name), attrValue = attr.value; if (options.decodeEntities && attrValue) { @@ -540,7 +545,9 @@ function normalizeAttr(attr, attrs, tag, options) { return; } - attrValue = cleanAttributeValue(tag, attrName, attrValue, options, attrs); + if (attrValue) { + attrValue = cleanAttributeValue(tag, attrName, attrValue, options, attrs); + } if (options.removeEmptyAttributes && canDeleteEmptyAttribute(tag, attrName, attrValue, options)) { @@ -615,100 +622,102 @@ function identity(value) { return value; } -function processOptions(options) { - ['html5', 'includeAutoGeneratedTags'].forEach(function(key) { - if (!(key in options)) { - options[key] = true; - } - }); - - if (typeof options.log !== 'function') { - options.log = identity; - } - - if (!options.canCollapseWhitespace) { - options.canCollapseWhitespace = canCollapseWhitespace; - } - if (!options.canTrimWhitespace) { - options.canTrimWhitespace = canTrimWhitespace; - } - - if (!('ignoreCustomComments' in options)) { - options.ignoreCustomComments = [/^!/]; - } - - if (!('ignoreCustomFragments' in options)) { - options.ignoreCustomFragments = [ +function processOptions(values) { + var options = { + name: function(name) { + return name.toLowerCase(); + }, + canCollapseWhitespace: canCollapseWhitespace, + canTrimWhitespace: canTrimWhitespace, + html5: true, + ignoreCustomComments: [/^!/], + ignoreCustomFragments: [ /<%[\s\S]*?%>/, /<\?[\s\S]*?\?>/ - ]; - } - - if (!options.minifyURLs) { - options.minifyURLs = identity; - } - if (typeof options.minifyURLs !== 'function') { - var minifyURLs = options.minifyURLs; - if (typeof minifyURLs === 'string') { - minifyURLs = { site: minifyURLs }; + ], + includeAutoGeneratedTags: true, + log: identity, + minifyCSS: identity, + minifyJS: identity, + minifyURLs: identity + }; + Object.keys(values).forEach(function(key) { + var value = values[key]; + if (key === 'caseSensitive') { + if (value) { + options.name = identity; + } } - else if (typeof minifyURLs !== 'object') { - minifyURLs = {}; + else if (key === 'log') { + if (typeof value === 'function') { + options.log = value; + } } - options.minifyURLs = function(text) { - try { - return RelateUrl.relate(text, minifyURLs); + else if (key === 'minifyCSS' && typeof value !== 'function') { + if (!value) { + return; } - catch (err) { - options.log(err); - return text; + if (typeof value !== 'object') { + value = {}; } - }; - } - - if (!options.minifyJS) { - options.minifyJS = identity; - } - if (typeof options.minifyJS !== 'function') { - var minifyJS = options.minifyJS; - if (typeof minifyJS !== 'object') { - minifyJS = {}; + options.minifyCSS = function(text, type) { + text = text.replace(/(url\s*\(\s*)("|'|)(.*?)\2(\s*\))/ig, function(match, prefix, quote, url, suffix) { + return prefix + quote + options.minifyURLs(url) + quote + suffix; + }); + try { + return unwrapCSS(new CleanCSS(value).minify(wrapCSS(text, type)).styles, type); + } + catch (err) { + options.log(err); + return text; + } + }; } - (minifyJS.parse || (minifyJS.parse = {})).bare_returns = false; - options.minifyJS = function(text, inline) { - var start = text.match(/^\s*<!--.*/); - var code = start ? text.slice(start[0].length).replace(/\n\s*-->\s*$/, '') : text; - minifyJS.parse.bare_returns = inline; - var result = UglifyJS.minify(code, minifyJS); - if (result.error) { - options.log(result.error); - return text; + else if (key === 'minifyJS' && typeof value !== 'function') { + if (!value) { + return; } - return result.code.replace(/;$/, ''); - }; - } - - if (!options.minifyCSS) { - options.minifyCSS = identity; - } - if (typeof options.minifyCSS !== 'function') { - var minifyCSS = options.minifyCSS; - if (typeof minifyCSS !== 'object') { - minifyCSS = {}; + if (typeof value !== 'object') { + value = {}; + } + (value.parse || (value.parse = {})).bare_returns = false; + options.minifyJS = function(text, inline) { + var start = text.match(/^\s*<!--.*/); + var code = start ? text.slice(start[0].length).replace(/\n\s*-->\s*$/, '') : text; + value.parse.bare_returns = inline; + var result = UglifyJS.minify(code, value); + if (result.error) { + options.log(result.error); + return text; + } + return result.code.replace(/;$/, ''); + }; } - options.minifyCSS = function(text) { - text = text.replace(/(url\s*\(\s*)("|'|)(.*?)\2(\s*\))/ig, function(match, prefix, quote, url, suffix) { - return prefix + quote + options.minifyURLs(url) + quote + suffix; - }); - try { - return new CleanCSS(minifyCSS).minify(text).styles; + else if (key === 'minifyURLs' && typeof value !== 'function') { + if (!value) { + return; } - catch (err) { - options.log(err); - return text; + if (typeof value === 'string') { + value = { site: value }; } - }; - } + else if (typeof value !== 'object') { + value = {}; + } + options.minifyURLs = function(text) { + try { + return RelateUrl.relate(text, value); + } + catch (err) { + options.log(err); + return text; + } + }; + } + else { + options[key] = value; + } + }); + return options; } function uniqueId(value) { @@ -727,7 +736,7 @@ function createSortFns(value, options, uidIgnore, uidAttr) { function attrNames(attrs) { return attrs.map(function(attr) { - return options.caseSensitive ? attr.name : attr.name.toLowerCase(); + return options.name(attr.name); }); } @@ -751,7 +760,7 @@ function createSortFns(value, options, uidIgnore, uidAttr) { } for (var i = 0, len = attrs.length; i < len; i++) { var attr = attrs[i]; - if (classChain && (options.caseSensitive ? attr.name : attr.name.toLowerCase()) === 'class') { + if (classChain && attr.value && options.name(attr.name) === 'class') { classChain.add(trimWhitespace(attr.value).split(/[ \t\n\f\r]+/).filter(shouldSkipUIDs)); } else if (options.processScripts && attr.name.toLowerCase() === 'type') { @@ -773,7 +782,7 @@ function createSortFns(value, options, uidIgnore, uidAttr) { } var log = options.log; - options.log = null; + options.log = identity; options.sortAttributes = false; options.sortClassName = false; scan(minify(value, options)); @@ -806,9 +815,6 @@ function createSortFns(value, options, uidIgnore, uidAttr) { } function minify(value, options, partialMarkup) { - options = options || {}; - var optionsStack = []; - processOptions(options); if (options.collapseWhitespace) { value = collapseWhitespace(value, options, true, true); } @@ -823,7 +829,6 @@ function minify(value, options, partialMarkup) { stackNoCollapseWhitespace = [], optionalStartTag = '', optionalEndTag = '', - t = Date.now(), ignoredMarkupChunks = [], ignoredCustomMarkupChunks = [], uidIgnore, @@ -839,24 +844,18 @@ function minify(value, options, partialMarkup) { uidIgnore = uniqueId(value); var pattern = new RegExp('^' + uidIgnore + '([0-9]+)$'); if (options.ignoreCustomComments) { - options.ignoreCustomComments.push(pattern); + options.ignoreCustomComments = options.ignoreCustomComments.slice(); } else { - options.ignoreCustomComments = [pattern]; + options.ignoreCustomComments = []; } + options.ignoreCustomComments.push(pattern); } var token = '<!--' + uidIgnore + ignoredMarkupChunks.length + '-->'; ignoredMarkupChunks.push(group1); return token; }); - function escapeFragments(text) { - return text.replace(uidPattern, function(match, prefix, index) { - var chunks = ignoredCustomMarkupChunks[+index]; - return chunks[1] + uidAttr + index + chunks[2]; - }); - } - var customFragments = options.ignoreCustomFragments.map(function(re) { return re.source; }); @@ -867,17 +866,39 @@ function minify(value, options, partialMarkup) { if (!uidAttr) { uidAttr = uniqueId(value); uidPattern = new RegExp('(\\s*)' + uidAttr + '([0-9]+)(\\s*)', 'g'); - var minifyCSS = options.minifyCSS; - if (minifyCSS) { - options.minifyCSS = function(text) { - return minifyCSS(escapeFragments(text)); - }; + if (options.minifyCSS) { + options.minifyCSS = (function(fn) { + return function(text, type) { + text = text.replace(uidPattern, function(match, prefix, index) { + var chunks = ignoredCustomMarkupChunks[+index]; + return chunks[1] + uidAttr + index + chunks[2]; + }); + var ids = []; + new CleanCSS().minify(wrapCSS(text, type)).warnings.forEach(function(warning) { + var match = uidPattern.exec(warning); + if (match) { + var id = uidAttr + match[2]; + text = text.replace(id, ignoreCSS(id)); + ids.push(id); + } + }); + text = fn(text, type); + ids.forEach(function(id) { + text = text.replace(ignoreCSS(id), id); + }); + return text; + }; + })(options.minifyCSS); } - var minifyJS = options.minifyJS; - if (minifyJS) { - options.minifyJS = function(text, inline) { - return minifyJS(escapeFragments(text), inline); - }; + if (options.minifyJS) { + options.minifyJS = (function(fn) { + return function(text, type) { + return fn(text.replace(uidPattern, function(match, prefix, index) { + var chunks = ignoredCustomMarkupChunks[+index]; + return chunks[1] + uidAttr + index + chunks[2]; + }), type); + }; + })(options.minifyJS); } } var token = uidAttr + ignoredCustomMarkupChunks.length; @@ -948,21 +969,13 @@ function minify(value, options, partialMarkup) { html5: options.html5, start: function(tag, attrs, unary, unarySlash, autoGenerated) { - var lowerTag = tag.toLowerCase(); - - if (lowerTag === 'svg') { - optionsStack.push(options); - var nextOptions = {}; - for (var key in options) { - nextOptions[key] = options[key]; - } - nextOptions.keepClosingSlash = true; - nextOptions.caseSensitive = true; - options = nextOptions; + if (tag.toLowerCase() === 'svg') { + options = Object.create(options); + options.caseSensitive = true; + options.keepClosingSlash = true; + options.name = identity; } - - tag = options.caseSensitive ? tag : lowerTag; - + tag = options.name(tag); currentTag = tag; charsPrevTag = tag; if (!inlineTextTags(tag)) { @@ -998,11 +1011,13 @@ function minify(value, options, partialMarkup) { if (!stackNoTrimWhitespace.length) { squashTrailingWhitespace(tag); } - if (!_canTrimWhitespace(tag, attrs) || stackNoTrimWhitespace.length) { - stackNoTrimWhitespace.push(tag); - } - if (!_canCollapseWhitespace(tag, attrs) || stackNoCollapseWhitespace.length) { - stackNoCollapseWhitespace.push(tag); + if (!unary) { + if (!_canTrimWhitespace(tag, attrs) || stackNoTrimWhitespace.length) { + stackNoTrimWhitespace.push(tag); + } + if (!_canCollapseWhitespace(tag, attrs) || stackNoCollapseWhitespace.length) { + stackNoCollapseWhitespace.push(tag); + } } } @@ -1040,11 +1055,10 @@ function minify(value, options, partialMarkup) { } }, end: function(tag, attrs, autoGenerated) { - var lowerTag = tag.toLowerCase(); - if (lowerTag === 'svg') { - options = optionsStack.pop(); + if (tag.toLowerCase() === 'svg') { + options = Object.getPrototypeOf(options); } - tag = options.caseSensitive ? tag : lowerTag; + tag = options.name(tag); // check if current tag is in a whitespace stack if (options.collapseWhitespace) { @@ -1186,6 +1200,11 @@ function minify(value, options, partialMarkup) { // https://mathiasbynens.be/notes/ambiguous-ampersands text = text.replace(/&(#?[0-9a-zA-Z]+;)/g, '&$1').replace(/</g, '<'); } + if (uidPattern && options.collapseWhitespace && stackNoTrimWhitespace.length) { + text = text.replace(uidPattern, function(match, prefix, index) { + return ignoredCustomMarkupChunks[+index][0]; + }); + } currentChars += text; if (text) { hasChars = true; @@ -1217,7 +1236,7 @@ function minify(value, options, partialMarkup) { buffer.push(text); }, doctype: function(doctype) { - buffer.push(options.useShortDoctype ? '<!DOCTYPE html>' : collapseWhitespaceAll(doctype)); + buffer.push(options.useShortDoctype ? '<!doctype html>' : collapseWhitespaceAll(doctype)); }, customAttrAssign: options.customAttrAssign, customAttrSurround: options.customAttrSurround @@ -1238,10 +1257,8 @@ function minify(value, options, partialMarkup) { squashTrailingWhitespace('br'); } - var str = joinResultSegments(buffer, options); - - if (uidPattern) { - str = str.replace(uidPattern, function(match, prefix, index, suffix) { + return joinResultSegments(buffer, options, uidPattern ? function(str) { + return str.replace(uidPattern, function(match, prefix, index, suffix) { var chunk = ignoredCustomMarkupChunks[+index][0]; if (options.collapseWhitespace) { if (prefix !== '\t') { @@ -1253,48 +1270,56 @@ function minify(value, options, partialMarkup) { return collapseWhitespace(chunk, { preserveLineBreaks: options.preserveLineBreaks, conservativeCollapse: !options.trimCustomFragments - }, /^\s/.test(chunk), /\s$/.test(chunk)); + }, /^[ \n\r\t\f]/.test(chunk), /[ \n\r\t\f]$/.test(chunk)); } return chunk; }); - } - if (uidIgnore) { - str = str.replace(new RegExp('<!--' + uidIgnore + '([0-9]+)-->', 'g'), function(match, index) { + } : identity, uidIgnore ? function(str) { + return str.replace(new RegExp('<!--' + uidIgnore + '([0-9]+)-->', 'g'), function(match, index) { return ignoredMarkupChunks[+index]; }); - } - - options.log('minified in: ' + (Date.now() - t) + 'ms'); - return str; + } : identity); } -function joinResultSegments(results, options) { +function joinResultSegments(results, options, restoreCustom, restoreIgnore) { var str; var maxLineLength = options.maxLineLength; if (maxLineLength) { - var token; - var lines = []; - var line = ''; - for (var i = 0, len = results.length; i < len; i++) { - token = results[i]; - if (line.length + token.length < maxLineLength) { - line += token; + var line = '', lines = []; + while (results.length) { + var len = line.length; + var end = results[0].indexOf('\n'); + if (end < 0) { + line += restoreIgnore(restoreCustom(results.shift())); } else { - lines.push(line.replace(/^\n/, '')); - line = token; + line += restoreIgnore(restoreCustom(results[0].slice(0, end))); + results[0] = results[0].slice(end + 1); + } + if (len > 0 && line.length > maxLineLength) { + lines.push(line.slice(0, len)); + line = line.slice(len); + } + else if (end >= 0) { + lines.push(line); + line = ''; } } - lines.push(line); - + if (line) { + lines.push(line); + } str = lines.join('\n'); } else { - str = results.join(''); + str = restoreIgnore(restoreCustom(results.join(''))); } return options.collapseWhitespace ? collapseWhitespace(str, options, true, true) : str; } exports.minify = function(value, options) { - return minify(value, options); + var start = Date.now(); + options = processOptions(options || {}); + var result = minify(value, options); + options.log('minified in: ' + (Date.now() - start) + 'ms'); + return result; }; |