remove inline parameter from minifyCSS
authoralexlamsl <alexlamsl>
Sun, 26 Jun 2016 10:24:04 +0000 (18:24 +0800)
committeralexlamsl <alexlamsl>
Sun, 26 Jun 2016 10:24:04 +0000 (18:24 +0800)
README.md
src/htmlminifier.js
tests/minifier.js

index fc87b1f..3bc874e 100644 (file)
--- 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` |
index 0dd93be..6793187 100644 (file)
@@ -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*<!--/);
       var style = start ? text.slice(start[0].length).replace(/-->\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');
             });
           };
index 9a87f21..d335d84 100644 (file)
@@ -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 = '<style>\n.foo { font: 12pt "bar" } </style>';
   assert.equal(minify(input), input);
   assert.equal(minify(input, { minifyCSS: null }), input);
   assert.equal(minify(input, { minifyCSS: false }), input);
-  output = '<style>Normal CSS</style>';
+  output = '<style>Some CSS</style>';
   assert.equal(minify(input, { minifyCSS: css }), output);
 
   input = '<p style="font: 12pt \'bar\'"></p>';
   assert.equal(minify(input), input);
   assert.equal(minify(input, { minifyCSS: null }), input);
   assert.equal(minify(input, { minifyCSS: false }), input);
-  output = '<p style="Inline CSS"></p>';
+  output = '<p style="Some CSS"></p>';
   assert.equal(minify(input, { minifyCSS: css }), output);
 
   function js(text, inline) {