From 03273b78791c3359865040081fc58b53de782db8 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 1 Jun 2025 16:13:30 -0400 Subject: [PATCH 1/5] chore: better HTML normalization test helper --- packages/svelte/tests/html_equal.js | 96 +++++++++++-------- .../samples/binding-select/_config.js | 4 +- .../samples/input-list/_config.js | 4 +- .../samples/namespace-html/_config.js | 2 +- .../samples/select-in-each/_config.js | 4 +- .../svelte/tests/runtime-legacy/shared.ts | 4 +- 6 files changed, 66 insertions(+), 48 deletions(-) diff --git a/packages/svelte/tests/html_equal.js b/packages/svelte/tests/html_equal.js index 4c9e2a725332..f3ab5c54caae 100644 --- a/packages/svelte/tests/html_equal.js +++ b/packages/svelte/tests/html_equal.js @@ -3,6 +3,15 @@ import { assert } from 'vitest'; /** @param {Element} node */ function clean_children(node) { let previous = null; + let has_element_children = false; + let template = + node.nodeName === 'TEMPLATE' ? /** @type {HTMLTemplateElement} */ (node) : undefined; + + if (template) { + const div = document.createElement('div'); + div.append(template.content); + node = div; + } // sort attributes const attributes = Array.from(node.attributes).sort((a, b) => { @@ -14,6 +23,10 @@ function clean_children(node) { }); attributes.forEach((attr) => { + if ((attr.name === 'onload' || attr.name === 'onerror') && attr.value === 'this.__e=event') { + return; + } + node.setAttribute(attr.name, attr.value); }); @@ -27,23 +40,35 @@ function clean_children(node) { node.tagName !== 'tspan' ) { node.removeChild(child); + continue; } - text.data = text.data.replace(/[ \t\n\r\f]+/g, '\n'); + text.data = text.data.replace(/[^\S]+/g, ' '); if (previous && previous.nodeType === 3) { const prev = /** @type {Text} */ (previous); prev.data += text.data; - prev.data = prev.data.replace(/[ \t\n\r\f]+/g, '\n'); - node.removeChild(text); + text = prev; + text.data = text.data.replace(/[^\S]+/g, ' '); + + continue; } } else if (child.nodeType === 8) { // comment - // do nothing - } else { + child.remove(); + continue; + } else if (child.nodeType === 1) { + if (previous?.nodeType === 3) { + const prev = /** @type {Text} */ (previous); + prev.data = prev.data.replace(/^[^\S]+$/, '\n'); + } else if (previous?.nodeType === 1) { + node.insertBefore(document.createTextNode('\n'), child); + } + + has_element_children = true; clean_children(/** @type {Element} */ (child)); } @@ -53,37 +78,35 @@ function clean_children(node) { // collapse whitespace if (node.firstChild && node.firstChild.nodeType === 3) { const text = /** @type {Text} */ (node.firstChild); - text.data = text.data.replace(/^[ \t\n\r\f]+/, ''); - if (!text.data.length) node.removeChild(text); + text.data = text.data.trimStart(); } if (node.lastChild && node.lastChild.nodeType === 3) { const text = /** @type {Text} */ (node.lastChild); - text.data = text.data.replace(/[ \t\n\r\f]+$/, ''); - if (!text.data.length) node.removeChild(text); + text.data = text.data.trimEnd(); + } + + if (has_element_children && node.parentNode) { + node.innerHTML = `\n\t${node.innerHTML.replace(/\n/g, '\n\t')}\n`; + } + + if (template) { + template.innerHTML = node.innerHTML; } } /** * @param {Window} window * @param {string} html - * @param {{ removeDataSvelte?: boolean, preserveComments?: boolean }} param2 + * @param {{ preserveComments?: boolean }} opts */ -export function normalize_html( - window, - html, - { removeDataSvelte = false, preserveComments = false } -) { +export function normalize_html(window, html, { preserveComments = false } = {}) { try { const node = window.document.createElement('div'); - node.innerHTML = html - .replace(/()/g, preserveComments ? '$1' : '') - .replace(/(data-svelte-h="[^"]+")/g, removeDataSvelte ? '' : '$1') - .replace(/>[ \t\n\r\f]+<') - // Strip out the special onload/onerror hydration events from the test output - .replace(/\s?onerror="this.__e=event"|\s?onload="this.__e=event"/g, '') - .trim(); + node.innerHTML = html.replace(/()/g, preserveComments ? '$1' : '').trim(); + clean_children(node); + return node.innerHTML; } catch (err) { throw new Error(`Failed to normalize HTML:\n${html}\nCause: ${err}`); @@ -98,10 +121,7 @@ export function normalize_new_line(html) { return html.replace(/\r\n/g, '\n'); } -/** - * @param {{ removeDataSvelte?: boolean }} options - */ -export function setup_html_equal(options = {}) { +export function setup_html_equal() { /** * @param {string} actual * @param {string} expected @@ -109,11 +129,7 @@ export function setup_html_equal(options = {}) { */ const assert_html_equal = (actual, expected, message) => { try { - assert.deepEqual( - normalize_html(window, actual, options), - normalize_html(window, expected, options), - message - ); + assert.deepEqual(normalize_html(window, actual), normalize_html(window, expected), message); } catch (e) { if (Error.captureStackTrace) Error.captureStackTrace(/** @type {Error} */ (e), assert_html_equal); @@ -137,15 +153,17 @@ export function setup_html_equal(options = {}) { try { assert.deepEqual( withoutNormalizeHtml - ? normalize_new_line(actual.trim()) - .replace(/(\sdata-svelte-h="[^"]+")/g, options.removeDataSvelte ? '' : '$1') - .replace(/()/g, preserveComments !== false ? '$1' : '') - : normalize_html(window, actual.trim(), { ...options, preserveComments }), + ? normalize_new_line(actual.trim()).replace( + /()/g, + preserveComments !== false ? '$1' : '' + ) + : normalize_html(window, actual.trim(), { preserveComments }), withoutNormalizeHtml - ? normalize_new_line(expected.trim()) - .replace(/(\sdata-svelte-h="[^"]+")/g, options.removeDataSvelte ? '' : '$1') - .replace(/()/g, preserveComments !== false ? '$1' : '') - : normalize_html(window, expected.trim(), { ...options, preserveComments }), + ? normalize_new_line(expected.trim()).replace( + /()/g, + preserveComments !== false ? '$1' : '' + ) + : normalize_html(window, expected.trim(), { preserveComments }), message ); } catch (e) { diff --git a/packages/svelte/tests/runtime-legacy/samples/binding-select/_config.js b/packages/svelte/tests/runtime-legacy/samples/binding-select/_config.js index 2507f5fc83aa..996f68e39f89 100644 --- a/packages/svelte/tests/runtime-legacy/samples/binding-select/_config.js +++ b/packages/svelte/tests/runtime-legacy/samples/binding-select/_config.js @@ -25,7 +25,7 @@ export default test({

selected: one

@@ -54,7 +54,7 @@ export default test({

selected: two

diff --git a/packages/svelte/tests/runtime-legacy/samples/input-list/_config.js b/packages/svelte/tests/runtime-legacy/samples/input-list/_config.js index fe6a29207d4c..1e95aaafa6d9 100644 --- a/packages/svelte/tests/runtime-legacy/samples/input-list/_config.js +++ b/packages/svelte/tests/runtime-legacy/samples/input-list/_config.js @@ -4,7 +4,9 @@ export default test({ html: ` - + + ` }); diff --git a/packages/svelte/tests/runtime-legacy/samples/namespace-html/_config.js b/packages/svelte/tests/runtime-legacy/samples/namespace-html/_config.js index 3be9f0e92539..b7ecd04def65 100644 --- a/packages/svelte/tests/runtime-legacy/samples/namespace-html/_config.js +++ b/packages/svelte/tests/runtime-legacy/samples/namespace-html/_config.js @@ -9,7 +9,7 @@ export default test({ - +
hi
`, diff --git a/packages/svelte/tests/runtime-legacy/samples/select-in-each/_config.js b/packages/svelte/tests/runtime-legacy/samples/select-in-each/_config.js index 4c94ea1e0172..df03b7a053bc 100644 --- a/packages/svelte/tests/runtime-legacy/samples/select-in-each/_config.js +++ b/packages/svelte/tests/runtime-legacy/samples/select-in-each/_config.js @@ -7,7 +7,7 @@ export default test({ target.innerHTML, ` selected: a @@ -23,7 +23,7 @@ export default test({ target.innerHTML, ` selected: b diff --git a/packages/svelte/tests/runtime-legacy/shared.ts b/packages/svelte/tests/runtime-legacy/shared.ts index 690a7e3d98fe..c94c4ed42294 100644 --- a/packages/svelte/tests/runtime-legacy/shared.ts +++ b/packages/svelte/tests/runtime-legacy/shared.ts @@ -86,9 +86,7 @@ function unhandled_rejection_handler(err: Error) { const listeners = process.rawListeners('unhandledRejection'); -const { assert_html_equal, assert_html_equal_with_options } = setup_html_equal({ - removeDataSvelte: true -}); +const { assert_html_equal, assert_html_equal_with_options } = setup_html_equal(); beforeAll(() => { // @ts-expect-error TODO huh? From 7c10b237d15c563e5a9160f19fad04be8cc9c77f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 1 Jun 2025 16:20:36 -0400 Subject: [PATCH 2/5] simplify --- packages/svelte/tests/html_equal.js | 108 ++++++++---------- .../svelte/tests/runtime-legacy/shared.ts | 4 +- 2 files changed, 50 insertions(+), 62 deletions(-) diff --git a/packages/svelte/tests/html_equal.js b/packages/svelte/tests/html_equal.js index f3ab5c54caae..22e52417a2de 100644 --- a/packages/svelte/tests/html_equal.js +++ b/packages/svelte/tests/html_equal.js @@ -121,63 +121,53 @@ export function normalize_new_line(html) { return html.replace(/\r\n/g, '\n'); } -export function setup_html_equal() { - /** - * @param {string} actual - * @param {string} expected - * @param {string} [message] - */ - const assert_html_equal = (actual, expected, message) => { - try { - assert.deepEqual(normalize_html(window, actual), normalize_html(window, expected), message); - } catch (e) { - if (Error.captureStackTrace) - Error.captureStackTrace(/** @type {Error} */ (e), assert_html_equal); - throw e; - } - }; - - /** - * - * @param {string} actual - * @param {string} expected - * @param {{ preserveComments?: boolean, withoutNormalizeHtml?: boolean }} param2 - * @param {string} [message] - */ - const assert_html_equal_with_options = ( - actual, - expected, - { preserveComments, withoutNormalizeHtml }, - message - ) => { - try { - assert.deepEqual( - withoutNormalizeHtml - ? normalize_new_line(actual.trim()).replace( - /()/g, - preserveComments !== false ? '$1' : '' - ) - : normalize_html(window, actual.trim(), { preserveComments }), - withoutNormalizeHtml - ? normalize_new_line(expected.trim()).replace( - /()/g, - preserveComments !== false ? '$1' : '' - ) - : normalize_html(window, expected.trim(), { preserveComments }), - message - ); - } catch (e) { - if (Error.captureStackTrace) - Error.captureStackTrace(/** @type {Error} */ (e), assert_html_equal_with_options); - throw e; - } - }; - - return { - assert_html_equal, - assert_html_equal_with_options - }; -} +/** + * @param {string} actual + * @param {string} expected + * @param {string} [message] + */ +export const assert_html_equal = (actual, expected, message) => { + try { + assert.deepEqual(normalize_html(window, actual), normalize_html(window, expected), message); + } catch (e) { + if (Error.captureStackTrace) + Error.captureStackTrace(/** @type {Error} */ (e), assert_html_equal); + throw e; + } +}; -// Common case without options -export const { assert_html_equal, assert_html_equal_with_options } = setup_html_equal(); +/** + * + * @param {string} actual + * @param {string} expected + * @param {{ preserveComments?: boolean, withoutNormalizeHtml?: boolean }} param2 + * @param {string} [message] + */ +export const assert_html_equal_with_options = ( + actual, + expected, + { preserveComments, withoutNormalizeHtml }, + message +) => { + try { + assert.deepEqual( + withoutNormalizeHtml + ? normalize_new_line(actual.trim()).replace( + /()/g, + preserveComments !== false ? '$1' : '' + ) + : normalize_html(window, actual.trim(), { preserveComments }), + withoutNormalizeHtml + ? normalize_new_line(expected.trim()).replace( + /()/g, + preserveComments !== false ? '$1' : '' + ) + : normalize_html(window, expected.trim(), { preserveComments }), + message + ); + } catch (e) { + if (Error.captureStackTrace) + Error.captureStackTrace(/** @type {Error} */ (e), assert_html_equal_with_options); + throw e; + } +}; diff --git a/packages/svelte/tests/runtime-legacy/shared.ts b/packages/svelte/tests/runtime-legacy/shared.ts index c94c4ed42294..c0d1177a823e 100644 --- a/packages/svelte/tests/runtime-legacy/shared.ts +++ b/packages/svelte/tests/runtime-legacy/shared.ts @@ -7,7 +7,7 @@ import { flushSync, hydrate, mount, unmount } from 'svelte'; import { render } from 'svelte/server'; import { afterAll, assert, beforeAll } from 'vitest'; import { compile_directory, fragments } from '../helpers.js'; -import { setup_html_equal } from '../html_equal.js'; +import { assert_html_equal, assert_html_equal_with_options } from '../html_equal.js'; import { raf } from '../animation-helpers.js'; import type { CompileOptions } from '#compiler'; import { suite_with_variants, type BaseTest } from '../suite.js'; @@ -86,8 +86,6 @@ function unhandled_rejection_handler(err: Error) { const listeners = process.rawListeners('unhandledRejection'); -const { assert_html_equal, assert_html_equal_with_options } = setup_html_equal(); - beforeAll(() => { // @ts-expect-error TODO huh? process.prependListener('unhandledRejection', unhandled_rejection_handler); From 73796c49b05b2d2eb8c5120a78f599ebaaade377 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 1 Jun 2025 16:45:38 -0400 Subject: [PATCH 3/5] simplify/robustify --- packages/svelte/tests/html_equal.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/svelte/tests/html_equal.js b/packages/svelte/tests/html_equal.js index 22e52417a2de..b5e18fdd4286 100644 --- a/packages/svelte/tests/html_equal.js +++ b/packages/svelte/tests/html_equal.js @@ -1,7 +1,10 @@ import { assert } from 'vitest'; -/** @param {Element} node */ -function clean_children(node) { +/** + * @param {Element} node + * @param {{ preserveComments: boolean }} opts + */ +function clean_children(node, opts) { let previous = null; let has_element_children = false; let template = @@ -56,20 +59,26 @@ function clean_children(node) { continue; } - } else if (child.nodeType === 8) { + } + + if (child.nodeType === 8 && !opts.preserveComments) { // comment child.remove(); continue; - } else if (child.nodeType === 1) { + } + + if (child.nodeType === 1 || child.nodeType === 8) { if (previous?.nodeType === 3) { const prev = /** @type {Text} */ (previous); prev.data = prev.data.replace(/^[^\S]+$/, '\n'); - } else if (previous?.nodeType === 1) { + } else if (previous?.nodeType === 1 || previous?.nodeType === 8) { node.insertBefore(document.createTextNode('\n'), child); } - has_element_children = true; - clean_children(/** @type {Element} */ (child)); + if (child.nodeType === 1) { + has_element_children = true; + clean_children(/** @type {Element} */ (child), opts); + } } previous = child; @@ -103,9 +112,9 @@ function clean_children(node) { export function normalize_html(window, html, { preserveComments = false } = {}) { try { const node = window.document.createElement('div'); - node.innerHTML = html.replace(/()/g, preserveComments ? '$1' : '').trim(); - clean_children(node); + node.innerHTML = html.trim(); + clean_children(node, { preserveComments }); return node.innerHTML; } catch (err) { From c2732946825fc99a5d6adcbd047ebd7fa0fdfca2 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 1 Jun 2025 17:12:20 -0400 Subject: [PATCH 4/5] tweak --- packages/svelte/tests/html_equal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/tests/html_equal.js b/packages/svelte/tests/html_equal.js index b5e18fdd4286..44e0c3f5422e 100644 --- a/packages/svelte/tests/html_equal.js +++ b/packages/svelte/tests/html_equal.js @@ -96,7 +96,7 @@ function clean_children(node, opts) { } if (has_element_children && node.parentNode) { - node.innerHTML = `\n\t${node.innerHTML.replace(/\n/g, '\n\t')}\n`; + node.innerHTML = `\n\ ${node.innerHTML.replace(/\n/g, '\n ')}\n`; } if (template) { From 7f5530cba6d2d522da8dec90cf2198af09d70ce3 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 2 Jun 2025 16:26:44 -0400 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> --- packages/svelte/tests/html_equal.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/svelte/tests/html_equal.js b/packages/svelte/tests/html_equal.js index 44e0c3f5422e..6948d8db32d1 100644 --- a/packages/svelte/tests/html_equal.js +++ b/packages/svelte/tests/html_equal.js @@ -26,6 +26,7 @@ function clean_children(node, opts) { }); attributes.forEach((attr) => { + // Strip out the special onload/onerror hydration events from the test output if ((attr.name === 'onload' || attr.name === 'onerror') && attr.value === 'this.__e=event') { return; } @@ -67,6 +68,7 @@ function clean_children(node, opts) { continue; } + // add newlines for better readability and potentially recurse into children if (child.nodeType === 1 || child.nodeType === 8) { if (previous?.nodeType === 3) { const prev = /** @type {Text} */ (previous); @@ -95,6 +97,7 @@ function clean_children(node, opts) { text.data = text.data.trimEnd(); } + // indent code for better readability if (has_element_children && node.parentNode) { node.innerHTML = `\n\ ${node.innerHTML.replace(/\n/g, '\n ')}\n`; }