From: Thomas Genin Date: Wed, 11 Nov 2015 22:24:14 +0000 (-0800) Subject: Start to minify SVG X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=83d35aa22dcb536ee8e2da9b2a1b44498b2ccd9b;p=html-minifier.git Start to minify SVG --- diff --git a/cli.js b/cli.js index 79b5adc..f53817e 100755 --- 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'], diff --git a/package.json b/package.json index 3967b20..1a0015f 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/sample-cli-config-file.conf b/sample-cli-config-file.conf index 75ffdf1..96ef2af 100644 --- a/sample-cli-config-file.conf +++ b/sample-cli-config-file.conf @@ -16,6 +16,10 @@ "caseSensitive": false, "minifyJS": false, "minifyCSS": false, + "minifySVG": { + "lol": 4, + "cleanupAttrs": true + }, "ignoreCustomComments": [], "processScripts": [] } diff --git a/src/htmlminifier.js b/src/htmlminifier.js index b291e25..a9c330a 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -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 ]*$/, @@ -561,6 +561,34 @@ 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 || {}; @@ -739,6 +767,9 @@ 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')) ? diff --git a/src/htmlparser.js b/src/htmlparser.js index 3fc5988..3f42e1e 100644 --- a/src/htmlparser.js +++ b/src/htmlparser.js @@ -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;