aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Piganeau <nicolas.piganeau@ndp-systemes.fr>2017-08-08 10:22:18 +0200
committerBrett Vickers <beevik@users.noreply.github.com>2017-08-09 16:35:59 -0700
commit15a30b44cfd6c5a16a7ddfe271bf146aaf2d3195 (patch)
tree9f15c3d272581b02ac7ae5d3511aff67d4b959bb
parent49a60cf39919cd8765e8c34e366dcbb914c8499e (diff)
downloadgo-etree-15a30b44cfd6c5a16a7ddfe271bf146aaf2d3195.tar.gz
Added filterText type
-rw-r--r--path.go35
1 files changed, 20 insertions, 15 deletions
diff --git a/path.go b/path.go
index a2cefa2..9cf245e 100644
--- a/path.go
+++ b/path.go
@@ -26,7 +26,7 @@ only the following limited syntax is supported:
[tag] Selects all elements with a child element named tag
[tag='val'] Selects all elements with a child element named tag
and text matching val
- [text()] Selects all elements with non empty text
+ [text()] Selects all elements with non-empty text
[text()='val'] Selects all elements whose text matches val
Examples:
@@ -283,8 +283,8 @@ func (c *compiler) parseFilter(path string) filter {
switch {
case path[0] == '@':
return newFilterAttr(path[1:])
- case strings.HasPrefix(path, "text()"):
- return newFilterTextAll()
+ case path == "text()":
+ return newFilterText()
case isInteger(path):
pos, _ := strconv.Atoi(path)
switch {
@@ -431,29 +431,34 @@ func (f *filterAttrVal) apply(p *pather) {
p.candidates, p.scratch = p.scratch, p.candidates[0:0]
}
+// filterText filters the candidate list for elements having text.
+type filterText struct{}
+
+func newFilterText() *filterText {
+ return &filterText{}
+}
+
+func (f *filterText) apply(p *pather) {
+ for _, c := range p.candidates {
+ if c.Text() != "" {
+ p.scratch = append(p.scratch, c)
+ }
+ }
+ p.candidates, p.scratch = p.scratch, p.candidates[0:0]
+}
+
// filterTextVal filters the candidate list for elements having
// text equal to the specified value.
type filterTextVal struct {
val string
- all bool
}
func newFilterTextVal(value string) *filterTextVal {
- return &filterTextVal{val: value}
-}
-
-func newFilterTextAll() *filterTextVal {
- return &filterTextVal{all: true}
+ return &filterTextVal{value}
}
func (f *filterTextVal) apply(p *pather) {
for _, c := range p.candidates {
- if f.all {
- if c.Text() != "" {
- p.scratch = append(p.scratch, c)
- }
- continue
- }
if c.Text() == f.val {
p.scratch = append(p.scratch, c)
}