Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
brainfood
/
astro-wt
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
eaa705ab
authored
2024-07-12 14:48:10 -0500
by
Adam Heath
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Add support for attribute decoding, via a proxy(this should really be
fixed upstream).
1 parent
9b920fd6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
2 deletions
lib/html.ts
package-lock.json
package.json
lib/html.ts
View file @
eaa705a
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
=>
{
...
...
package-lock.json
View file @
eaa705a
...
...
@@ -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"
,
...
...
package.json
View file @
eaa705a
...
...
@@ -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"
},
...
...
Please
register
or
sign in
to post a comment