|
|
@ -1,3 +1,4 @@
|
|
|
|
|
|
|
|
import { COMMENT_NODE, ELEMENT_NODE, TEXT_NODE } from '#client/constants';
|
|
|
|
import { assert } from 'vitest';
|
|
|
|
import { assert } from 'vitest';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -35,7 +36,7 @@ function clean_children(node, opts) {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
for (let child of [...node.childNodes]) {
|
|
|
|
for (let child of [...node.childNodes]) {
|
|
|
|
if (child.nodeType === 3) {
|
|
|
|
if (child.nodeType === TEXT_NODE) {
|
|
|
|
let text = /** @type {Text} */ (child);
|
|
|
|
let text = /** @type {Text} */ (child);
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
if (
|
|
|
@ -49,7 +50,7 @@ function clean_children(node, opts) {
|
|
|
|
|
|
|
|
|
|
|
|
text.data = text.data.replace(/[^\S]+/g, ' ');
|
|
|
|
text.data = text.data.replace(/[^\S]+/g, ' ');
|
|
|
|
|
|
|
|
|
|
|
|
if (previous && previous.nodeType === 3) {
|
|
|
|
if (previous && previous.nodeType === TEXT_NODE) {
|
|
|
|
const prev = /** @type {Text} */ (previous);
|
|
|
|
const prev = /** @type {Text} */ (previous);
|
|
|
|
|
|
|
|
|
|
|
|
prev.data += text.data;
|
|
|
|
prev.data += text.data;
|
|
|
@ -62,22 +63,22 @@ function clean_children(node, opts) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (child.nodeType === 8 && !opts.preserveComments) {
|
|
|
|
if (child.nodeType === COMMENT_NODE && !opts.preserveComments) {
|
|
|
|
// comment
|
|
|
|
// comment
|
|
|
|
child.remove();
|
|
|
|
child.remove();
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// add newlines for better readability and potentially recurse into children
|
|
|
|
// add newlines for better readability and potentially recurse into children
|
|
|
|
if (child.nodeType === 1 || child.nodeType === 8) {
|
|
|
|
if (child.nodeType === ELEMENT_NODE || child.nodeType === COMMENT_NODE) {
|
|
|
|
if (previous?.nodeType === 3) {
|
|
|
|
if (previous?.nodeType === TEXT_NODE) {
|
|
|
|
const prev = /** @type {Text} */ (previous);
|
|
|
|
const prev = /** @type {Text} */ (previous);
|
|
|
|
prev.data = prev.data.replace(/^[^\S]+$/, '\n');
|
|
|
|
prev.data = prev.data.replace(/^[^\S]+$/, '\n');
|
|
|
|
} else if (previous?.nodeType === 1 || previous?.nodeType === 8) {
|
|
|
|
} else if (previous?.nodeType === ELEMENT_NODE || previous?.nodeType === COMMENT_NODE) {
|
|
|
|
node.insertBefore(document.createTextNode('\n'), child);
|
|
|
|
node.insertBefore(document.createTextNode('\n'), child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (child.nodeType === 1) {
|
|
|
|
if (child.nodeType === ELEMENT_NODE) {
|
|
|
|
has_element_children = true;
|
|
|
|
has_element_children = true;
|
|
|
|
clean_children(/** @type {Element} */ (child), opts);
|
|
|
|
clean_children(/** @type {Element} */ (child), opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -87,12 +88,12 @@ function clean_children(node, opts) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// collapse whitespace
|
|
|
|
// collapse whitespace
|
|
|
|
if (node.firstChild && node.firstChild.nodeType === 3) {
|
|
|
|
if (node.firstChild && node.firstChild.nodeType === TEXT_NODE) {
|
|
|
|
const text = /** @type {Text} */ (node.firstChild);
|
|
|
|
const text = /** @type {Text} */ (node.firstChild);
|
|
|
|
text.data = text.data.trimStart();
|
|
|
|
text.data = text.data.trimStart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (node.lastChild && node.lastChild.nodeType === 3) {
|
|
|
|
if (node.lastChild && node.lastChild.nodeType === TEXT_NODE) {
|
|
|
|
const text = /** @type {Text} */ (node.lastChild);
|
|
|
|
const text = /** @type {Text} */ (node.lastChild);
|
|
|
|
text.data = text.data.trimEnd();
|
|
|
|
text.data = text.data.trimEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|