1
0
mirror of https://github.com/ericchiang/pup synced 2024-11-24 08:58:08 +00:00

class selector fixed

This commit is contained in:
ericchiang 2014-09-14 19:00:31 -04:00
parent 1aa3770b59
commit 3e26ed725f
2 changed files with 16 additions and 6 deletions

View File

@ -11,7 +11,7 @@ import (
"strings" "strings"
) )
const VERSION string = "0.1.0" const VERSION string = "0.1.1"
var ( var (
// Flags // Flags

View File

@ -42,7 +42,7 @@ func parseAttrField(command string) (attrKey string, matcher *regexp.Regexp,
attrKeyLen := len(attrKey) attrKeyLen := len(attrKey)
switch attrKey[attrKeyLen-1] { switch attrKey[attrKeyLen-1] {
case '~': case '~':
matcherString = fmt.Sprintf(`[^\s]%s[$\s]`, attrVal) matcherString = fmt.Sprintf(`\b%s\b`, attrVal)
case '$': case '$':
matcherString = fmt.Sprintf("%s$", attrVal) matcherString = fmt.Sprintf("%s$", attrVal)
case '^': case '^':
@ -67,18 +67,27 @@ func (s *Selector) setFieldValue(f selectorField, v string) error {
if v == "" { if v == "" {
return nil return nil
} }
r, err := regexp.Compile(fmt.Sprintf("^%s$", v))
if err != nil {
return err
}
switch f { switch f {
case ClassField: case ClassField:
r, err := regexp.Compile(fmt.Sprintf(`\b%s\b`, v))
if err != nil {
return err
}
s.Attrs["class"] = r s.Attrs["class"] = r
case IDField: case IDField:
r, err := regexp.Compile(fmt.Sprintf("^%s$", v))
if err != nil {
return err
}
s.Attrs["id"] = r s.Attrs["id"] = r
case NameField: case NameField:
r, err := regexp.Compile(fmt.Sprintf("^%s$", v))
if err != nil {
return err
}
s.Name = r s.Name = r
case AttrField: case AttrField:
// Attribute fields are a little more complicated
keystring, matcher, err := parseAttrField(v) keystring, matcher, err := parseAttrField(v)
if err != nil { if err != nil {
return err return err
@ -94,6 +103,7 @@ func NewSelector(s string) (*Selector, error) {
selector := &Selector{nil, attrs} selector := &Selector{nil, attrs}
nextField := NameField nextField := NameField
start := 0 start := 0
// Parse the selector character by character
for i, c := range s { for i, c := range s {
switch c { switch c {
case '.': case '.':