From 490538698ad6806e792e74e9bc526b1a53a7b721 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 8 Jul 2024 18:00:27 +0200 Subject: [PATCH] Create absolute filter-URLs when needed in `DOMFilterFactory` (issue 18406) This functionality is purposely limited to development mode and GENERIC builds, since it's unnecessary in e.g. the *built-in* Firefox PDF Viewer, and will only be used when a ``-element is actually present. *Please note:* We enforce relative filter-URLs when running tests, such that we test the same code as used in the Firefox PDF Viewer. --- src/display/display_utils.js | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 459df1f7c6b550..808fcd63742e42 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -49,6 +49,8 @@ class PixelsPerInch { * does the magic for us. */ class DOMFilterFactory extends BaseFilterFactory { + #baseUrl; + #_cache; #_defs; @@ -121,6 +123,28 @@ class DOMFilterFactory extends BaseFilterFactory { return [bufferR.join(","), bufferG.join(","), bufferB.join(",")]; } + #createUrl(id) { + if ( + typeof PDFJSDev === "undefined" || + PDFJSDev.test("GENERIC && !TESTING") + ) { + if (this.#baseUrl === undefined) { + const url = this.#document.URL; + if (url === this.#document.baseURI) { + // No ``-element present, hence a relative URL should work. + this.#baseUrl = ""; + } else if (isDataScheme(url)) { + warn('#createUrl: ignore "data:"-URL for performance reasons.'); + this.#baseUrl = ""; + } else { + this.#baseUrl = url.split("#", 1)[0]; + } + } + return `url(${this.#baseUrl}#${id})`; + } + return `url(${id})`; + } + addFilter(maps) { if (!maps) { return "none"; @@ -146,7 +170,7 @@ class DOMFilterFactory extends BaseFilterFactory { // https://www.w3.org/TR/SVG11/filters.html#feComponentTransferElement const id = `g_${this.#docId}_transfer_map_${this.#id++}`; - const url = `url(#${id})`; + const url = this.#createUrl(id); this.#cache.set(maps, url); this.#cache.set(key, url); @@ -232,7 +256,7 @@ class DOMFilterFactory extends BaseFilterFactory { filter ); - info.url = `url(#${id})`; + info.url = this.#createUrl(id); return info.url; } @@ -254,7 +278,7 @@ class DOMFilterFactory extends BaseFilterFactory { } const id = `g_${this.#docId}_alpha_map_${this.#id++}`; - const url = `url(#${id})`; + const url = this.#createUrl(id); this.#cache.set(map, url); this.#cache.set(key, url); @@ -287,7 +311,7 @@ class DOMFilterFactory extends BaseFilterFactory { } const id = `g_${this.#docId}_luminosity_map_${this.#id++}`; - const url = `url(#${id})`; + const url = this.#createUrl(id); this.#cache.set(map, url); this.#cache.set(key, url); @@ -389,7 +413,7 @@ class DOMFilterFactory extends BaseFilterFactory { filter ); - info.url = `url(#${id})`; + info.url = this.#createUrl(id); return info.url; }