| 125 | } |
| 126 | |
| 127 | function walkParse5(p5node, xmlParent, xmlDoc) { |
| 128 | for (const child of p5node.childNodes || []) { |
| 129 | const name = child.nodeName |
| 130 | if (name === '#text') { |
| 131 | if (child.value != null) { |
| 132 | const t = xmlDoc.createTextNode(child.value) |
| 133 | if (child.sourceCodeLocation) t.__line = child.sourceCodeLocation.startLine |
| 134 | xmlParent.appendChild(t) |
| 135 | } |
| 136 | } else if (name === '#comment') { |
| 137 | try { |
| 138 | xmlParent.appendChild(xmlDoc.createComment(child.data || '')) |
| 139 | } catch { |
| 140 | // ignore comments xmldom rejects |
| 141 | } |
| 142 | } else if (name === '#documentType') { |
| 143 | // skip doctype |
| 144 | } else { |
| 145 | const tagName = child.tagName || name |
| 146 | let el |
| 147 | try { |
| 148 | el = xmlDoc.createElement(tagName) |
| 149 | } catch { |
| 150 | continue |
| 151 | } |
| 152 | for (const attr of child.attrs || []) { |
| 153 | try { |
| 154 | el.setAttribute(attr.name, attr.value) |
| 155 | } catch { |
| 156 | // ignore attrs xmldom rejects (namespaces, invalid names) |
| 157 | } |
| 158 | } |
| 159 | const loc = child.sourceCodeLocation |
| 160 | if (loc) { |
| 161 | el.__line = loc.startLine |
| 162 | el.__startOffset = loc.startOffset |
| 163 | el.__endOffset = loc.endOffset |
| 164 | el.__startTagEndOffset = loc.startTag ? loc.startTag.endOffset : loc.endOffset |
| 165 | } |
| 166 | xmlParent.appendChild(el) |
| 167 | walkParse5(child, el, xmlDoc) |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | function renderSnippet(node, source, snippetLen, full) { |
| 173 | if (typeof node.__startOffset !== 'number') { |