* @param {Element} node * @param {{ preserveComments: boolean }} opts
(node, opts)
| 6 | * @param {{ preserveComments: boolean }} opts |
| 7 | */ |
| 8 | function clean_children(node, opts) { |
| 9 | let previous = null; |
| 10 | let has_element_children = false; |
| 11 | let template = |
| 12 | node.nodeName.toUpperCase() === 'TEMPLATE' |
| 13 | ? /** @type {HTMLTemplateElement} */ (node) |
| 14 | : undefined; |
| 15 | |
| 16 | if (template) { |
| 17 | const div = document.createElement('div'); |
| 18 | div.append(template.content); |
| 19 | node = div; |
| 20 | } |
| 21 | |
| 22 | // sort attributes |
| 23 | const attributes = Array.from(node.attributes).sort((a, b) => { |
| 24 | return a.name < b.name ? -1 : 1; |
| 25 | }); |
| 26 | |
| 27 | attributes.forEach((attr) => { |
| 28 | if (attr.name !== 'xmlns') node.removeAttribute(attr.name); |
| 29 | }); |
| 30 | |
| 31 | attributes.forEach(({ name, value }) => { |
| 32 | // Strip out the special onload/onerror hydration events from the test output |
| 33 | if (['onload', 'onerror', 'xmlns'].includes(name) && value === 'this.__e=event') { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | if (name === 'class') { |
| 38 | value = value.replace(/svelte-\w+/, 'svelte-xyz123'); |
| 39 | } |
| 40 | |
| 41 | node.setAttribute(name, value); |
| 42 | }); |
| 43 | |
| 44 | for (let child of [...node.childNodes]) { |
| 45 | if (child.nodeType === TEXT_NODE) { |
| 46 | let text = /** @type {Text} */ (child); |
| 47 | |
| 48 | if ( |
| 49 | node.namespaceURI === 'http://www.w3.org/2000/svg' && |
| 50 | node.tagName !== 'text' && |
| 51 | node.tagName !== 'tspan' |
| 52 | ) { |
| 53 | node.removeChild(child); |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | text.data = text.data.replace(/[^\S]+/g, ' '); |
| 58 | |
| 59 | if (previous && previous.nodeType === TEXT_NODE) { |
| 60 | const prev = /** @type {Text} */ (previous); |
| 61 | |
| 62 | prev.data += text.data; |
| 63 | node.removeChild(text); |
| 64 | |
| 65 | text = prev; |
no test coverage detected