* Deeply clone the shadow's DOM so that changes don't back-wash to the block. * * @param shadow A tree of XML elements. * @param opt_noId True if the encoder should skip the block ID. * @returns A tree of XML elements.
(shadow: Element, opt_noId?: boolean)
| 330 | * @returns A tree of XML elements. |
| 331 | */ |
| 332 | function cloneShadow(shadow: Element, opt_noId?: boolean): Element { |
| 333 | shadow = shadow.cloneNode(true) as Element; |
| 334 | // Walk the tree looking for whitespace. Don't prune whitespace in a tag. |
| 335 | let node: Node | null = shadow; |
| 336 | let textNode; |
| 337 | while (node) { |
| 338 | if (opt_noId && node.nodeName === 'shadow') { |
| 339 | // Strip off IDs from shadow blocks. There should never be a 'block' as |
| 340 | // a child of a 'shadow', so no need to check that. |
| 341 | (node as Element).removeAttribute('id'); |
| 342 | } |
| 343 | if (node.firstChild) { |
| 344 | node = node.firstChild; |
| 345 | } else { |
| 346 | while (node && !node.nextSibling) { |
| 347 | textNode = node; |
| 348 | node = node.parentNode; |
| 349 | if ( |
| 350 | textNode.nodeType === dom.NodeType.TEXT_NODE && |
| 351 | (textNode as Text).data.trim() === '' && |
| 352 | node?.firstChild !== textNode |
| 353 | ) { |
| 354 | // Prune whitespace after a tag. |
| 355 | dom.removeNode(textNode); |
| 356 | } |
| 357 | } |
| 358 | if (node) { |
| 359 | textNode = node; |
| 360 | node = node.nextSibling; |
| 361 | if ( |
| 362 | textNode.nodeType === dom.NodeType.TEXT_NODE && |
| 363 | (textNode as Text).data.trim() === '' |
| 364 | ) { |
| 365 | // Prune whitespace before a tag. |
| 366 | dom.removeNode(textNode); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | return shadow; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Converts a DOM structure into plain text. |
no test coverage detected