MCPcopy Index your code
hub / github.com/tinymce/tinymce / addRootBlocks

Function addRootBlocks

modules/tinymce/src/core/main/ts/api/html/DomParser.ts:418–468  ·  view source on GitHub ↗
(rootNode: AstNode, rootBlockName: string)

Source from the content-addressed store, hash-verified

416 };
417
418 const addRootBlocks = (rootNode: AstNode, rootBlockName: string): void => {
419 const blockElements = extend(makeMap(extraBlockLikeElements), schema.getBlockElements());
420 const startWhiteSpaceRegExp = /^[ \t\r\n]+/;
421 const endWhiteSpaceRegExp = /[ \t\r\n]+$/;
422
423 let node = rootNode.firstChild, rootBlockNode: AstNode | null = null;
424
425 // Removes whitespace at beginning and end of block so:
426 // <p> x </p> -> <p>x</p>
427 const trim = (rootBlock: AstNode | null) => {
428 if (rootBlock) {
429 node = rootBlock.firstChild;
430 if (node && node.type === 3) {
431 node.value = node.value?.replace(startWhiteSpaceRegExp, '');
432 }
433
434 node = rootBlock.lastChild;
435 if (node && node.type === 3) {
436 node.value = node.value?.replace(endWhiteSpaceRegExp, '');
437 }
438 }
439 };
440
441 // Check if rootBlock is valid within rootNode for example if P is valid in H1 if H1 is the contentEditable root
442 if (!schema.isValidChild(rootNode.name, rootBlockName.toLowerCase())) {
443 return;
444 }
445
446 while (node) {
447 const next = node.next;
448
449 if (isWrappableNode(blockElements, node)) {
450 if (!rootBlockNode) {
451 // Create a new root block element
452 rootBlockNode = new AstNode(rootBlockName, 1);
453 rootBlockNode.attr(defaultedSettings.forced_root_block_attrs);
454 rootNode.insert(rootBlockNode, node);
455 rootBlockNode.append(node);
456 } else {
457 rootBlockNode.append(node);
458 }
459 } else {
460 trim(rootBlockNode);
461 rootBlockNode = null;
462 }
463
464 node = next;
465 }
466
467 trim(rootBlockNode);
468 };
469
470 /**
471 * Parses the specified HTML string into a DOM like node tree and returns the result.

Callers 1

parseFunction · 0.70

Calls 7

extendFunction · 0.85
isWrappableNodeFunction · 0.85
attrMethod · 0.80
insertMethod · 0.80
appendMethod · 0.80
trimFunction · 0.70
makeMapFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…