fix minifyJS on event attributes
authoralexlamsl <alexlamsl@gmail.com>
Mon, 13 Mar 2017 19:24:42 +0000 (03:24 +0800)
committeralexlamsl <alexlamsl@gmail.com>
Mon, 13 Mar 2017 19:24:42 +0000 (03:24 +0800)
latest `uglify-js` has advanced side-effect-free optimisation which invalidates an old hack

fixes #792

src/htmlminifier.js
tests/minifier.js

index 18118aa..3fa6700 100644 (file)
@@ -615,9 +615,6 @@ function identity(value) {
   return value;
 }
 
-var fnPrefix = '!function(){';
-var fnSuffix = '}();';
-
 function processOptions(options) {
   ['html5', 'includeAutoGeneratedTags'].forEach(function(key) {
     if (!(key in options)) {
@@ -681,17 +678,13 @@ function processOptions(options) {
     }
     minifyJS.fromString = true;
     (minifyJS.output || (minifyJS.output = {})).inline_script = true;
+    (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;
       try {
-        if (inline) {
-          code = fnPrefix + code + fnSuffix;
-        }
+        minifyJS.parse.bare_returns = inline;
         code = UglifyJS.minify(code, minifyJS).code;
-        if (inline) {
-          code = code.slice(fnPrefix.length, -fnSuffix.length);
-        }
         if (/;$/.test(code)) {
           code = code.slice(0, -1);
         }
index 7b7bec6..c39e3d8 100644 (file)
@@ -1931,7 +1931,7 @@ QUnit.test('script minification', function(assert) {
   assert.equal(minify(input, { minifyJS: true }), output);
 
   input = '<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({\'gtm.start\':new Date().getTime(),event:\'gtm.js\'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!=\'dataLayer\'?\'&l=\'+l:\'\';j.async=true;j.src=\'//www.googletagmanager.com/gtm.js?id=\'+i+dl;f.parentNode.insertBefore(j,f);})(window,document,\'script\',\'dataLayer\',\'GTM-67NT\');</script>';
-  output = '<script>!function(w,d,s,l,i){w[l]=w[l]||[],w[l].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var f=d.getElementsByTagName(s)[0],j=d.createElement(s);j.async=!0,j.src="//www.googletagmanager.com/gtm.js?id=GTM-67NT",f.parentNode.insertBefore(j,f)}(window,document,"script","dataLayer","GTM-67NT")</script>';
+  output = '<script>!function(w,d,s,l,i){w[l]=w[l]||[],w[l].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var f=d.getElementsByTagName(s)[0],j=d.createElement(s);j.async=!0,j.src="//www.googletagmanager.com/gtm.js?id=GTM-67NT",f.parentNode.insertBefore(j,f)}(window,document,"script","dataLayer")</script>';
 
   assert.equal(minify(input, { minifyJS: { mangle: false } }), output);