mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2026-09-25 03:31:08 +00:00
Merge pull request #560 from jvoisin/if
perf: pre-check raw class attribute in HasClass
This commit is contained in:
@@ -137,12 +137,16 @@ func (s *Selection) AddClass(class ...string) *Selection {
|
||||
// HasClass determines whether any of the matched elements are assigned the
|
||||
// given class.
|
||||
func (s *Selection) HasClass(class string) bool {
|
||||
rawClass := class
|
||||
class = " " + class + " "
|
||||
for _, n := range s.Nodes {
|
||||
if n.Type != html.ElementNode {
|
||||
continue
|
||||
}
|
||||
if attr := getAttributePtr("class", n); attr != nil {
|
||||
if !strings.Contains(attr.Val, rawClass) {
|
||||
continue
|
||||
}
|
||||
val := classTrimReplacer.Replace(attr.Val)
|
||||
if strings.Contains(" "+val+" ", class) {
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user