Last active
May 17, 2024 03:43
-
-
Save PatrickJS/0443b7b6e9c0983e46209e45a2734955 to your computer and use it in GitHub Desktop.
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
| const isNode = (value) => { | |
| return value && typeof value.nodeType === 'number'; | |
| }; | |
| const isDocument = (value) => { | |
| return value.nodeType === 9; | |
| }; | |
| const isElement = (value) => { | |
| return value.nodeType === 1; | |
| }; | |
| const isQwikElement = (value) => { | |
| const nodeType = value.nodeType; | |
| return nodeType === 1 || nodeType === 111; | |
| }; | |
| const isNodeElement = (value) => { | |
| const nodeType = value.nodeType; | |
| return nodeType === 1 || nodeType === 111 || nodeType === 3; | |
| }; | |
| const isVirtualElement = (value) => { | |
| return value.nodeType === 111; | |
| }; | |
| const isVirtualElementOpenComment = (value) => { | |
| return isComment(value) && value.data.startsWith('qv '); | |
| }; | |
| const isText = (value) => { | |
| return value.nodeType === 3; | |
| }; | |
| const isComment = (value) => { | |
| return value.nodeType === 8; | |
| }; | |
| const getTextNode = (mark) => { | |
| const nextNode = mark?.nextSibling || null; | |
| if (isText(nextNode)) { | |
| return nextNode; | |
| } else { | |
| const textNode = mark.ownerDocument.createTextNode(''); | |
| if (mark.parentElement) { | |
| mark.parentElement.insertBefore(textNode, mark); | |
| } | |
| return textNode; | |
| } | |
| }; | |
| const strToInt = (nu) => { | |
| return parseInt(nu, 36); | |
| }; | |
| const getID = (stuff) => { | |
| const index = stuff.indexOf('q:id='); | |
| if (index > 0) { | |
| return strToInt(stuff.slice(index + 5)); | |
| } | |
| return -1; | |
| }; | |
| const doc = document; | |
| const containerEl = document.documentElement; | |
| const SHOW_COMMENT = 128 | |
| const elements = new Map(); | |
| const text = new Map(); | |
| let node = null; | |
| let container = 0; | |
| // Collect all virtual elements | |
| const elementWalker = doc.createTreeWalker(containerEl, SHOW_COMMENT); | |
| while ((node = elementWalker.nextNode())) { | |
| const data = node.data; | |
| if (container === 0) { | |
| if (data.startsWith('qv ')) { | |
| const id = getID(data); // TODO: remove | |
| if (id >= 0) { | |
| elements.set(id, node); | |
| } | |
| } else if (data.startsWith('t=')) { | |
| const id = data.slice(2); | |
| const index = strToInt(id); | |
| const textNode = getTextNode(node); | |
| elements.set(index, textNode); | |
| text.set(index, textNode.data); | |
| } | |
| } | |
| if (data === 'cq') { | |
| container++; | |
| } else if (data === '/cq') { | |
| container--; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment