(rootNode: AstNode, rootBlockName: string)
| 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. |
no test coverage detected
searching dependent graphs…