-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(katzencore): Removed Any DOM Parser and Wrote own implementation
- Loading branch information
Showing
3 changed files
with
131 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,152 @@ | ||
<script setup lang="ts"> | ||
import { parse, type HTMLElement } from 'node-html-parser' | ||
import { onMounted, ref, watch } from '#imports' | ||
const props = defineProps({ | ||
content: String, | ||
}) | ||
const htmlElements = ref<HTMLElement[]>() | ||
interface Node { | ||
type: string | ||
} | ||
interface ElementNode extends Node { | ||
tag: string | ||
nodes: Node[] | ||
} | ||
interface TextNode extends Node { | ||
text: string | ||
} | ||
interface RecursiveElementNode extends ElementNode { | ||
parent?: RecursiveElementNode | ||
} | ||
const parseIntoNodes = (html: string): Node[] => { | ||
// loop through characters | ||
const characters = html.split('') | ||
let tagOpen = false | ||
let tagEnd = false | ||
let currentElementTag = '' | ||
let currentTextContent = '' | ||
let currentElement: RecursiveElementNode | undefined = undefined | ||
const elementTree = [] | ||
for (let i = 0; i < characters.length; i++) { | ||
const char = characters[i] | ||
if (tagOpen) { | ||
if (char === '/') { | ||
tagEnd = true | ||
} | ||
else if (char === '>') { | ||
tagOpen = false | ||
if (tagEnd) { | ||
// AN ELEMENT HAS ENDED | ||
if (currentElement) { | ||
currentElement = currentElement.parent | ||
} | ||
} | ||
else { | ||
// AN ELEMENT HAS STARTED | ||
const parent: RecursiveElementNode | undefined = currentElement | ||
currentElement = { | ||
tag: currentElementTag, | ||
nodes: [], | ||
parent, | ||
type: 'element', | ||
} | ||
if (parent) { | ||
parent.nodes.push(currentElement) | ||
} | ||
else { | ||
elementTree.push(currentElement) | ||
} | ||
if (currentElementTag === 'br') { | ||
currentElement = parent | ||
tagOpen = false | ||
} | ||
} | ||
} | ||
else { | ||
currentElementTag += char | ||
} | ||
continue | ||
} | ||
if (char === '<') { | ||
if (currentTextContent) { | ||
// A TEXT NODE HAS ENDED | ||
const textNode: TextNode = { | ||
text: currentTextContent, | ||
type: 'text', | ||
} | ||
currentTextContent = '' | ||
if (currentElement) { | ||
currentElement.nodes.push(textNode) | ||
} | ||
else { | ||
elementTree.push(textNode) | ||
} | ||
} | ||
currentElementTag = '' | ||
tagOpen = true | ||
tagEnd = false | ||
continue | ||
} | ||
currentTextContent += char | ||
} | ||
return elementTree | ||
} | ||
const nodesToHtml = (nodes: Node[]): string => { | ||
let html = '' | ||
for (const node of nodes) { | ||
if (node.type === 'element') { | ||
const element = node as ElementNode | ||
html += `<${element.tag}>${nodesToHtml(element.nodes)}</${element.tag}>` | ||
} | ||
else if (node.type === 'text') { | ||
const text = node as TextNode | ||
html += text.text | ||
} | ||
} | ||
return html | ||
} | ||
const parsedNodes = ref<Node[]>(parseIntoNodes(props.content || '')) | ||
onMounted(() => { | ||
updateHtmlElements(props.content || '') | ||
parsedNodes.value = parseIntoNodes(props.content || '') | ||
}) | ||
watch(() => props.content, (content) => { | ||
updateHtmlElements(content || '') | ||
parsedNodes.value = parseIntoNodes(content || '') | ||
}) | ||
const updateHtmlElements = (content: string) => { | ||
if (!content) return | ||
const parsedContent = parse(content) | ||
htmlElements.value = parsedContent.childNodes as HTMLElement[] | ||
} | ||
updateHtmlElements(props.content || '') | ||
const getElementTag = (element: HTMLElement) => { | ||
return element.rawTagName | ||
const getElementTag = (element: Node) => { | ||
if (element.type === 'element') { | ||
return (element as ElementNode).tag | ||
} | ||
return 'span' | ||
} | ||
const getElementContent = (element: HTMLElement) => { | ||
return element.innerHTML | ||
const getElementContent = (element: Node) => { | ||
return element.type === 'element' ? nodesToHtml((element as ElementNode).nodes) : '' | ||
} | ||
</script> | ||
|
||
<template> | ||
<component | ||
:is="getElementTag(element)" | ||
v-for="(element, index) in htmlElements" | ||
:key="index+element.rawTagName" | ||
v-for="(element, index) in parsedNodes" | ||
:key="index+element.type" | ||
v-hypertext="getElementContent(element)" | ||
/> | ||
</template> |