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
124aa3c4
authored
2024-11-07 18:11:08 -0600
by
Adam Heath
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Add a 'contains' pseudo-class, used to match against text children of a
node.
1 parent
ea13aa94
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
0 deletions
lib/html.ts
lib/html.ts
View file @
124aa3c
...
...
@@ -96,6 +96,14 @@ const compileMatcher = (ast: AST, selector: string): MatcherProducer => {
}
}
const
getNodeText
=
(
node
)
=>
{
if
(
node
.
type
===
TEXT_NODE
)
return
node
.
value
if
(
node
.
type
!==
ELEMENT_NODE
)
return
''
const
{
children
}
=
node
if
(
children
.
length
===
0
)
return
''
return
children
.
map
(
getNodeText
).
join
(
''
)
}
const
getAttrValueMatch
=
(
value
:
string
,
operator
:
string
=
'='
,
caseSensitive
:
boolean
):
AttrValueMatch
=>
{
if
(
value
===
undefined
)
return
(
attrValue
)
=>
attrValue
!==
undefined
if
(
value
[
0
]
===
'"'
||
value
[
0
]
===
'\''
)
value
=
value
.
substring
(
1
,
value
.
length
-
1
)
...
...
@@ -267,6 +275,13 @@ const compileMatcher = (ast: AST, selector: string): MatcherProducer => {
return
nthChildMatcher
(
context
,
node
,
parent
,
pos
+
1
,
debug
||
parent
?.
name
===
'body'
)
}
}
case
'contains'
:
{
const
contentValueMatch
=
getAttrValueMatch
(
ast
.
argument
,
'*='
,
false
)
return
(
context
,
node
,
parent
,
i
,
debug
)
=>
{
const
nodeText
=
getNodeText
(
node
)
return
contentValueMatch
(
nodeText
)
}
}
default
:
console
.
error
(
'pseudo-class'
,
nodeUtil
.
inspect
({
selector
,
ast
},
{
depth
:
null
,
colors
:
true
}))
throw
new
Error
(
`Unknown pseudo-class:
${
ast
.
name
}
`
)
...
...
Please
register
or
sign in
to post a comment