mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
make regexp var package-level, compiled once. fixes #11
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var rxClassTrim = regexp.MustCompile("[\t\r\n]")
|
||||
|
||||
// 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 {
|
||||
@@ -44,14 +46,12 @@ func (this *Selection) IsNodes(nodes ...*html.Node) bool {
|
||||
// HasClass() determines whether any of the matched elements are assigned the
|
||||
// given class.
|
||||
func (this *Selection) HasClass(class string) bool {
|
||||
var rx = regexp.MustCompile("[\t\r\n]")
|
||||
|
||||
class = " " + class + " "
|
||||
for _, n := range this.Nodes {
|
||||
// Applies only to element nodes
|
||||
if n.Type == html.ElementNode {
|
||||
if elClass, ok := getAttributeValue("class", n); ok {
|
||||
elClass = rx.ReplaceAllString(" "+elClass+" ", " ")
|
||||
elClass = rxClassTrim.ReplaceAllString(" "+elClass+" ", " ")
|
||||
if strings.Index(elClass, class) > -1 {
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user