Created
September 24, 2025 10:54
-
-
Save gijsk/c3fcb07d3d193a88626e455adafca49c to your computer and use it in GitHub Desktop.
Alt phrasing manipulation loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function postProcessParagraph(p) { | |
| // Trim leading and trailing whitespace. | |
| while (p.firstChild && this._isWhitespace(p.firstChild)) { | |
| p.firstChild.remove(); | |
| } | |
| while (p.lastChild && this._isWhitespace(p.lastChild)) { | |
| p.lastChild.remove(); | |
| } | |
| // If there's nothing left, drop it. | |
| if (!p.firstChild) { | |
| p.remove(); | |
| } | |
| } | |
| // Turn all divs that don't have children block level elements into p's | |
| if (node.tagName === "DIV") { | |
| // Put phrasing content into paragraphs. | |
| var childNode = node.firstChild; | |
| var p = null; | |
| while (childNode) { | |
| var nextSibling = childNode.nextSibling; | |
| if (this._isPhrasingContent(childNode)) { | |
| if (!p) { | |
| p = doc.createElement("p"); | |
| node.replaceChild(p, childNode); | |
| } | |
| p.appendChild(childNode); | |
| } else if (p) { | |
| postProcessParagraph(p); | |
| p = null; | |
| } | |
| childNode = nextSibling; | |
| } | |
| if (p) { | |
| postProcessParagraph(p); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment