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
82b35ef4
authored
2024-11-22 10:25:51 -0600
by
Adam Heath
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Allow for finding multiple matching nodes.
1 parent
3312ebad
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
13 deletions
lib/html.ts
lib/html.ts
View file @
82b35ef
...
...
@@ -396,24 +396,29 @@ export const parseHtml = (html: string, options): NodeType => {
return
proxyNode
(
cached
,
options
)
}
export
const
findNode
=
(
doc
:
NodeType
,
selector
:
string
):
NodeType
=>
{
export
const
findNode
=
(
doc
:
NodeType
,
selector
:
string
,
options
=
{
single
:
true
}
):
NodeType
=>
{
if
(
!
selector
)
return
doc
let
docCache
=
findNodeCache
.
get
(
doc
)
if
(
!
docCache
)
{
docCache
=
new
TTLCache
({
ttl
:
10
*
60
})
findNodeCache
.
set
(
doc
,
docCache
)
}
const
found
=
docCache
.
get
(
selector
)
if
(
found
!==
undefined
)
return
found
[
0
]
//console.log('cache miss', {selector})
const
matcher
=
createMatcher
(
selector
)
try
{
walkSync
(
doc
,
(
node
,
parent
,
index
)
=>
{
if
(
matcher
(
node
,
parent
,
index
))
throw
node
})
}
catch
(
e
)
{
if
(
e
instanceof
Error
)
throw
e
docCache
.
set
(
selector
,
[
e
])
return
e
let
found
=
docCache
.
get
(
selector
)
if
(
found
===
undefined
)
{
const
matcher
=
createMatcher
(
selector
)
found
=
[]
try
{
walkSync
(
doc
,
(
node
,
parent
,
index
)
=>
{
if
(
node
&&
node
.
type
!==
ELEMENT_NODE
)
return
if
(
matcher
(
node
,
parent
,
index
))
{
found
.
push
(
node
)
if
(
options
.
single
)
throw
node
}
})
}
catch
(
e
)
{
if
(
e
instanceof
Error
)
throw
e
}
docCache
.
set
(
selector
,
found
)
}
return
options
.
single
?
found
[
0
]
:
found
}
...
...
Please
register
or
sign in
to post a comment