eaa705ab by Adam Heath

Add support for attribute decoding, via a proxy(this should really be

fixed upstream).
1 parent 9b920fd6
import nodeUtil from 'util'
import TTLCache from '@isaacs/ttlcache'
import { decode } from 'html-entities'
import type { NodeType } from 'ultrahtml'
import {
......@@ -306,12 +307,42 @@ export const createMatcher = (selector: string): Matcher => {
return newMatcherCreater()
}
const nodeProxyHandler = {
get(target, prop, receiver) {
const { _proxy } = target
if (prop in _proxy) return _proxy[ prop ]
const { [ prop ]: origValue } = target
if (!origValue) return origValue
switch (prop) {
case 'attributes':
return _proxy[ prop ] = (origValue ? Object.fromEntries(Object.entries(origValue).map(([ key, value ]) => {
return [ key, decode(value) ]
})) : origValue)
return newValue
case 'parent':
return _proxy[ prop ] = proxyNode(origValue)
case 'children':
return _proxy[ prop ] = origValue.map((child) => proxyNode(child))
default:
return origValue
}
}
}
const proxyNode = (node) => {
const { _proxy: { proxy } = {} } = node
if (proxy) return proxy
const newProxy = new Proxy(node, nodeProxyHandler)
node._proxy = { proxy: newProxy }
return newProxy
}
export const parseHtml = (html: string): NodeType => {
const cached = parsedHtmlCache.get(html)
if (cached) return cached
if (cached) return proxyNode(cached)
const doc = ultraParse(html)
parsedHtmlCache.set(html, doc)
return doc
return proxyNode(doc)
}
export const findNode = (doc: NodeType, selector: string): NodeType => {
......
......@@ -8,6 +8,7 @@
"dependencies": {
"@isaacs/ttlcache": "^1.4.1",
"@reduxjs/toolkit": "^2.2.5",
"html-entities": "^2.5.2",
"parsel-js": "^1.1.2",
"ultrahtml": "^1.5.3"
},
......@@ -3200,6 +3201,21 @@
"url": "https://opencollective.com/unified"
}
},
"node_modules/html-entities": {
"version": "2.5.2",
"resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.5.2.tgz",
"integrity": "sha512-K//PSRMQk4FZ78Kyau+mZurHn3FH0Vwr+H36eE0rPbeYkRRi9YxceYPhuN60UwWorxyKHhqoAJl2OFKa4BVtaA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/mdevils"
},
{
"type": "patreon",
"url": "https://patreon.com/mdevils"
}
]
},
"node_modules/html-escaper": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz",
......
......@@ -12,6 +12,7 @@
"dependencies": {
"@isaacs/ttlcache": "^1.4.1",
"@reduxjs/toolkit": "^2.2.5",
"html-entities": "^2.5.2",
"parsel-js": "^1.1.2",
"ultrahtml": "^1.5.3"
},
......