From afa47d92bc92752345cdc8cfe0540763540c9707 Mon Sep 17 00:00:00 2001 From: Alexandre Capt Date: Fri, 26 Jan 2024 11:37:21 +0100 Subject: [PATCH] feat: manage absolute images adjustment --- .../defaults/rules/adjustImageUrls.js | 41 ++++++++++++++----- src/importer/defaults/transformDOM.js | 4 +- .../fixtures/adjust-image-urls.expected.html | 12 ++++-- .../fixtures/adjust-image-urls.input.html | 12 ++++-- test/importers/defaults/transformDOM.spec.js | 3 ++ 5 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/importer/defaults/rules/adjustImageUrls.js b/src/importer/defaults/rules/adjustImageUrls.js index 8a10c3fb..7cc4873d 100644 --- a/src/importer/defaults/rules/adjustImageUrls.js +++ b/src/importer/defaults/rules/adjustImageUrls.js @@ -10,18 +10,39 @@ * governing permissions and limitations under the License. */ -export default function adjustImageUrls(main, url) { +export default function adjustImageUrls(main, url, current) { [...main.querySelectorAll('img')].forEach((img) => { const src = img.getAttribute('src'); - if (src && (src.startsWith('./') || src.startsWith('/') || src.startsWith('../'))) { - try { - const u = new URL(src, url); - // eslint-disable-next-line no-param-reassign - img.src = u.toString(); - } catch (e) { - // eslint-disable-next-line no-console - console.log(`Unable to adjust image URL ${img.src} - removing image`); - img.remove(); + if (src) { + if (src.startsWith('./') || src.startsWith('/') || src.startsWith('../')) { + // transform relative URLs to absolute URLs + try { + const targetUrl = new URL(src, url); + // eslint-disable-next-line no-param-reassign + img.src = targetUrl.toString(); + } catch (e) { + // eslint-disable-next-line no-console + console.log(`Unable to adjust image URL ${img.src} - removing image`); + img.remove(); + } + } else if (current) { + // also transform absolute URLs to current host + try { + const currentSrc = new URL(src); + const currentUrl = new URL(current); + if (currentSrc.host === currentUrl.host) { + // if current host is same than src host, switch src host with url host + // this is the case for absolutes URLs pointing to the same host + const targetUrl = new URL(url); + const newSrc = new URL(`${currentSrc.pathname}${currentSrc.search}${currentSrc.hash}`, `${targetUrl.protocol}//${targetUrl.host}`); + // eslint-disable-next-line no-param-reassign + img.src = newSrc.toString(); + } + } catch (e) { + // eslint-disable-next-line no-console + console.log(`Unable to adjust image URL ${img.src} - removing image`); + img.remove(); + } } } }); diff --git a/src/importer/defaults/transformDOM.js b/src/importer/defaults/transformDOM.js index 4e14ea6f..15293b80 100644 --- a/src/importer/defaults/transformDOM.js +++ b/src/importer/defaults/transformDOM.js @@ -17,7 +17,7 @@ import transformBackgroundImages from './rules/transformBackgroundImages.js'; export default async function transformDOM({ // eslint-disable-next-line no-unused-vars - url, document, html, params, + url, document, html, params = {}, }) { const main = document.body; @@ -35,7 +35,7 @@ export default async function transformDOM({ createMetadata(main, document); transformBackgroundImages(main, document); - adjustImageUrls(main, url); + adjustImageUrls(main, url, params.originalURL); convertIcons(main, document); return main; diff --git a/test/importers/defaults/fixtures/adjust-image-urls.expected.html b/test/importers/defaults/fixtures/adjust-image-urls.expected.html index da597a74..5ac61f8d 100644 --- a/test/importers/defaults/fixtures/adjust-image-urls.expected.html +++ b/test/importers/defaults/fixtures/adjust-image-urls.expected.html @@ -1,7 +1,13 @@

Hello World

- - - + + + + + + + + + \ No newline at end of file diff --git a/test/importers/defaults/fixtures/adjust-image-urls.input.html b/test/importers/defaults/fixtures/adjust-image-urls.input.html index 6133d9cc..f4f8cdec 100644 --- a/test/importers/defaults/fixtures/adjust-image-urls.input.html +++ b/test/importers/defaults/fixtures/adjust-image-urls.input.html @@ -2,9 +2,15 @@

Hello World

- - - + + + + + + + + + \ No newline at end of file diff --git a/test/importers/defaults/transformDOM.spec.js b/test/importers/defaults/transformDOM.spec.js index 5f77819d..4c84ffc4 100644 --- a/test/importers/defaults/transformDOM.spec.js +++ b/test/importers/defaults/transformDOM.spec.js @@ -77,6 +77,9 @@ describe('defaultTransformDOM tests', () => { it('default transformation adjusts image urls', async () => { await runTest('adjust-image-urls', { url: 'https://wwww.sample.com/path/page.html', + params: { + originalURL: 'https://wwww.currenthost.com/path/page.html', + }, }); });