Skip to content

Commit 73796c4

Browse files
committed
simplify/robustify
1 parent 7c10b23 commit 73796c4

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

packages/svelte/tests/html_equal.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { assert } from 'vitest';
22

3-
/** @param {Element} node */
4-
function clean_children(node) {
3+
/**
4+
* @param {Element} node
5+
* @param {{ preserveComments: boolean }} opts
6+
*/
7+
function clean_children(node, opts) {
58
let previous = null;
69
let has_element_children = false;
710
let template =
@@ -56,20 +59,26 @@ function clean_children(node) {
5659

5760
continue;
5861
}
59-
} else if (child.nodeType === 8) {
62+
}
63+
64+
if (child.nodeType === 8 && !opts.preserveComments) {
6065
// comment
6166
child.remove();
6267
continue;
63-
} else if (child.nodeType === 1) {
68+
}
69+
70+
if (child.nodeType === 1 || child.nodeType === 8) {
6471
if (previous?.nodeType === 3) {
6572
const prev = /** @type {Text} */ (previous);
6673
prev.data = prev.data.replace(/^[^\S]+$/, '\n');
67-
} else if (previous?.nodeType === 1) {
74+
} else if (previous?.nodeType === 1 || previous?.nodeType === 8) {
6875
node.insertBefore(document.createTextNode('\n'), child);
6976
}
7077

71-
has_element_children = true;
72-
clean_children(/** @type {Element} */ (child));
78+
if (child.nodeType === 1) {
79+
has_element_children = true;
80+
clean_children(/** @type {Element} */ (child), opts);
81+
}
7382
}
7483

7584
previous = child;
@@ -103,9 +112,9 @@ function clean_children(node) {
103112
export function normalize_html(window, html, { preserveComments = false } = {}) {
104113
try {
105114
const node = window.document.createElement('div');
106-
node.innerHTML = html.replace(/(<!(--)?.*?\2>)/g, preserveComments ? '$1' : '').trim();
107115

108-
clean_children(node);
116+
node.innerHTML = html.trim();
117+
clean_children(node, { preserveComments });
109118

110119
return node.innerHTML;
111120
} catch (err) {

0 commit comments

Comments
 (0)