// Is() checks the current matched set of elements against a selector and // returns true if at least one of these elements matches. func (this *Selection) Is(selector string) bool { if len(this.Nodes) > 0 { // The selector must be done on the document if it has positional criteria // TODO : Not sure it is required, as Cascadia's selector checks within the parent of the // node when there is such a positionaly selector... In jQuery, this is for the // non-css selectors (Sizzle-implemented selectors, an extension of CSS) /*if ok, e := regexp.MatchString(rxNeedsContext, selector); ok { sel := this.document.Root.Find(selector) for _, n := range this.Nodes { if sel.IndexOfNode(n) > -1 { return true } } } else if e != nil { panic(e.Error()) } else {*/ // Attempt a match with the selector cs := cascadia.MustCompile(selector) if len(this.Nodes) == 1 { return cs.Match(this.Nodes[0]) } else { return len(cs.Filter(this.Nodes)) > 0 } //} } return false }
// Internal implementation of Find that return raw nodes. func findWithSelector(nodes []*html.Node, selector string) []*html.Node { // Compile the selector once sel := cascadia.MustCompile(selector) // Map nodes to find the matches within the children of each node return mapNodes(nodes, func(i int, n *html.Node) (result []*html.Node) { // Go down one level, becausejQuery's Find() selects only within descendants for c := n.FirstChild; c != nil; c = c.NextSibling { if c.Type == html.ElementNode { result = append(result, sel.MatchAll(c)...) } } return }) }
// Filter based on a selector string, and the indicator to keep (Filter) or // to get rid of (Not) the matching elements. func winnow(sel *Selection, selector string, keep bool) []*html.Node { cs := cascadia.MustCompile(selector) // Optimize if keep is requested if keep { return cs.Filter(sel.Nodes) } else { // Use grep return grep(sel, func(i int, s *Selection) bool { return !cs.Match(s.Get(0)) }) } return nil }