()
| 412 | } |
| 413 | |
| 414 | function parseNode(): XmlNode { |
| 415 | if (xml[i] !== '<') throw new Error(`Expected '<' at ${i}`) |
| 416 | i++ |
| 417 | const name = readName() |
| 418 | const attrs = readAttributes() |
| 419 | skipWhitespace() |
| 420 | const node: XmlNode = { |
| 421 | name, |
| 422 | localName: localOf(name), |
| 423 | attributes: attrs, |
| 424 | children: [], |
| 425 | text: '', |
| 426 | } |
| 427 | if (xml[i] === '/') { |
| 428 | i += 2 // /> |
| 429 | return node |
| 430 | } |
| 431 | if (xml[i] !== '>') throw new Error(`Expected '>' at ${i}`) |
| 432 | i++ |
| 433 | |
| 434 | while (i < len) { |
| 435 | if (xml[i] === '<') { |
| 436 | if (xml.startsWith('<!--', i)) { |
| 437 | const end = xml.indexOf('-->', i) |
| 438 | i = end === -1 ? len : end + 3 |
| 439 | continue |
| 440 | } |
| 441 | if (xml.startsWith('<![CDATA[', i)) { |
| 442 | const end = xml.indexOf(']]>', i + 9) |
| 443 | const data = xml.slice(i + 9, end === -1 ? len : end) |
| 444 | node.text += data |
| 445 | i = end === -1 ? len : end + 3 |
| 446 | continue |
| 447 | } |
| 448 | if (xml[i + 1] === '/') { |
| 449 | i += 2 |
| 450 | while (i < len && xml[i] !== '>') i++ |
| 451 | if (i < len) i++ |
| 452 | return node |
| 453 | } |
| 454 | node.children.push(parseNode()) |
| 455 | } else { |
| 456 | const start = i |
| 457 | while (i < len && xml[i] !== '<') i++ |
| 458 | node.text += decodeEntities(xml.slice(start, i)) |
| 459 | } |
| 460 | } |
| 461 | return node |
| 462 | } |
| 463 | |
| 464 | // Skip XML declaration and DOCTYPE |
| 465 | while (i < len) { |
no test coverage detected