Start to minify SVG
authorThomas Genin <tgenin@expensify.com>
Wed, 11 Nov 2015 22:24:14 +0000 (14:24 -0800)
committerThomas Genin <tgenin@expensify.com>
Wed, 11 Nov 2015 22:24:14 +0000 (14:24 -0800)
cli.js
package.json
sample-cli-config-file.conf
src/htmlminifier.js
src/htmlparser.js

diff --git a/cli.js b/cli.js
index 79b5adc..f53817e 100755 (executable)
--- a/cli.js
+++ b/cli.js
@@ -79,6 +79,7 @@ var mainOptions = {
   caseSensitive: [[false, 'Treat attributes in case sensitive manner (useful for SVG; e.g. viewBox)']],
   minifyJS: [[false, 'Minify Javascript in script elements and on* attributes (uses UglifyJS)']],
   minifyCSS: [[false, 'Minify CSS in style elements and style attributes (uses clean-css)']],
+  minifySVG:[[false, 'Minify SVG (uses SVGO)']],
   minifyURLs: [[false, 'Minify URLs in various attributes (uses relateurl)']],
   ignoreCustomComments: [[false, 'Array of regex\'es that allow to ignore certain comments, when matched', 'string'], 'json-regex'],
   processScripts: [[false, 'Array of strings corresponding to types of script elements to process through minifier (e.g. "text/ng-template", "text/x-handlebars-template", etc.)', 'string'], 'json-regex'],
index 3967b20..1a0015f 100644 (file)
@@ -41,6 +41,7 @@
     "cli": "0.11.x",
     "concat-stream": "1.5.x",
     "relateurl": "0.2.x",
+    "svgo": "^0.6.0",
     "uglify-js": "2.5.x"
   },
   "devDependencies": {
index 75ffdf1..96ef2af 100644 (file)
   "caseSensitive": false,
   "minifyJS": false,
   "minifyCSS": false,
+  "minifySVG": {
+    "lol": 4,
+    "cleanupAttrs": true
+  },
   "ignoreCustomComments": [],
   "processScripts": []
 }
index b291e25..a9c330a 100644 (file)
@@ -47,7 +47,7 @@
       'a', 'abbr', 'acronym', 'b', 'bdi', 'bdo', 'big', 'button', 'cite',
       'code', 'del', 'dfn', 'em', 'font', 'i', 'ins', 'kbd', 'mark', 'q',
       'rt', 'rp', 's', 'samp', 'small', 'span', 'strike', 'strong',
-      'sub', 'sup', 'svg', 'time', 'tt', 'u', 'var'
+      'sub', 'sup', 'time', 'tt', 'u', 'var'
     ],
     lineBreakBefore = /^[\t ]*[\n\r]+[\t\n\r ]*/,
     lineBreakAfter = /[\t\n\r ]*[\n\r]+[\t ]*$/,
     return text;
   }
 
+  function minifySVG(text, options) {
+    if (!text.trim().length) {
+      return text;
+    }
+
+    if (typeof options !== 'object') {
+      options = { };
+    }
+
+    try {
+      if (typeof require === 'function'){
+        var SVGO = require( 'svgo' );
+
+        if (text){
+          var svgo1 = new SVGO(options);
+          svgo1.optimize(text, function (rr) {
+            text = rr.data;
+          });
+        }
+      }
+    }
+    catch (err) {
+      log(err);
+    }
+
+    return text;
+  }
+
   function minify(value, options) {
 
     options = options || {};
         if (currentTag === 'style' && options.minifyCSS) {
           text = minifyCSS(text, options.minifyCSS);
         }
+        if (currentTag === 'svg' && options.minifySVG) {
+          text = minifySVG(text, options.minifySVG);
+        }
         if (options.collapseWhitespace) {
           if (!stackNoTrimWhitespace.length) {
             text = ((prevTag && prevTag !== 'comment') || (nextTag && nextTag !== 'comment')) ?
index 3fc5988..3f42e1e 100644 (file)
@@ -58,7 +58,7 @@
   // var block = makeMap('address,applet,blockquote,button,center,dd,del,dir,div,dl,dt,fieldset,form,frameset,hr,iframe,ins,isindex,li,map,menu,noframes,noscript,object,ol,p,pre,script,table,tbody,td,tfoot,th,thead,tr,ul');
 
   // Inline Elements - HTML 4.01
-  var inline = makeMap('a,abbr,acronym,applet,b,basefont,bdo,big,br,button,cite,code,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,noscript,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,svg,textarea,tt,u,var');
+  var inline = makeMap('a,abbr,acronym,applet,b,basefont,bdo,big,br,button,cite,code,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,noscript,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var');
 
   // Elements that you can, intentionally, leave open
   // (and which close themselves)
@@ -68,7 +68,7 @@
   var fillAttrs = makeMap('checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected');
 
   // Special Elements (can contain anything)
-  var special = makeMap('script,style');
+  var special = makeMap('script,style,svg');
 
   var reCache = {}, stackedTag, reStackedTag, tagMatch;