From ff5d583e43f205335949c7e44729602be4f44760 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sun, 26 Jun 2016 18:24:04 +0800 Subject: [PATCH] remove inline parameter from minifyCSS --- README.md | 2 +- src/htmlminifier.js | 32 ++++++++++++++------------------ tests/minifier.js | 8 ++++---- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index fc87b1f..3bc874e 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ How does HTMLMinifier compare to other solutions — [HTML Minifier from Will Pe | `includeAutoGeneratedTags` | Insert tags generated by HTML parser | `true` | | `keepClosingSlash` | Keep the trailing slash on singleton elements | `false` | | `maxLineLength` | Specify a maximum line length. Compressed output will be split by newlines at valid HTML split-points | -| `minifyCSS` | Minify CSS in style elements and style attributes (uses [clean-css](https://github.com/jakubpawlowicz/clean-css)) | `false` (could be `true`, `Object`, `Function(text, inline)`) | +| `minifyCSS` | Minify CSS in style elements and style attributes (uses [clean-css](https://github.com/jakubpawlowicz/clean-css)) | `false` (could be `true`, `Object`, `Function(text)`) | | `minifyJS` | Minify JavaScript in script elements and event attributes (uses [UglifyJS](https://github.com/mishoo/UglifyJS2)) | `false` (could be `true`, `Object`, `Function(text, inline)`) | | `minifyURLs` | Minify URLs in various attributes (uses [relateurl](https://github.com/stevenvachon/relateurl)) | `false` (could be `String`, `Object`, `Function(text)`) | | `preserveLineBreaks` | Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break. Must be used in conjunction with `collapseWhitespace=true` | `false` | diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 0dd93be..6793187 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -269,10 +269,13 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs) { } else if (attrName === 'style') { attrValue = trimWhitespace(attrValue); - if (attrValue && /;$/.test(attrValue) && !/&#?[0-9a-zA-Z]+;$/.test(attrValue)) { - attrValue = attrValue.replace(/\s*;$/, ''); + if (attrValue) { + if (/;$/.test(attrValue) && !/&#?[0-9a-zA-Z]+;$/.test(attrValue)) { + attrValue = attrValue.replace(/\s*;$/, ''); + } + attrValue = unwrapInlineCSS(options.minifyCSS(wrapInlineCSS(attrValue))); } - return options.minifyCSS(attrValue, true); + return attrValue; } else if (isSrcset(attrName, tag)) { // https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset @@ -321,16 +324,13 @@ function isMetaViewport(tag, attrs) { // Wrap CSS declarations for CleanCSS > 3.x // See https://github.com/jakubpawlowicz/clean-css/issues/418 -function wrapCSS(text) { +function wrapInlineCSS(text) { return '*{' + text + '}'; } -function unwrapCSS(text) { - var matches = text.match(/^\*\{([\s\S]*)\}$/m); - if (matches && matches[1]) { - return matches[1]; - } - return text; +function unwrapInlineCSS(text) { + var matches = text.match(/^\*\{([\s\S]*)\}$/); + return matches ? matches[1] : text; } function cleanConditionalComment(comment, options) { @@ -685,18 +685,14 @@ function processOptions(options) { if (typeof minifyCSS.advanced === 'undefined') { minifyCSS.advanced = false; } - options.minifyCSS = function(text, inline) { + 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; }); var start = text.match(/^\s*\s*$/, '') : text; try { - var cleanCSS = new CleanCSS(minifyCSS); - if (inline) { - return unwrapCSS(cleanCSS.minify(wrapCSS(style)).styles); - } - return cleanCSS.minify(style).styles; + return new CleanCSS(minifyCSS).minify(style).styles; } catch (err) { options.log(err); @@ -850,8 +846,8 @@ function minify(value, options, partialMarkup) { uidPattern = new RegExp('(\\s*)' + uidAttr + '([0-9]+)(\\s*)', 'g'); var minifyCSS = options.minifyCSS; if (minifyCSS) { - options.minifyCSS = function(text, inline) { - return minifyCSS(text, inline).replace(uidPattern, function(match, prefix, index, suffix) { + options.minifyCSS = function(text) { + return minifyCSS(text).replace(uidPattern, function(match, prefix, index, suffix) { return (prefix && '\t') + uidAttr + index + (suffix && '\t'); }); }; diff --git a/tests/minifier.js b/tests/minifier.js index 9a87f21..d335d84 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -695,22 +695,22 @@ QUnit.test('remove CDATA sections from scripts/styles', function(assert) { QUnit.test('custom processors', function(assert) { var input, output; - function css(text, inline) { - return inline ? 'Inline CSS' : 'Normal CSS'; + function css() { + return 'Some CSS'; } input = ''; assert.equal(minify(input), input); assert.equal(minify(input, { minifyCSS: null }), input); assert.equal(minify(input, { minifyCSS: false }), input); - output = ''; + output = ''; assert.equal(minify(input, { minifyCSS: css }), output); input = '

'; assert.equal(minify(input), input); assert.equal(minify(input, { minifyCSS: null }), input); assert.equal(minify(input, { minifyCSS: false }), input); - output = '

'; + output = '

'; assert.equal(minify(input, { minifyCSS: css }), output); function js(text, inline) { -- 2.34.1