diff --git a/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts b/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts index c8a84a4e0b..179635eb57 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts @@ -132,7 +132,7 @@ export default class AttributeWrapper extends BaseAttributeWrapper { `); } else if (this.is_src) { block.chunks.hydrate.push( - b`if (new URL(${element.var}.src, location).href !== (new URL(${init}, location)).href) ${method}(${element.var}, "${name}", ${this.last});` + b`if (!@src_url_equal(${element.var}.src, ${init})) ${method}(${element.var}, "${name}", ${this.last});` ); updater = b`${method}(${element.var}, "${name}", ${should_cache ? this.last : value});`; } else if (property_name) { @@ -193,7 +193,7 @@ export default class AttributeWrapper extends BaseAttributeWrapper { if (should_cache) { condition = this.is_src - ? x`${condition} && (new URL(${element.var}.src, location).href !== (new URL((${last} = ${value}), location)).href)` + ? x`${condition} && (!@src_url_equal(${element.var}.src, (${last} = ${value})))` : x`${condition} && (${last} !== (${last} = ${value}))`; } diff --git a/src/runtime/internal/utils.ts b/src/runtime/internal/utils.ts index 9f3da8589a..84bf75b0c0 100644 --- a/src/runtime/internal/utils.ts +++ b/src/runtime/internal/utils.ts @@ -40,6 +40,10 @@ export function safe_not_equal(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } +export function src_url_equal(a, b) { + return new URL(a, window.location.origin).href === (new URL(b, window.location.origin)).href; +} + export function not_equal(a, b) { return a != a ? b == b : a !== b; } diff --git a/test/js/samples/hydrated-void-element/expected.js b/test/js/samples/hydrated-void-element/expected.js index cd837769cb..2df469e689 100644 --- a/test/js/samples/hydrated-void-element/expected.js +++ b/test/js/samples/hydrated-void-element/expected.js @@ -11,7 +11,8 @@ import { insert, noop, safe_not_equal, - space + space, + src_url_equal } from "svelte/internal"; function create_fragment(ctx) { @@ -35,7 +36,7 @@ function create_fragment(ctx) { this.h(); }, h() { - if (new URL(img.src, location).href !== new URL(img_src_value = "donuts.jpg", location).href) attr(img, "src", img_src_value); + if (!src_url_equal(img.src, img_src_value = "donuts.jpg")) attr(img, "src", img_src_value); attr(img, "alt", "donuts"); }, m(target, anchor) { diff --git a/test/js/samples/src-attribute-check/expected.js b/test/js/samples/src-attribute-check/expected.js index f8174e948a..a09b5c589f 100644 --- a/test/js/samples/src-attribute-check/expected.js +++ b/test/js/samples/src-attribute-check/expected.js @@ -10,7 +10,8 @@ import { insert, noop, safe_not_equal, - space + space, + src_url_equal } from "svelte/internal"; function create_fragment(ctx) { @@ -35,9 +36,9 @@ function create_fragment(ctx) { }, h() { attr(img0, "alt", "potato"); - if (new URL(img0.src, location).href !== new URL(img0_src_value = /*url*/ ctx[0], location).href) attr(img0, "src", img0_src_value); + if (!src_url_equal(img0.src, img0_src_value = /*url*/ ctx[0])) attr(img0, "src", img0_src_value); attr(img1, "alt", "potato"); - if (new URL(img1.src, location).href !== new URL(img1_src_value = "" + (/*slug*/ ctx[1] + ".jpg"), location).href) attr(img1, "src", img1_src_value); + if (!src_url_equal(img1.src, img1_src_value = "" + (/*slug*/ ctx[1] + ".jpg"))) attr(img1, "src", img1_src_value); }, m(target, anchor) { insert(target, img0, anchor); @@ -45,11 +46,11 @@ function create_fragment(ctx) { insert(target, img1, anchor); }, p(ctx, [dirty]) { - if (dirty & /*url*/ 1 && new URL(img0.src, location).href !== new URL(img0_src_value = /*url*/ ctx[0], location).href) { + if (dirty & /*url*/ 1 && !src_url_equal(img0.src, img0_src_value = /*url*/ ctx[0])) { attr(img0, "src", img0_src_value); } - if (dirty & /*slug*/ 2 && new URL(img1.src, location).href !== new URL(img1_src_value = "" + (/*slug*/ ctx[1] + ".jpg"), location).href) { + if (dirty & /*slug*/ 2 && !src_url_equal(img1.src, img1_src_value = "" + (/*slug*/ ctx[1] + ".jpg"))) { attr(img1, "src", img1_src_value); } },