From 9c73de8bbd0404334d02ae8432324b5bfd34140f Mon Sep 17 00:00:00 2001 From: Martti Laine Date: Sun, 23 Dec 2018 21:54:32 +0200 Subject: [PATCH] fix: don't encode already encoded characters --- lib/get.js | 8 +++++++- test/fixtures/Encoded Space.gif | Bin 0 -> 35 bytes test/fixtures/bad-url.result.html | 2 +- .../image-inline-encoded-space.result.html | 1 + test/fixtures/image-inline-encoded-space.src.html | 10 ++++++++++ 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/Encoded Space.gif create mode 100644 test/fixtures/image-inline-encoded-space.result.html create mode 100644 test/fixtures/image-inline-encoded-space.src.html diff --git a/lib/get.js b/lib/get.js index 2e95330..3d17ee7 100644 --- a/lib/get.js +++ b/lib/get.js @@ -5,6 +5,7 @@ var Promise = require('es6-promise').Promise; // jshint ignore:line var fs = require('then-fs'); var mime = require('mime'); var basename = require('path').basename; +var { URL } = require('url'); var cache = {}; @@ -71,7 +72,12 @@ module.exports = function get(url, options) { debug('request %s', url, settings); cache[url] = new Promise(function (resolve) { - request(encodeURI(url), settings, function response(error, res, body) { + var encodedUrl = new URL(url).toString(); + if (encodedUrl !== url) { + inliner.emit('warning', 'url ' + url + ' was transformed to ' + + encodedUrl); + } + request(encodedUrl, settings, function response(error, res, body) { if (error) { debug('request failed: %s', error.message); inliner.emit('warning', 'failed to request ' + base + ' (' + diff --git a/test/fixtures/Encoded Space.gif b/test/fixtures/Encoded Space.gif new file mode 100644 index 0000000000000000000000000000000000000000..472727f293859cf3859f33a6bfbb0de9b609c4fb GIT binary patch literal 35 ncmZ?wbhEHbWMp7uXkcXc|NlP&1A`6_1Nn>$3`|Tej11NQg`Nf5 literal 0 HcmV?d00001 diff --git a/test/fixtures/bad-url.result.html b/test/fixtures/bad-url.result.html index ccbe846..6d3ba24 100644 --- a/test/fixtures/bad-url.result.html +++ b/test/fixtures/bad-url.result.html @@ -1 +1 @@ - + diff --git a/test/fixtures/image-inline-encoded-space.result.html b/test/fixtures/image-inline-encoded-space.result.html new file mode 100644 index 0000000..fa85742 --- /dev/null +++ b/test/fixtures/image-inline-encoded-space.result.html @@ -0,0 +1 @@ + inline image with encoded space in the name diff --git a/test/fixtures/image-inline-encoded-space.src.html b/test/fixtures/image-inline-encoded-space.src.html new file mode 100644 index 0000000..981e4d3 --- /dev/null +++ b/test/fixtures/image-inline-encoded-space.src.html @@ -0,0 +1,10 @@ + + + + + inline image with encoded space in the name + + + + +