| 510 | } |
| 511 | |
| 512 | parse (html: string): MarkupNode { |
| 513 | const root: MarkupNode = { type: MarkupNodeType.doc, content: [] } |
| 514 | const state = new HtmlParseState(root, this.handlers) |
| 515 | |
| 516 | let rawPos: number | undefined |
| 517 | let rawDepth: number | undefined |
| 518 | |
| 519 | const parser = new Parser( |
| 520 | { |
| 521 | onopentag (tag: string, attributes: Record<string, string>) { |
| 522 | if (rawDepth !== undefined) { |
| 523 | rawDepth += 1 |
| 524 | return |
| 525 | } |
| 526 | |
| 527 | if (attributes.style !== undefined) { |
| 528 | const attrs = extractStyleAttrs(attributes, styleRules) |
| 529 | if (attrs !== undefined) { |
| 530 | attributes = { ...attributes, ...attrs } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | const handler = state.handlers[tag] |
| 535 | if (handler !== undefined) { |
| 536 | handler.handleOpenTag(state, tag, attributes) |
| 537 | } else { |
| 538 | rawPos = parser.startIndex |
| 539 | rawDepth = 0 |
| 540 | } |
| 541 | }, |
| 542 | |
| 543 | ontext (text: string) { |
| 544 | if (rawDepth !== undefined) { |
| 545 | return |
| 546 | } |
| 547 | |
| 548 | if (text.length === 0) { |
| 549 | return |
| 550 | } |
| 551 | |
| 552 | state.addText(text) |
| 553 | }, |
| 554 | |
| 555 | oncomment (text: string) { |
| 556 | if (rawDepth !== undefined) { |
| 557 | return |
| 558 | } |
| 559 | |
| 560 | state.openNode(MarkupNodeType.comment) |
| 561 | state.addText(text) |
| 562 | state.closeNode(MarkupNodeType.comment) |
| 563 | }, |
| 564 | |
| 565 | onclosetag (tag: string) { |
| 566 | if (rawDepth !== undefined) { |
| 567 | rawDepth -= 1 |
| 568 | |
| 569 | if (rawDepth === 0) { |