(html: string, args: ParserArgs = {})
| 478 | * const rootNode = tinymce.html.DomParser({...}).parse('<b>text</b>'); |
| 479 | */ |
| 480 | const parse = (html: string, args: ParserArgs = {}): AstNode => { |
| 481 | const validate = defaultedSettings.validate; |
| 482 | const preferFullDocument = (args.context ?? defaultedSettings.root_name) === '#document'; |
| 483 | const rootName = args.context ?? (preferFullDocument ? 'html' : defaultedSettings.root_name); |
| 484 | |
| 485 | // Parse and sanitize the content |
| 486 | const element = parseAndSanitizeWithContext(html, rootName, args.format, preferFullDocument); |
| 487 | |
| 488 | TransparentElements.updateChildren(schema, element); |
| 489 | |
| 490 | // Create the AST representation |
| 491 | const rootNode = new AstNode(rootName, 11); |
| 492 | transferChildren(rootNode, element, schema.getSpecialElements(), sanitizer.sanitizeNamespaceElement, defaultedSettings.sanitize && defaultedSettings.allow_html_in_comments); |
| 493 | |
| 494 | // This next line is needed to fix a memory leak in chrome and firefox. |
| 495 | // For more information see TINY-9186 |
| 496 | element.innerHTML = ''; |
| 497 | |
| 498 | // Set up whitespace fixes |
| 499 | const [ whitespacePre, whitespacePost ] = whitespaceCleaner(rootNode, schema, defaultedSettings, args); |
| 500 | |
| 501 | // Find the invalid children in the tree |
| 502 | const invalidChildren: AstNode[] = []; |
| 503 | const invalidFinder = validate ? (node: AstNode) => findInvalidChildren(node, invalidChildren) : Fun.noop; |
| 504 | |
| 505 | // Set up attribute and node matching |
| 506 | const matches: FilterNode.FilterMatches = { nodes: {}, attributes: {}}; |
| 507 | const matchFinder = (node: AstNode) => FilterNode.matchNode(getNodeFilters(), getAttributeFilters(), node, matches); |
| 508 | |
| 509 | // Walk the dom, apply all of the above things |
| 510 | walkTree(rootNode, [ whitespacePre, matchFinder ], [ whitespacePost, invalidFinder ]); |
| 511 | |
| 512 | // Because we collected invalid children while walking backwards, we need to reverse the list before operating on them |
| 513 | invalidChildren.reverse(); |
| 514 | |
| 515 | // Fix invalid children or report invalid children in a contextual parsing |
| 516 | if (validate && invalidChildren.length > 0) { |
| 517 | if (args.context) { |
| 518 | args.invalid = true; |
| 519 | } else { |
| 520 | InvalidNodes.cleanInvalidNodes(invalidChildren, schema, rootNode, matchFinder); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Wrap nodes in the root into block elements if the root is body |
| 525 | const rootBlockName = getRootBlockName(defaultedSettings, args); |
| 526 | if (rootBlockName && (rootNode.name === 'body' || args.isRootContent)) { |
| 527 | addRootBlocks(rootNode, rootBlockName); |
| 528 | } |
| 529 | |
| 530 | // Run filters only when the contents is valid |
| 531 | if (!args.invalid) { |
| 532 | FilterNode.runFilters(matches, args); |
| 533 | } |
| 534 | |
| 535 | return rootNode; |
| 536 | }; |
| 537 |
nothing calls this directly
no test coverage detected
searching dependent graphs…