From 353a2a92cb30e12172045553981f6460558e4a4a Mon Sep 17 00:00:00 2001 From: Eugene Park Date: Sat, 20 Feb 2016 10:45:46 +0000 Subject: [PATCH 1/3] feat: support for only inlining local files --- cli/options.js | 2 ++ docs/usage.txt | 1 + lib/defaults.js | 1 + lib/tasks/images.js | 8 ++++++++ lib/tasks/js.js | 5 +++++ lib/tasks/links.js | 8 +++++++- lib/tasks/videos.js | 8 ++++++++ test/fixtures/inline-noremote.opts.json | 4 ++++ test/fixtures/inline-noremote.result.html | 1 + test/fixtures/inline-noremote.src.html | 17 +++++++++++++++++ 10 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/inline-noremote.opts.json create mode 100644 test/fixtures/inline-noremote.result.html create mode 100644 test/fixtures/inline-noremote.src.html diff --git a/cli/options.js b/cli/options.js index c3a954d..0bc57c6 100644 --- a/cli/options.js +++ b/cli/options.js @@ -12,6 +12,7 @@ function options(args) { 'noimages', 'nocompress', 'nosvg', + 'noremote', 'videos', 'inlinemin', ], @@ -28,6 +29,7 @@ function options(args) { n: 'nocompress', e: 'encoding', s: 'nosvg', + r: 'noremote', o: 'videos', m: 'inlinemin', H: 'header', diff --git a/docs/usage.txt b/docs/usage.txt index 1da3a42..d9363ef 100644 --- a/docs/usage.txt +++ b/docs/usage.txt @@ -11,6 +11,7 @@ -i, --noimages don't encode images - keeps files size small, but more requests -o, --videos encode videos (and their poster image) - disabled by default -s, --nosvg don't compress SVG (through SVGO) + -r, --noremote don't inline remote URLs (only local files) -v, --verbose echo on STDERR the progress of inlining -V, --version output the version number -h, --help output usage information diff --git a/lib/defaults.js b/lib/defaults.js index a3ea3ae..045d1bd 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -5,5 +5,6 @@ module.exports = function () { compressJS: true, collapseWhitespace: true, nosvg: false, // by default, DO compress SVG with SVGO + noremote: false, }; }; diff --git a/lib/tasks/images.js b/lib/tasks/images.js index d748925..b603fba 100644 --- a/lib/tasks/images.js +++ b/lib/tasks/images.js @@ -5,6 +5,14 @@ function resolve(inliner, todo, $) { debug('start %s links', todo.length); return todo.map(function images(image) { var url = inliner.resolve(inliner.url, $(image).attr('src')); + + if (inliner.options.noremote && + (url.indexOf('//') === 0 || url.indexOf('http') === 0)) { + debug('skipping remote image'); + inliner.emit('progress', 'skipping remote image'); + return false; + } + return inliner.image(url).then(function then(dataURL) { $(image).attr('src', dataURL); }).then(inliner.jobs.done.images); diff --git a/lib/tasks/js.js b/lib/tasks/js.js index c82396e..266960b 100644 --- a/lib/tasks/js.js +++ b/lib/tasks/js.js @@ -32,6 +32,11 @@ function resolve(inliner, todo, $) { inliner.emit('progress', 'skipping analytics script'); // ignore analytics return false; + } else if (inliner.options.noremote && + (src.indexOf('//') === 0 || src.indexOf('http') === 0)) { + debug('skipping remote scripts'); + inliner.emit('progress', 'skipping remote script'); + return false; } $script.removeAttr('src'); diff --git a/lib/tasks/links.js b/lib/tasks/links.js index d3fbfe1..655f360 100644 --- a/lib/tasks/links.js +++ b/lib/tasks/links.js @@ -8,6 +8,12 @@ function resolve(inliner, todo, $) { if (url.indexOf('http') !== 0) { url = inliner.resolve(inliner.url, url); } + if (inliner.options.noremote && + (url.indexOf('//') === 0 || url.indexOf('http') === 0)) { + debug('skipping remote links'); + inliner.emit('progress', 'skipping remote links'); + return false; + } inliner.emit('progress', 'processing external css ' + url); return inliner.get(url).then(function then(res) { var css = res.body; @@ -18,4 +24,4 @@ function resolve(inliner, todo, $) { $(link).replaceWith(''); }); }); -} \ No newline at end of file +} diff --git a/lib/tasks/videos.js b/lib/tasks/videos.js index 8624619..e684df0 100644 --- a/lib/tasks/videos.js +++ b/lib/tasks/videos.js @@ -6,6 +6,14 @@ function resolve(inliner, todo, $) { return todo.map(function videos(video) { var url = inliner.resolve(inliner.url, $(video).attr('src')); var posterUrl = inliner.resolve(inliner.url, $(video).attr('poster')); + + if (inliner.options.noremote && + (url.indexOf('//') === 0 || url.indexOf('http') === 0)) { + debug('skipping remote video'); + inliner.emit('progress', 'skipping remote video'); + return false; + } + return Promise.all([ inliner.image(url).then(function then(dataURL) { $(video).attr('src', dataURL); diff --git a/test/fixtures/inline-noremote.opts.json b/test/fixtures/inline-noremote.opts.json new file mode 100644 index 0000000..8c901a1 --- /dev/null +++ b/test/fixtures/inline-noremote.opts.json @@ -0,0 +1,4 @@ +{ + "noremote": true, + "videos": true +} diff --git a/test/fixtures/inline-noremote.result.html b/test/fixtures/inline-noremote.result.html new file mode 100644 index 0000000..e873bc0 --- /dev/null +++ b/test/fixtures/inline-noremote.result.html @@ -0,0 +1 @@ + min script diff --git a/test/fixtures/inline-noremote.src.html b/test/fixtures/inline-noremote.src.html new file mode 100644 index 0000000..63e2b10 --- /dev/null +++ b/test/fixtures/inline-noremote.src.html @@ -0,0 +1,17 @@ + + + + + min script + + + + + + + + + + + + From 15e6b0dab671dfabfe1d2b6a6c2636c86d79eb7a Mon Sep 17 00:00:00 2001 From: Eugene Park Date: Sat, 20 Feb 2016 11:10:38 +0000 Subject: [PATCH 2/3] chore: renamed flag to --same-origin-only --- cli/options.js | 6 ++++-- docs/usage.txt | 3 ++- lib/defaults.js | 2 +- lib/tasks/images.js | 10 +++++----- lib/tasks/js.js | 4 ++-- lib/tasks/links.js | 2 +- lib/tasks/videos.js | 10 +++++----- test/fixtures/inline-noremote.opts.json | 4 ---- test/fixtures/inline-noremote.result.html | 1 - test/fixtures/inline-same-origin-only.opts.json | 4 ++++ test/fixtures/inline-same-origin-only.result.html | 1 + ...emote.src.html => inline-same-origin-only.src.html} | 2 +- 12 files changed, 26 insertions(+), 23 deletions(-) delete mode 100644 test/fixtures/inline-noremote.opts.json delete mode 100644 test/fixtures/inline-noremote.result.html create mode 100644 test/fixtures/inline-same-origin-only.opts.json create mode 100644 test/fixtures/inline-same-origin-only.result.html rename test/fixtures/{inline-noremote.src.html => inline-same-origin-only.src.html} (94%) diff --git a/cli/options.js b/cli/options.js index 0bc57c6..611cf9b 100644 --- a/cli/options.js +++ b/cli/options.js @@ -12,7 +12,7 @@ function options(args) { 'noimages', 'nocompress', 'nosvg', - 'noremote', + 'same-origin-only', 'videos', 'inlinemin', ], @@ -29,7 +29,6 @@ function options(args) { n: 'nocompress', e: 'encoding', s: 'nosvg', - r: 'noremote', o: 'videos', m: 'inlinemin', H: 'header', @@ -42,6 +41,9 @@ function options(args) { argv.compressJS = false; argv.collapseWhitespace = false; } + if (argv['same-origin-only']) { + argv.sameOriginOnly = true; + } argv.images = !argv.noimages; argv.useStdin = !process.stdin.isTTY; diff --git a/docs/usage.txt b/docs/usage.txt index d9363ef..7e4e6d2 100644 --- a/docs/usage.txt +++ b/docs/usage.txt @@ -11,7 +11,8 @@ -i, --noimages don't encode images - keeps files size small, but more requests -o, --videos encode videos (and their poster image) - disabled by default -s, --nosvg don't compress SVG (through SVGO) - -r, --noremote don't inline remote URLs (only local files) + --same-origin-only + don't inline files with URLs on other hosts (only local files) -v, --verbose echo on STDERR the progress of inlining -V, --version output the version number -h, --help output usage information diff --git a/lib/defaults.js b/lib/defaults.js index 045d1bd..db8a53c 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -5,6 +5,6 @@ module.exports = function () { compressJS: true, collapseWhitespace: true, nosvg: false, // by default, DO compress SVG with SVGO - noremote: false, + sameOriginOnly: false, }; }; diff --git a/lib/tasks/images.js b/lib/tasks/images.js index b603fba..be05784 100644 --- a/lib/tasks/images.js +++ b/lib/tasks/images.js @@ -6,11 +6,11 @@ function resolve(inliner, todo, $) { return todo.map(function images(image) { var url = inliner.resolve(inliner.url, $(image).attr('src')); - if (inliner.options.noremote && - (url.indexOf('//') === 0 || url.indexOf('http') === 0)) { - debug('skipping remote image'); - inliner.emit('progress', 'skipping remote image'); - return false; + if (inliner.options.sameOriginOnly && + (url.indexOf('//') === 0 || url.indexOf('http') === 0)) { + debug('skipping remote image'); + inliner.emit('progress', 'skipping remote image'); + return false; } return inliner.image(url).then(function then(dataURL) { diff --git a/lib/tasks/js.js b/lib/tasks/js.js index 266960b..efad194 100644 --- a/lib/tasks/js.js +++ b/lib/tasks/js.js @@ -32,8 +32,8 @@ function resolve(inliner, todo, $) { inliner.emit('progress', 'skipping analytics script'); // ignore analytics return false; - } else if (inliner.options.noremote && - (src.indexOf('//') === 0 || src.indexOf('http') === 0)) { + } else if (inliner.options.sameOriginOnly && + (src.indexOf('//') === 0 || src.indexOf('http') === 0)) { debug('skipping remote scripts'); inliner.emit('progress', 'skipping remote script'); return false; diff --git a/lib/tasks/links.js b/lib/tasks/links.js index 655f360..cdb8c02 100644 --- a/lib/tasks/links.js +++ b/lib/tasks/links.js @@ -8,7 +8,7 @@ function resolve(inliner, todo, $) { if (url.indexOf('http') !== 0) { url = inliner.resolve(inliner.url, url); } - if (inliner.options.noremote && + if (inliner.options.sameOriginOnly && (url.indexOf('//') === 0 || url.indexOf('http') === 0)) { debug('skipping remote links'); inliner.emit('progress', 'skipping remote links'); diff --git a/lib/tasks/videos.js b/lib/tasks/videos.js index e684df0..9235c01 100644 --- a/lib/tasks/videos.js +++ b/lib/tasks/videos.js @@ -7,11 +7,11 @@ function resolve(inliner, todo, $) { var url = inliner.resolve(inliner.url, $(video).attr('src')); var posterUrl = inliner.resolve(inliner.url, $(video).attr('poster')); - if (inliner.options.noremote && - (url.indexOf('//') === 0 || url.indexOf('http') === 0)) { - debug('skipping remote video'); - inliner.emit('progress', 'skipping remote video'); - return false; + if (inliner.options.sameOriginOnly && + (url.indexOf('//') === 0 || url.indexOf('http') === 0)) { + debug('skipping remote video'); + inliner.emit('progress', 'skipping remote video'); + return false; } return Promise.all([ diff --git a/test/fixtures/inline-noremote.opts.json b/test/fixtures/inline-noremote.opts.json deleted file mode 100644 index 8c901a1..0000000 --- a/test/fixtures/inline-noremote.opts.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "noremote": true, - "videos": true -} diff --git a/test/fixtures/inline-noremote.result.html b/test/fixtures/inline-noremote.result.html deleted file mode 100644 index e873bc0..0000000 --- a/test/fixtures/inline-noremote.result.html +++ /dev/null @@ -1 +0,0 @@ - min script diff --git a/test/fixtures/inline-same-origin-only.opts.json b/test/fixtures/inline-same-origin-only.opts.json new file mode 100644 index 0000000..1348bde --- /dev/null +++ b/test/fixtures/inline-same-origin-only.opts.json @@ -0,0 +1,4 @@ +{ + "sameOriginOnly": true, + "videos": true +} diff --git a/test/fixtures/inline-same-origin-only.result.html b/test/fixtures/inline-same-origin-only.result.html new file mode 100644 index 0000000..1bedc8e --- /dev/null +++ b/test/fixtures/inline-same-origin-only.result.html @@ -0,0 +1 @@ + no remote inline diff --git a/test/fixtures/inline-noremote.src.html b/test/fixtures/inline-same-origin-only.src.html similarity index 94% rename from test/fixtures/inline-noremote.src.html rename to test/fixtures/inline-same-origin-only.src.html index 63e2b10..d69fbdc 100644 --- a/test/fixtures/inline-noremote.src.html +++ b/test/fixtures/inline-same-origin-only.src.html @@ -2,7 +2,7 @@ - min script + no remote inline From 825ec601dcf5487bbbd1924016af8da23e35e740 Mon Sep 17 00:00:00 2001 From: Eugene Park Date: Wed, 6 Apr 2016 07:53:43 +0000 Subject: [PATCH 3/3] chore: renamed flag to --skip-absolute-urls --- cli/options.js | 6 +++--- docs/usage.txt | 4 ++-- lib/defaults.js | 2 +- lib/tasks/images.js | 2 +- lib/tasks/js.js | 2 +- lib/tasks/links.js | 2 +- lib/tasks/videos.js | 2 +- test/fixtures/inline-same-origin-only.opts.json | 4 ---- test/fixtures/inline-skip-absolute-urls.opts.json | 4 ++++ ...ly.result.html => inline-skip-absolute-urls.result.html} | 0 ...gin-only.src.html => inline-skip-absolute-urls.src.html} | 0 11 files changed, 14 insertions(+), 14 deletions(-) delete mode 100644 test/fixtures/inline-same-origin-only.opts.json create mode 100644 test/fixtures/inline-skip-absolute-urls.opts.json rename test/fixtures/{inline-same-origin-only.result.html => inline-skip-absolute-urls.result.html} (100%) rename test/fixtures/{inline-same-origin-only.src.html => inline-skip-absolute-urls.src.html} (100%) diff --git a/cli/options.js b/cli/options.js index 611cf9b..cb82b88 100644 --- a/cli/options.js +++ b/cli/options.js @@ -12,7 +12,7 @@ function options(args) { 'noimages', 'nocompress', 'nosvg', - 'same-origin-only', + 'skip-absolute-urls', 'videos', 'inlinemin', ], @@ -41,8 +41,8 @@ function options(args) { argv.compressJS = false; argv.collapseWhitespace = false; } - if (argv['same-origin-only']) { - argv.sameOriginOnly = true; + if (argv['skip-absolute-urls']) { + argv.skipAbsoluteUrls = true; } argv.images = !argv.noimages; diff --git a/docs/usage.txt b/docs/usage.txt index 7e4e6d2..1c4e923 100644 --- a/docs/usage.txt +++ b/docs/usage.txt @@ -11,12 +11,12 @@ -i, --noimages don't encode images - keeps files size small, but more requests -o, --videos encode videos (and their poster image) - disabled by default -s, --nosvg don't compress SVG (through SVGO) - --same-origin-only - don't inline files with URLs on other hosts (only local files) -v, --verbose echo on STDERR the progress of inlining -V, --version output the version number -h, --help output usage information -m, --inlinemin inline minified files + --skip-absolute-urls + don't inline files with fully-qualified absolute URLs Options: diff --git a/lib/defaults.js b/lib/defaults.js index db8a53c..9a119a3 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -5,6 +5,6 @@ module.exports = function () { compressJS: true, collapseWhitespace: true, nosvg: false, // by default, DO compress SVG with SVGO - sameOriginOnly: false, + skipAbsoluteUrls: false, }; }; diff --git a/lib/tasks/images.js b/lib/tasks/images.js index be05784..3bd41f5 100644 --- a/lib/tasks/images.js +++ b/lib/tasks/images.js @@ -6,7 +6,7 @@ function resolve(inliner, todo, $) { return todo.map(function images(image) { var url = inliner.resolve(inliner.url, $(image).attr('src')); - if (inliner.options.sameOriginOnly && + if (inliner.options.skipAbsoluteUrls && (url.indexOf('//') === 0 || url.indexOf('http') === 0)) { debug('skipping remote image'); inliner.emit('progress', 'skipping remote image'); diff --git a/lib/tasks/js.js b/lib/tasks/js.js index efad194..405727b 100644 --- a/lib/tasks/js.js +++ b/lib/tasks/js.js @@ -32,7 +32,7 @@ function resolve(inliner, todo, $) { inliner.emit('progress', 'skipping analytics script'); // ignore analytics return false; - } else if (inliner.options.sameOriginOnly && + } else if (inliner.options.skipAbsoluteUrls && (src.indexOf('//') === 0 || src.indexOf('http') === 0)) { debug('skipping remote scripts'); inliner.emit('progress', 'skipping remote script'); diff --git a/lib/tasks/links.js b/lib/tasks/links.js index cdb8c02..76dae86 100644 --- a/lib/tasks/links.js +++ b/lib/tasks/links.js @@ -8,7 +8,7 @@ function resolve(inliner, todo, $) { if (url.indexOf('http') !== 0) { url = inliner.resolve(inliner.url, url); } - if (inliner.options.sameOriginOnly && + if (inliner.options.skipAbsoluteUrls && (url.indexOf('//') === 0 || url.indexOf('http') === 0)) { debug('skipping remote links'); inliner.emit('progress', 'skipping remote links'); diff --git a/lib/tasks/videos.js b/lib/tasks/videos.js index 9235c01..6f73911 100644 --- a/lib/tasks/videos.js +++ b/lib/tasks/videos.js @@ -7,7 +7,7 @@ function resolve(inliner, todo, $) { var url = inliner.resolve(inliner.url, $(video).attr('src')); var posterUrl = inliner.resolve(inliner.url, $(video).attr('poster')); - if (inliner.options.sameOriginOnly && + if (inliner.options.skipAbsoluteUrls && (url.indexOf('//') === 0 || url.indexOf('http') === 0)) { debug('skipping remote video'); inliner.emit('progress', 'skipping remote video'); diff --git a/test/fixtures/inline-same-origin-only.opts.json b/test/fixtures/inline-same-origin-only.opts.json deleted file mode 100644 index 1348bde..0000000 --- a/test/fixtures/inline-same-origin-only.opts.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "sameOriginOnly": true, - "videos": true -} diff --git a/test/fixtures/inline-skip-absolute-urls.opts.json b/test/fixtures/inline-skip-absolute-urls.opts.json new file mode 100644 index 0000000..050589f --- /dev/null +++ b/test/fixtures/inline-skip-absolute-urls.opts.json @@ -0,0 +1,4 @@ +{ + "skipAbsoluteUrls": true, + "videos": true +} diff --git a/test/fixtures/inline-same-origin-only.result.html b/test/fixtures/inline-skip-absolute-urls.result.html similarity index 100% rename from test/fixtures/inline-same-origin-only.result.html rename to test/fixtures/inline-skip-absolute-urls.result.html diff --git a/test/fixtures/inline-same-origin-only.src.html b/test/fixtures/inline-skip-absolute-urls.src.html similarity index 100% rename from test/fixtures/inline-same-origin-only.src.html rename to test/fixtures/inline-skip-absolute-urls.src.html