|
48 | 48 | * User configuration.
|
49 | 49 | */
|
50 | 50 |
|
51 |
| -import {Parser, Token, TokenizerMode, html} from 'parse5' |
52 |
| -import {pointStart, pointEnd} from 'unist-util-position' |
53 |
| -import {visit} from 'unist-util-visit' |
| 51 | +import extend from 'extend' |
54 | 52 | import {fromParse5} from 'hast-util-from-parse5'
|
55 | 53 | import {toParse5} from 'hast-util-to-parse5'
|
56 | 54 | import {htmlVoidElements} from 'html-void-elements'
|
| 55 | +import {Parser, Token, TokenizerMode, html} from 'parse5' |
| 56 | +import {pointStart, pointEnd} from 'unist-util-position' |
| 57 | +import {visit} from 'unist-util-visit' |
57 | 58 | import {zwitch} from 'zwitch'
|
58 | 59 |
|
59 | 60 | /** @type {ParserOptions} */
|
@@ -282,23 +283,18 @@ function stitch(node, state) {
|
282 | 283 | state.stitches = true
|
283 | 284 |
|
284 | 285 | /** @type {Node} */
|
285 |
| - let clone |
| 286 | + const clone = cloneWithoutChildren(node) |
286 | 287 |
|
287 | 288 | // Recurse, because to somewhat handle `[<x>]</x>` (where `[]` denotes the
|
288 | 289 | // passed through node).
|
289 |
| - if ('children' in node) { |
290 |
| - // To do: deep clone. |
291 |
| - clone = { |
292 |
| - ...node, |
293 |
| - children: raw( |
294 |
| - {type: 'root', children: node.children}, |
295 |
| - state.file, |
296 |
| - state.options |
297 |
| - // @ts-expect-error Assume a given parent yields a parent. |
298 |
| - ).children |
299 |
| - } |
300 |
| - } else { |
301 |
| - clone = {...node} |
| 290 | + if ('children' in node && 'children' in clone) { |
| 291 | + const fakeRoot = raw( |
| 292 | + {type: 'root', children: node.children}, |
| 293 | + state.file, |
| 294 | + state.options |
| 295 | + ) |
| 296 | + // @ts-expect-error Assume a given parent yields a parent. |
| 297 | + clone.children = fakeRoot.children |
302 | 298 | }
|
303 | 299 |
|
304 | 300 | // Hack: `value` is supposed to be a string, but as none of the tools
|
@@ -641,3 +637,17 @@ function createParse5Location(node) {
|
641 | 637 | function isOptions(value) {
|
642 | 638 | return Boolean(value && !('message' in value && 'messages' in value))
|
643 | 639 | }
|
| 640 | + |
| 641 | +/** |
| 642 | + * @template {Node} NodeType |
| 643 | + * Node type. |
| 644 | + * @param {NodeType} node |
| 645 | + * Node to clone. |
| 646 | + * @returns {NodeType} |
| 647 | + * Cloned node, without children. |
| 648 | + */ |
| 649 | +function cloneWithoutChildren(node) { |
| 650 | + return 'children' in node |
| 651 | + ? extend(true, {}, {...node, children: []}) |
| 652 | + : extend(true, {}, node) |
| 653 | +} |
0 commit comments