github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goquery/query.go (about) 1 package goquery 2 3 import ( 4 "code.google.com/p/cascadia" 5 "code.google.com/p/go.net/html" 6 "regexp" 7 "strings" 8 ) 9 10 var rxClassTrim = regexp.MustCompile("[\t\r\n]") 11 12 // Is() checks the current matched set of elements against a selector and 13 // returns true if at least one of these elements matches. 14 func (this *Selection) Is(selector string) bool { 15 if len(this.Nodes) > 0 { 16 // Attempt a match with the selector 17 cs := cascadia.MustCompile(selector) 18 if len(this.Nodes) == 1 { 19 return cs.Match(this.Nodes[0]) 20 } else { 21 return len(cs.Filter(this.Nodes)) > 0 22 } 23 } 24 25 return false 26 } 27 28 // IsFunction() checks the current matched set of elements against a predicate and 29 // returns true if at least one of these elements matches. 30 func (this *Selection) IsFunction(f func(int, *Selection) bool) bool { 31 return this.FilterFunction(f).Length() > 0 32 } 33 34 // IsSelection() checks the current matched set of elements against a Selection object 35 // and returns true if at least one of these elements matches. 36 func (this *Selection) IsSelection(s *Selection) bool { 37 return this.FilterSelection(s).Length() > 0 38 } 39 40 // IsNodes() checks the current matched set of elements against the specified nodes 41 // and returns true if at least one of these elements matches. 42 func (this *Selection) IsNodes(nodes ...*html.Node) bool { 43 return this.FilterNodes(nodes...).Length() > 0 44 } 45 46 // HasClass() determines whether any of the matched elements are assigned the 47 // given class. 48 func (this *Selection) HasClass(class string) bool { 49 class = " " + class + " " 50 for _, n := range this.Nodes { 51 // Applies only to element nodes 52 if n.Type == html.ElementNode { 53 if elClass, ok := getAttributeValue("class", n); ok { 54 elClass = rxClassTrim.ReplaceAllString(" "+elClass+" ", " ") 55 if strings.Index(elClass, class) > -1 { 56 return true 57 } 58 } 59 } 60 } 61 return false 62 } 63 64 // Contains() returns true if the specified Node is within, 65 // at any depth, one of the nodes in the Selection object. 66 // It is NOT inclusive, to behave like jQuery's implementation, and 67 // unlike Javascript's .contains(), so if the contained 68 // node is itself in the selection, it returns false. 69 func (this *Selection) Contains(n *html.Node) bool { 70 return sliceContains(this.Nodes, n) 71 }