From 15a30b44cfd6c5a16a7ddfe271bf146aaf2d3195 Mon Sep 17 00:00:00 2001 From: Nicolas Piganeau Date: Tue, 8 Aug 2017 10:22:18 +0200 Subject: [PATCH] Added filterText type --- path.go | 35 ++++++++++++++++++++--------------- 1 file 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) }