From 552851733ce175ab7f4e6397855159a08900d8a5 Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Wed, 5 Apr 2017 10:51:59 +0100 Subject: [PATCH] fix: don't corrupt whitespace in scripts Fixes #120 --- lib/index.js | 9 ++++++++- lib/tasks/js.js | 1 + test/README.md | 20 +++++++++++++++++++ test/fixtures/120-script-template.result.html | 7 +++++++ test/fixtures/120-script-template.src.html | 18 +++++++++++++++++ test/fixtures/backtick-string.result.html | 5 +++++ test/fixtures/backtick-string.src.html | 7 +++++++ test/fixtures/headers.result.html | 2 +- test/fixtures/headers.src.html | 2 +- test/index.test.js | 6 +++--- 10 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 test/README.md create mode 100644 test/fixtures/120-script-template.result.html create mode 100644 test/fixtures/120-script-template.src.html create mode 100644 test/fixtures/backtick-string.result.html create mode 100644 test/fixtures/backtick-string.src.html diff --git a/lib/index.js b/lib/index.js index bbf32bc..3542c27 100644 --- a/lib/index.js +++ b/lib/index.js @@ -190,7 +190,7 @@ function main() { inliner.url = url; // this is a hack for the `resolve` function later on return inliner.get(this.filename || url, { encoding: 'binary' }); }) - .catch(function isUrl(error) { + .catch(function isUrl() { // make the best guess as to whether we're working with a url if (inliner.url || url.indexOf('<') === -1) { url = inliner.url || inliner.source; @@ -312,12 +312,19 @@ function main() { // collapse the white space if (inliner.options.collapseWhitespace) { + debug('collapsing whitespace'); $('pre, textarea').each(function () { $(this).html($(this).html() .replace(/\n/g, '~~nl~~') .replace(/\s/g, '~~s~~')); }); + $('script').each(function () { + $(this).text($(this).text() + .replace(/\n/g, '~~nl~~') + .replace(/\s/g, '~~s~~')); + }); + html = $.html() .replace(/\s+/g, ' ') .replace(/~~nl~~/g, '\n') diff --git a/lib/tasks/js.js b/lib/tasks/js.js index 82ddd7a..6c085eb 100644 --- a/lib/tasks/js.js +++ b/lib/tasks/js.js @@ -16,6 +16,7 @@ function resolve(inliner, todo, $) { var isMinified = false; if (type && type.toLowerCase() !== 'text/javascript') { + debug('skipping %s', type); return false; } diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..f3cbb02 --- /dev/null +++ b/test/README.md @@ -0,0 +1,20 @@ +# Tests + +When filing bugs, if it's an inlining issue, ensure you include full test source to work against. + +This can be via a [PR](https://github.com/remy/inliner/pulls) or by linking to a [gist](https://gist.github.com) that include at least **two** (first) files: + +- `.src.html` +- `.result.html` + +When these are put in the [fixtures](https://github.com/remy/inliner/tree/master/test/fixtures) directory, they are automatically tested against. + +If there are any external assets the example needs, please also include these and name them with the same root as your example, i.e. `.css` or `.min.js` etc. + +In addition `.opts.json` can be loaded to help specify runtime options during the test. + +**To test a single fixture you can use:**: + +```bash +$ FILTER= npm test +``` diff --git a/test/fixtures/120-script-template.result.html b/test/fixtures/120-script-template.result.html new file mode 100644 index 0000000..297dc1e --- /dev/null +++ b/test/fixtures/120-script-template.result.html @@ -0,0 +1,7 @@ + diff --git a/test/fixtures/120-script-template.src.html b/test/fixtures/120-script-template.src.html new file mode 100644 index 0000000..ad08a9e --- /dev/null +++ b/test/fixtures/120-script-template.src.html @@ -0,0 +1,18 @@ + + + + + diff --git a/test/fixtures/backtick-string.result.html b/test/fixtures/backtick-string.result.html new file mode 100644 index 0000000..8147ddc --- /dev/null +++ b/test/fixtures/backtick-string.result.html @@ -0,0 +1,5 @@ + diff --git a/test/fixtures/backtick-string.src.html b/test/fixtures/backtick-string.src.html new file mode 100644 index 0000000..038f6b6 --- /dev/null +++ b/test/fixtures/backtick-string.src.html @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/test/fixtures/headers.result.html b/test/fixtures/headers.result.html index d4cd519..6abdd64 100644 --- a/test/fixtures/headers.result.html +++ b/test/fixtures/headers.result.html @@ -1 +1 @@ - + diff --git a/test/fixtures/headers.src.html b/test/fixtures/headers.src.html index dc69e18..20582b7 100644 --- a/test/fixtures/headers.src.html +++ b/test/fixtures/headers.src.html @@ -1 +1 @@ - \ No newline at end of file + diff --git a/test/index.test.js b/test/index.test.js index 3e70c0a..12d5890 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -8,7 +8,7 @@ var st = require('st'); var server; test('setup mock server', function (t) { - server = http.createServer(function(req, res) { + server = http.createServer(function (req, res) { if (isASCII(req.url)) { st(path.resolve(__dirname, 'fixtures'))(req, res); } @@ -39,7 +39,7 @@ test('inliner core functions', function coreTests(t) { t.ok(inliner, 'inline is instantiated'); var roundtripHTML = ''; - new Inliner(roundtripHTML, function(error, html) { + new Inliner(roundtripHTML, function (error, html) { t.equal(html, roundtripHTML, 'recognizes HTML as main input'); }); }); @@ -50,7 +50,7 @@ test('inliner handles given source as local', function sourcedTests(t) { t.plan(1); var content = fs.readFileSync(__dirname + '/fixtures/css-ext-import.src.html', 'utf8'); - new Inliner(content, function (error, content) { + new Inliner(content, function (error) { t.equal(error, null, 'treats local content as file'); }); });