Skip to content

Commit 15a30b4

Browse files
npiganeaubeevik
authored andcommitted
Added filterText type
1 parent 49a60cf commit 15a30b4

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

path.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ only the following limited syntax is supported:
2626
[tag] Selects all elements with a child element named tag
2727
[tag='val'] Selects all elements with a child element named tag
2828
and text matching val
29-
[text()] Selects all elements with non empty text
29+
[text()] Selects all elements with non-empty text
3030
[text()='val'] Selects all elements whose text matches val
3131
3232
Examples:
@@ -283,8 +283,8 @@ func (c *compiler) parseFilter(path string) filter {
283283
switch {
284284
case path[0] == '@':
285285
return newFilterAttr(path[1:])
286-
case strings.HasPrefix(path, "text()"):
287-
return newFilterTextAll()
286+
case path == "text()":
287+
return newFilterText()
288288
case isInteger(path):
289289
pos, _ := strconv.Atoi(path)
290290
switch {
@@ -431,29 +431,34 @@ func (f *filterAttrVal) apply(p *pather) {
431431
p.candidates, p.scratch = p.scratch, p.candidates[0:0]
432432
}
433433

434+
// filterText filters the candidate list for elements having text.
435+
type filterText struct{}
436+
437+
func newFilterText() *filterText {
438+
return &filterText{}
439+
}
440+
441+
func (f *filterText) apply(p *pather) {
442+
for _, c := range p.candidates {
443+
if c.Text() != "" {
444+
p.scratch = append(p.scratch, c)
445+
}
446+
}
447+
p.candidates, p.scratch = p.scratch, p.candidates[0:0]
448+
}
449+
434450
// filterTextVal filters the candidate list for elements having
435451
// text equal to the specified value.
436452
type filterTextVal struct {
437453
val string
438-
all bool
439454
}
440455

441456
func newFilterTextVal(value string) *filterTextVal {
442-
return &filterTextVal{val: value}
443-
}
444-
445-
func newFilterTextAll() *filterTextVal {
446-
return &filterTextVal{all: true}
457+
return &filterTextVal{value}
447458
}
448459

449460
func (f *filterTextVal) apply(p *pather) {
450461
for _, c := range p.candidates {
451-
if f.all {
452-
if c.Text() != "" {
453-
p.scratch = append(p.scratch, c)
454-
}
455-
continue
456-
}
457462
if c.Text() == f.val {
458463
p.scratch = append(p.scratch, c)
459464
}

0 commit comments

Comments
 (0)