From 6610d77eb84875d4edebba56ade826d14250819b Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Wed, 20 Apr 2016 18:57:16 +0800 Subject: [PATCH] allow minifyURLs to be simple URL string --- README.md | 6 +++--- assets/master.js | 3 --- cli.js | 11 +++++------ src/htmlminifier.js | 14 ++++++++++---- tests/minifier.js | 7 +++++++ 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 96d74e0..a4fdd25 100644 --- a/README.md +++ b/README.md @@ -57,9 +57,9 @@ 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`, `false`, `Object`, `Function(text, inline)`) | -| `minifyJS` | Minify JavaScript in script elements and event attributes (uses [UglifyJS](https://github.com/mishoo/UglifyJS2)) | `false` (could be `true`, `false`, `Object`, `Function(text, inline)`) | -| `minifyURLs` | Minify URLs in various attributes (uses [relateurl](https://github.com/stevenvachon/relateurl)) | `false` (could be `Object`, `Function(text)`) | +| `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)`) | +| `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` | | `preventAttributesEscaping` | Prevents the escaping of the values of attributes | `false` | | `processConditionalComments` | Process contents of conditional comments through minifier | `false` | diff --git a/assets/master.js b/assets/master.js index 8df11f7..567c896 100644 --- a/assets/master.js +++ b/assets/master.js @@ -37,9 +37,6 @@ case 'maxLineLength': value = parseInt(value); break; - case 'minifyURLs': - value = { site: value }; - break; case 'processScripts': value = value.split(/\s*,\s*/); } diff --git a/cli.js b/cli.js index d14178b..e066690 100755 --- a/cli.js +++ b/cli.js @@ -72,7 +72,10 @@ function parseJSON(value) { return JSON.parse(value); } catch (e) { - fatal('Could not parse JSON value \'' + value + '\''); + if (/^{/.test(value)) { + fatal('Could not parse JSON value \'' + value + '\''); + } + return value; } } } @@ -89,10 +92,6 @@ function parseJSONRegExpArray(value) { return value && value.map(parseRegExp); } -function parseSiteURL(value) { - return value && { site: value }; -} - function parseString(value) { return value; } @@ -116,7 +115,7 @@ var mainOptions = { maxLineLength: ['Max line length', parseInt], minifyCSS: ['Minify CSS in style elements and style attributes (uses clean-css)', parseJSON], minifyJS: ['Minify Javascript in script elements and on* attributes (uses uglify-js)', parseJSON], - minifyURLs: ['Minify URLs in various attributes (uses relateurl)', parseSiteURL], + minifyURLs: ['Minify URLs in various attributes (uses relateurl)', parseJSON], preserveLineBreaks: 'Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break.', preventAttributesEscaping: 'Prevents the escaping of the values of attributes.', processConditionalComments: 'Process contents of conditional comments through minifier', diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 6638ebf..447b0a5 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -619,8 +619,17 @@ function processOptions(options) { ]; } - if (typeof options.minifyURLs === 'object') { + if (!options.minifyURLs) { + options.minifyURLs = identity; + } + if (typeof options.minifyURLs !== 'function') { var minifyURLs = options.minifyURLs; + if (typeof minifyURLs === 'string') { + minifyURLs = { site: minifyURLs }; + } + else if (typeof minifyURLs !== 'object') { + minifyURLs = {}; + } options.minifyURLs = function(text) { try { return RelateUrl.relate(text, minifyURLs); @@ -631,9 +640,6 @@ function processOptions(options) { } }; } - else if (typeof options.minifyURLs !== 'function') { - options.minifyURLs = identity; - } if (!options.minifyJS) { options.minifyJS = identity; diff --git a/tests/minifier.js b/tests/minifier.js index d0f1642..09a7f17 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -1862,16 +1862,23 @@ test('style attribute minification', function() { test('url attribute minification', function() { input = '
link
'; output = '
link
'; + equal(minify(input, { minifyURLs: 'http://website.com/folder/' }), output); equal(minify(input, { minifyURLs: { site: 'http://website.com/folder/' } }), output); input = ''; + equal(minify(input, { minifyURLs: 'http://website.com/' }), input); equal(minify(input, { minifyURLs: { site: 'http://website.com/' } }), input); input = ''; + equal(minify(input, { minifyURLs: 'http://website.com/' }), input); equal(minify(input, { minifyURLs: { site: 'http://website.com/' } }), input); output = ''; equal(minify(input, { minifyCSS: true }), output); output = ''; + equal(minify(input, { + minifyCSS: true, + minifyURLs: 'http://website.com/' + }), output); equal(minify(input, { minifyCSS: true, minifyURLs: { site: 'http://website.com/' } -- 2.34.1