MCPcopy
hub / github.com/tinymce/tinymce / parse

Function parse

modules/tinymce/src/core/main/ts/api/html/DomParser.ts:480–536  ·  view source on GitHub ↗
(html: string, args: ParserArgs = {})

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 7

transferChildrenFunction · 0.85
whitespaceCleanerFunction · 0.85
findInvalidChildrenFunction · 0.85
walkTreeFunction · 0.85
getRootBlockNameFunction · 0.85
addRootBlocksFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…