Permalink
Browse files

make regexp var package-level, compiled once. fixes #11

  • Loading branch information...
mna committed May 7, 2013
1 parent 485b1f5 commit 27b24066a6d964e835a6691a3a1647a01bcee3ab
Showing with 3 additions and 3 deletions.
  1. +3 −3 query.go
View
@@ -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
}

0 comments on commit 27b2406

Please sign in to comment.