2017-05-07 10:40:31 -04:00
|
|
|
// Package cascadia is an implementation of CSS selectors.
|
|
|
|
package cascadia
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// a parser for CSS selectors
|
|
|
|
type parser struct {
|
|
|
|
s string // the source text
|
|
|
|
i int // the current position
|
2021-06-10 10:44:25 -04:00
|
|
|
|
|
|
|
// if `false`, parsing a pseudo-element
|
|
|
|
// returns an error.
|
|
|
|
acceptPseudoElements bool
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseEscape parses a backslash escape.
|
|
|
|
func (p *parser) parseEscape() (result string, err error) {
|
|
|
|
if len(p.s) < p.i+2 || p.s[p.i] != '\\' {
|
|
|
|
return "", errors.New("invalid escape sequence")
|
|
|
|
}
|
|
|
|
|
|
|
|
start := p.i + 1
|
|
|
|
c := p.s[start]
|
|
|
|
switch {
|
|
|
|
case c == '\r' || c == '\n' || c == '\f':
|
|
|
|
return "", errors.New("escaped line ending outside string")
|
|
|
|
case hexDigit(c):
|
|
|
|
// unicode escape (hex)
|
|
|
|
var i int
|
2021-06-10 10:44:25 -04:00
|
|
|
for i = start; i < start+6 && i < len(p.s) && hexDigit(p.s[i]); i++ {
|
2017-05-07 10:40:31 -04:00
|
|
|
// empty
|
|
|
|
}
|
|
|
|
v, _ := strconv.ParseUint(p.s[start:i], 16, 21)
|
|
|
|
if len(p.s) > i {
|
|
|
|
switch p.s[i] {
|
|
|
|
case '\r':
|
|
|
|
i++
|
|
|
|
if len(p.s) > i && p.s[i] == '\n' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
case ' ', '\t', '\n', '\f':
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.i = i
|
|
|
|
return string(rune(v)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the literal character after the backslash.
|
|
|
|
result = p.s[start : start+1]
|
|
|
|
p.i += 2
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-07-11 17:07:52 -04:00
|
|
|
// toLowerASCII returns s with all ASCII capital letters lowercased.
|
|
|
|
func toLowerASCII(s string) string {
|
|
|
|
var b []byte
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if c := s[i]; 'A' <= c && c <= 'Z' {
|
|
|
|
if b == nil {
|
|
|
|
b = make([]byte, len(s))
|
|
|
|
copy(b, s)
|
|
|
|
}
|
|
|
|
b[i] = s[i] + ('a' - 'A')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if b == nil {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2017-05-07 10:40:31 -04:00
|
|
|
func hexDigit(c byte) bool {
|
|
|
|
return '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F'
|
|
|
|
}
|
|
|
|
|
|
|
|
// nameStart returns whether c can be the first character of an identifier
|
|
|
|
// (not counting an initial hyphen, or an escape sequence).
|
|
|
|
func nameStart(c byte) bool {
|
|
|
|
return 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_' || c > 127
|
|
|
|
}
|
|
|
|
|
|
|
|
// nameChar returns whether c can be a character within an identifier
|
|
|
|
// (not counting an escape sequence).
|
|
|
|
func nameChar(c byte) bool {
|
|
|
|
return 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_' || c > 127 ||
|
|
|
|
c == '-' || '0' <= c && c <= '9'
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseIdentifier parses an identifier.
|
|
|
|
func (p *parser) parseIdentifier() (result string, err error) {
|
|
|
|
startingDash := false
|
|
|
|
if len(p.s) > p.i && p.s[p.i] == '-' {
|
|
|
|
startingDash = true
|
|
|
|
p.i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.s) <= p.i {
|
|
|
|
return "", errors.New("expected identifier, found EOF instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c := p.s[p.i]; !(nameStart(c) || c == '\\') {
|
|
|
|
return "", fmt.Errorf("expected identifier, found %c instead", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err = p.parseName()
|
|
|
|
if startingDash && err == nil {
|
|
|
|
result = "-" + result
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseName parses a name (which is like an identifier, but doesn't have
|
|
|
|
// extra restrictions on the first character).
|
|
|
|
func (p *parser) parseName() (result string, err error) {
|
|
|
|
i := p.i
|
|
|
|
loop:
|
|
|
|
for i < len(p.s) {
|
|
|
|
c := p.s[i]
|
|
|
|
switch {
|
|
|
|
case nameChar(c):
|
|
|
|
start := i
|
|
|
|
for i < len(p.s) && nameChar(p.s[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
result += p.s[start:i]
|
|
|
|
case c == '\\':
|
|
|
|
p.i = i
|
|
|
|
val, err := p.parseEscape()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
i = p.i
|
|
|
|
result += val
|
|
|
|
default:
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == "" {
|
|
|
|
return "", errors.New("expected name, found EOF instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.i = i
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseString parses a single- or double-quoted string.
|
|
|
|
func (p *parser) parseString() (result string, err error) {
|
|
|
|
i := p.i
|
|
|
|
if len(p.s) < i+2 {
|
|
|
|
return "", errors.New("expected string, found EOF instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
quote := p.s[i]
|
|
|
|
i++
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for i < len(p.s) {
|
|
|
|
switch p.s[i] {
|
|
|
|
case '\\':
|
|
|
|
if len(p.s) > i+1 {
|
|
|
|
switch c := p.s[i+1]; c {
|
|
|
|
case '\r':
|
|
|
|
if len(p.s) > i+2 && p.s[i+2] == '\n' {
|
|
|
|
i += 3
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case '\n', '\f':
|
|
|
|
i += 2
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.i = i
|
|
|
|
val, err := p.parseEscape()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
i = p.i
|
|
|
|
result += val
|
|
|
|
case quote:
|
|
|
|
break loop
|
|
|
|
case '\r', '\n', '\f':
|
|
|
|
return "", errors.New("unexpected end of line in string")
|
|
|
|
default:
|
|
|
|
start := i
|
|
|
|
for i < len(p.s) {
|
|
|
|
if c := p.s[i]; c == quote || c == '\\' || c == '\r' || c == '\n' || c == '\f' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
result += p.s[start:i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i >= len(p.s) {
|
|
|
|
return "", errors.New("EOF in string")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume the final quote.
|
|
|
|
i++
|
|
|
|
|
|
|
|
p.i = i
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseRegex parses a regular expression; the end is defined by encountering an
|
|
|
|
// unmatched closing ')' or ']' which is not consumed
|
|
|
|
func (p *parser) parseRegex() (rx *regexp.Regexp, err error) {
|
|
|
|
i := p.i
|
|
|
|
if len(p.s) < i+2 {
|
|
|
|
return nil, errors.New("expected regular expression, found EOF instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
// number of open parens or brackets;
|
|
|
|
// when it becomes negative, finished parsing regex
|
|
|
|
open := 0
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for i < len(p.s) {
|
|
|
|
switch p.s[i] {
|
|
|
|
case '(', '[':
|
|
|
|
open++
|
|
|
|
case ')', ']':
|
|
|
|
open--
|
|
|
|
if open < 0 {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if i >= len(p.s) {
|
|
|
|
return nil, errors.New("EOF in regular expression")
|
|
|
|
}
|
|
|
|
rx, err = regexp.Compile(p.s[p.i:i])
|
|
|
|
p.i = i
|
|
|
|
return rx, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// skipWhitespace consumes whitespace characters and comments.
|
|
|
|
// It returns true if there was actually anything to skip.
|
|
|
|
func (p *parser) skipWhitespace() bool {
|
|
|
|
i := p.i
|
|
|
|
for i < len(p.s) {
|
|
|
|
switch p.s[i] {
|
|
|
|
case ' ', '\t', '\r', '\n', '\f':
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
case '/':
|
|
|
|
if strings.HasPrefix(p.s[i:], "/*") {
|
|
|
|
end := strings.Index(p.s[i+len("/*"):], "*/")
|
|
|
|
if end != -1 {
|
|
|
|
i += end + len("/**/")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > p.i {
|
|
|
|
p.i = i
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// consumeParenthesis consumes an opening parenthesis and any following
|
|
|
|
// whitespace. It returns true if there was actually a parenthesis to skip.
|
|
|
|
func (p *parser) consumeParenthesis() bool {
|
|
|
|
if p.i < len(p.s) && p.s[p.i] == '(' {
|
|
|
|
p.i++
|
|
|
|
p.skipWhitespace()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// consumeClosingParenthesis consumes a closing parenthesis and any preceding
|
|
|
|
// whitespace. It returns true if there was actually a parenthesis to skip.
|
|
|
|
func (p *parser) consumeClosingParenthesis() bool {
|
|
|
|
i := p.i
|
|
|
|
p.skipWhitespace()
|
|
|
|
if p.i < len(p.s) && p.s[p.i] == ')' {
|
|
|
|
p.i++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
p.i = i
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseTypeSelector parses a type selector (one that matches by tag name).
|
2020-07-11 17:07:52 -04:00
|
|
|
func (p *parser) parseTypeSelector() (result tagSelector, err error) {
|
2017-05-07 10:40:31 -04:00
|
|
|
tag, err := p.parseIdentifier()
|
|
|
|
if err != nil {
|
2020-07-11 17:07:52 -04:00
|
|
|
return
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
2020-07-11 17:07:52 -04:00
|
|
|
return tagSelector{tag: toLowerASCII(tag)}, nil
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseIDSelector parses a selector that matches by id attribute.
|
2020-07-11 17:07:52 -04:00
|
|
|
func (p *parser) parseIDSelector() (idSelector, error) {
|
2017-05-07 10:40:31 -04:00
|
|
|
if p.i >= len(p.s) {
|
2020-07-11 17:07:52 -04:00
|
|
|
return idSelector{}, fmt.Errorf("expected id selector (#id), found EOF instead")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
if p.s[p.i] != '#' {
|
2020-07-11 17:07:52 -04:00
|
|
|
return idSelector{}, fmt.Errorf("expected id selector (#id), found '%c' instead", p.s[p.i])
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p.i++
|
|
|
|
id, err := p.parseName()
|
|
|
|
if err != nil {
|
2020-07-11 17:07:52 -04:00
|
|
|
return idSelector{}, err
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
2020-07-11 17:07:52 -04:00
|
|
|
return idSelector{id: id}, nil
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseClassSelector parses a selector that matches by class attribute.
|
2020-07-11 17:07:52 -04:00
|
|
|
func (p *parser) parseClassSelector() (classSelector, error) {
|
2017-05-07 10:40:31 -04:00
|
|
|
if p.i >= len(p.s) {
|
2020-07-11 17:07:52 -04:00
|
|
|
return classSelector{}, fmt.Errorf("expected class selector (.class), found EOF instead")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
if p.s[p.i] != '.' {
|
2020-07-11 17:07:52 -04:00
|
|
|
return classSelector{}, fmt.Errorf("expected class selector (.class), found '%c' instead", p.s[p.i])
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p.i++
|
|
|
|
class, err := p.parseIdentifier()
|
|
|
|
if err != nil {
|
2020-07-11 17:07:52 -04:00
|
|
|
return classSelector{}, err
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
2020-07-11 17:07:52 -04:00
|
|
|
return classSelector{class: class}, nil
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseAttributeSelector parses a selector that matches by attribute value.
|
2020-07-11 17:07:52 -04:00
|
|
|
func (p *parser) parseAttributeSelector() (attrSelector, error) {
|
2017-05-07 10:40:31 -04:00
|
|
|
if p.i >= len(p.s) {
|
2020-07-11 17:07:52 -04:00
|
|
|
return attrSelector{}, fmt.Errorf("expected attribute selector ([attribute]), found EOF instead")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
if p.s[p.i] != '[' {
|
2020-07-11 17:07:52 -04:00
|
|
|
return attrSelector{}, fmt.Errorf("expected attribute selector ([attribute]), found '%c' instead", p.s[p.i])
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p.i++
|
|
|
|
p.skipWhitespace()
|
|
|
|
key, err := p.parseIdentifier()
|
|
|
|
if err != nil {
|
2020-07-11 17:07:52 -04:00
|
|
|
return attrSelector{}, err
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
2020-07-11 17:07:52 -04:00
|
|
|
key = toLowerASCII(key)
|
2017-05-07 10:40:31 -04:00
|
|
|
|
|
|
|
p.skipWhitespace()
|
|
|
|
if p.i >= len(p.s) {
|
2020-07-11 17:07:52 -04:00
|
|
|
return attrSelector{}, errors.New("unexpected EOF in attribute selector")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.s[p.i] == ']' {
|
|
|
|
p.i++
|
2020-07-11 17:07:52 -04:00
|
|
|
return attrSelector{key: key, operation: ""}, nil
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.i+2 >= len(p.s) {
|
2020-07-11 17:07:52 -04:00
|
|
|
return attrSelector{}, errors.New("unexpected EOF in attribute selector")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
op := p.s[p.i : p.i+2]
|
|
|
|
if op[0] == '=' {
|
|
|
|
op = "="
|
|
|
|
} else if op[1] != '=' {
|
2020-07-11 17:07:52 -04:00
|
|
|
return attrSelector{}, fmt.Errorf(`expected equality operator, found "%s" instead`, op)
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
p.i += len(op)
|
|
|
|
|
|
|
|
p.skipWhitespace()
|
|
|
|
if p.i >= len(p.s) {
|
2020-07-11 17:07:52 -04:00
|
|
|
return attrSelector{}, errors.New("unexpected EOF in attribute selector")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
var val string
|
|
|
|
var rx *regexp.Regexp
|
|
|
|
if op == "#=" {
|
|
|
|
rx, err = p.parseRegex()
|
|
|
|
} else {
|
|
|
|
switch p.s[p.i] {
|
|
|
|
case '\'', '"':
|
|
|
|
val, err = p.parseString()
|
|
|
|
default:
|
|
|
|
val, err = p.parseIdentifier()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-07-11 17:07:52 -04:00
|
|
|
return attrSelector{}, err
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p.skipWhitespace()
|
|
|
|
if p.i >= len(p.s) {
|
2020-07-11 17:07:52 -04:00
|
|
|
return attrSelector{}, errors.New("unexpected EOF in attribute selector")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
if p.s[p.i] != ']' {
|
2020-07-11 17:07:52 -04:00
|
|
|
return attrSelector{}, fmt.Errorf("expected ']', found '%c' instead", p.s[p.i])
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
p.i++
|
|
|
|
|
|
|
|
switch op {
|
2020-07-11 17:07:52 -04:00
|
|
|
case "=", "!=", "~=", "|=", "^=", "$=", "*=", "#=":
|
|
|
|
return attrSelector{key: key, val: val, operation: op, regexp: rx}, nil
|
|
|
|
default:
|
|
|
|
return attrSelector{}, fmt.Errorf("attribute operator %q is not supported", op)
|
|
|
|
}
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var errExpectedParenthesis = errors.New("expected '(' but didn't find it")
|
|
|
|
var errExpectedClosingParenthesis = errors.New("expected ')' but didn't find it")
|
|
|
|
var errUnmatchedParenthesis = errors.New("unmatched '('")
|
|
|
|
|
2021-06-10 10:44:25 -04:00
|
|
|
// parsePseudoclassSelector parses a pseudoclass selector like :not(p) or a pseudo-element
|
|
|
|
// For backwards compatibility, both ':' and '::' prefix are allowed for pseudo-elements.
|
|
|
|
// https://drafts.csswg.org/selectors-3/#pseudo-elements
|
|
|
|
// Returning a nil `Sel` (and a nil `error`) means we found a pseudo-element.
|
|
|
|
func (p *parser) parsePseudoclassSelector() (out Sel, pseudoElement string, err error) {
|
2017-05-07 10:40:31 -04:00
|
|
|
if p.i >= len(p.s) {
|
2021-06-10 10:44:25 -04:00
|
|
|
return nil, "", fmt.Errorf("expected pseudoclass selector (:pseudoclass), found EOF instead")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
if p.s[p.i] != ':' {
|
2021-06-10 10:44:25 -04:00
|
|
|
return nil, "", fmt.Errorf("expected attribute selector (:pseudoclass), found '%c' instead", p.s[p.i])
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p.i++
|
2021-06-10 10:44:25 -04:00
|
|
|
var mustBePseudoElement bool
|
|
|
|
if p.i >= len(p.s) {
|
|
|
|
return nil, "", fmt.Errorf("got empty pseudoclass (or pseudoelement)")
|
|
|
|
}
|
2020-07-11 17:07:52 -04:00
|
|
|
if p.s[p.i] == ':' { // we found a pseudo-element
|
2021-06-10 10:44:25 -04:00
|
|
|
mustBePseudoElement = true
|
2020-07-11 17:07:52 -04:00
|
|
|
p.i++
|
|
|
|
}
|
|
|
|
|
2017-05-07 10:40:31 -04:00
|
|
|
name, err := p.parseIdentifier()
|
|
|
|
if err != nil {
|
2020-07-11 17:07:52 -04:00
|
|
|
return
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
name = toLowerASCII(name)
|
2021-06-10 10:44:25 -04:00
|
|
|
if mustBePseudoElement && (name != "after" && name != "backdrop" && name != "before" &&
|
|
|
|
name != "cue" && name != "first-letter" && name != "first-line" && name != "grammar-error" &&
|
|
|
|
name != "marker" && name != "placeholder" && name != "selection" && name != "spelling-error") {
|
|
|
|
return out, "", fmt.Errorf("unknown pseudoelement :%s", name)
|
|
|
|
}
|
|
|
|
|
2017-05-07 10:40:31 -04:00
|
|
|
switch name {
|
|
|
|
case "not", "has", "haschild":
|
|
|
|
if !p.consumeParenthesis() {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", errExpectedParenthesis
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
sel, parseErr := p.parseSelectorGroup()
|
|
|
|
if parseErr != nil {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", parseErr
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
if !p.consumeClosingParenthesis() {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", errExpectedClosingParenthesis
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
2020-07-11 17:07:52 -04:00
|
|
|
out = relativePseudoClassSelector{name: name, match: sel}
|
2017-05-07 10:40:31 -04:00
|
|
|
|
|
|
|
case "contains", "containsown":
|
|
|
|
if !p.consumeParenthesis() {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", errExpectedParenthesis
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
if p.i == len(p.s) {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", errUnmatchedParenthesis
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
var val string
|
|
|
|
switch p.s[p.i] {
|
|
|
|
case '\'', '"':
|
|
|
|
val, err = p.parseString()
|
|
|
|
default:
|
|
|
|
val, err = p.parseIdentifier()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", err
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
val = strings.ToLower(val)
|
|
|
|
p.skipWhitespace()
|
|
|
|
if p.i >= len(p.s) {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", errors.New("unexpected EOF in pseudo selector")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
if !p.consumeClosingParenthesis() {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", errExpectedClosingParenthesis
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
2020-07-11 17:07:52 -04:00
|
|
|
out = containsPseudoClassSelector{own: name == "containsown", value: val}
|
2017-05-07 10:40:31 -04:00
|
|
|
|
|
|
|
case "matches", "matchesown":
|
|
|
|
if !p.consumeParenthesis() {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", errExpectedParenthesis
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
rx, err := p.parseRegex()
|
|
|
|
if err != nil {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", err
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
if p.i >= len(p.s) {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", errors.New("unexpected EOF in pseudo selector")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
if !p.consumeClosingParenthesis() {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", errExpectedClosingParenthesis
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
2020-07-11 17:07:52 -04:00
|
|
|
out = regexpPseudoClassSelector{own: name == "matchesown", regexp: rx}
|
2017-05-07 10:40:31 -04:00
|
|
|
|
|
|
|
case "nth-child", "nth-last-child", "nth-of-type", "nth-last-of-type":
|
|
|
|
if !p.consumeParenthesis() {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", errExpectedParenthesis
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
a, b, err := p.parseNth()
|
|
|
|
if err != nil {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", err
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
if !p.consumeClosingParenthesis() {
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", errExpectedClosingParenthesis
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
2020-07-11 17:07:52 -04:00
|
|
|
last := name == "nth-last-child" || name == "nth-last-of-type"
|
|
|
|
ofType := name == "nth-of-type" || name == "nth-last-of-type"
|
|
|
|
out = nthPseudoClassSelector{a: a, b: b, last: last, ofType: ofType}
|
2017-05-07 10:40:31 -04:00
|
|
|
|
|
|
|
case "first-child":
|
2020-07-11 17:07:52 -04:00
|
|
|
out = nthPseudoClassSelector{a: 0, b: 1, ofType: false, last: false}
|
2017-05-07 10:40:31 -04:00
|
|
|
case "last-child":
|
2020-07-11 17:07:52 -04:00
|
|
|
out = nthPseudoClassSelector{a: 0, b: 1, ofType: false, last: true}
|
2017-05-07 10:40:31 -04:00
|
|
|
case "first-of-type":
|
2020-07-11 17:07:52 -04:00
|
|
|
out = nthPseudoClassSelector{a: 0, b: 1, ofType: true, last: false}
|
2017-05-07 10:40:31 -04:00
|
|
|
case "last-of-type":
|
2020-07-11 17:07:52 -04:00
|
|
|
out = nthPseudoClassSelector{a: 0, b: 1, ofType: true, last: true}
|
2017-05-07 10:40:31 -04:00
|
|
|
case "only-child":
|
2020-07-11 17:07:52 -04:00
|
|
|
out = onlyChildPseudoClassSelector{ofType: false}
|
2017-05-07 10:40:31 -04:00
|
|
|
case "only-of-type":
|
2020-07-11 17:07:52 -04:00
|
|
|
out = onlyChildPseudoClassSelector{ofType: true}
|
2017-05-07 10:40:31 -04:00
|
|
|
case "input":
|
2020-07-11 17:07:52 -04:00
|
|
|
out = inputPseudoClassSelector{}
|
2017-05-07 10:40:31 -04:00
|
|
|
case "empty":
|
2020-07-11 17:07:52 -04:00
|
|
|
out = emptyElementPseudoClassSelector{}
|
2017-05-07 10:40:31 -04:00
|
|
|
case "root":
|
2020-07-11 17:07:52 -04:00
|
|
|
out = rootPseudoClassSelector{}
|
|
|
|
case "after", "backdrop", "before", "cue", "first-letter", "first-line", "grammar-error", "marker", "placeholder", "selection", "spelling-error":
|
2021-06-10 10:44:25 -04:00
|
|
|
return nil, name, nil
|
2020-07-11 17:07:52 -04:00
|
|
|
default:
|
2021-06-10 10:44:25 -04:00
|
|
|
return out, "", fmt.Errorf("unknown pseudoclass or pseudoelement :%s", name)
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
2020-07-11 17:07:52 -04:00
|
|
|
return
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseInteger parses a decimal integer.
|
|
|
|
func (p *parser) parseInteger() (int, error) {
|
|
|
|
i := p.i
|
|
|
|
start := i
|
|
|
|
for i < len(p.s) && '0' <= p.s[i] && p.s[i] <= '9' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i == start {
|
|
|
|
return 0, errors.New("expected integer, but didn't find it")
|
|
|
|
}
|
|
|
|
p.i = i
|
|
|
|
|
|
|
|
val, err := strconv.Atoi(p.s[start:i])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseNth parses the argument for :nth-child (normally of the form an+b).
|
|
|
|
func (p *parser) parseNth() (a, b int, err error) {
|
|
|
|
// initial state
|
|
|
|
if p.i >= len(p.s) {
|
|
|
|
goto eof
|
|
|
|
}
|
|
|
|
switch p.s[p.i] {
|
|
|
|
case '-':
|
|
|
|
p.i++
|
|
|
|
goto negativeA
|
|
|
|
case '+':
|
|
|
|
p.i++
|
|
|
|
goto positiveA
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
goto positiveA
|
|
|
|
case 'n', 'N':
|
|
|
|
a = 1
|
|
|
|
p.i++
|
|
|
|
goto readN
|
|
|
|
case 'o', 'O', 'e', 'E':
|
|
|
|
id, nameErr := p.parseName()
|
|
|
|
if nameErr != nil {
|
|
|
|
return 0, 0, nameErr
|
|
|
|
}
|
|
|
|
id = toLowerASCII(id)
|
|
|
|
if id == "odd" {
|
|
|
|
return 2, 1, nil
|
|
|
|
}
|
|
|
|
if id == "even" {
|
|
|
|
return 2, 0, nil
|
|
|
|
}
|
|
|
|
return 0, 0, fmt.Errorf("expected 'odd' or 'even', but found '%s' instead", id)
|
|
|
|
default:
|
|
|
|
goto invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
positiveA:
|
|
|
|
if p.i >= len(p.s) {
|
|
|
|
goto eof
|
|
|
|
}
|
|
|
|
switch p.s[p.i] {
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
a, err = p.parseInteger()
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
goto readA
|
|
|
|
case 'n', 'N':
|
|
|
|
a = 1
|
|
|
|
p.i++
|
|
|
|
goto readN
|
|
|
|
default:
|
|
|
|
goto invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
negativeA:
|
|
|
|
if p.i >= len(p.s) {
|
|
|
|
goto eof
|
|
|
|
}
|
|
|
|
switch p.s[p.i] {
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
a, err = p.parseInteger()
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
a = -a
|
|
|
|
goto readA
|
|
|
|
case 'n', 'N':
|
|
|
|
a = -1
|
|
|
|
p.i++
|
|
|
|
goto readN
|
|
|
|
default:
|
|
|
|
goto invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
readA:
|
|
|
|
if p.i >= len(p.s) {
|
|
|
|
goto eof
|
|
|
|
}
|
|
|
|
switch p.s[p.i] {
|
|
|
|
case 'n', 'N':
|
|
|
|
p.i++
|
|
|
|
goto readN
|
|
|
|
default:
|
|
|
|
// The number we read as a is actually b.
|
|
|
|
return 0, a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
readN:
|
|
|
|
p.skipWhitespace()
|
|
|
|
if p.i >= len(p.s) {
|
|
|
|
goto eof
|
|
|
|
}
|
|
|
|
switch p.s[p.i] {
|
|
|
|
case '+':
|
|
|
|
p.i++
|
|
|
|
p.skipWhitespace()
|
|
|
|
b, err = p.parseInteger()
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return a, b, nil
|
|
|
|
case '-':
|
|
|
|
p.i++
|
|
|
|
p.skipWhitespace()
|
|
|
|
b, err = p.parseInteger()
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return a, -b, nil
|
|
|
|
default:
|
|
|
|
return a, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
eof:
|
|
|
|
return 0, 0, errors.New("unexpected EOF while attempting to parse expression of form an+b")
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
return 0, 0, errors.New("unexpected character while attempting to parse expression of form an+b")
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseSimpleSelectorSequence parses a selector sequence that applies to
|
|
|
|
// a single element.
|
2020-07-11 17:07:52 -04:00
|
|
|
func (p *parser) parseSimpleSelectorSequence() (Sel, error) {
|
|
|
|
var selectors []Sel
|
2017-05-07 10:40:31 -04:00
|
|
|
|
|
|
|
if p.i >= len(p.s) {
|
|
|
|
return nil, errors.New("expected selector, found EOF instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch p.s[p.i] {
|
|
|
|
case '*':
|
|
|
|
// It's the universal selector. Just skip over it, since it doesn't affect the meaning.
|
|
|
|
p.i++
|
|
|
|
case '#', '.', '[', ':':
|
|
|
|
// There's no type selector. Wait to process the other till the main loop.
|
|
|
|
default:
|
|
|
|
r, err := p.parseTypeSelector()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-11 17:07:52 -04:00
|
|
|
selectors = append(selectors, r)
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
2021-06-10 10:44:25 -04:00
|
|
|
var pseudoElement string
|
2017-05-07 10:40:31 -04:00
|
|
|
loop:
|
|
|
|
for p.i < len(p.s) {
|
2020-07-11 17:07:52 -04:00
|
|
|
var (
|
2021-06-10 10:44:25 -04:00
|
|
|
ns Sel
|
|
|
|
newPseudoElement string
|
|
|
|
err error
|
2020-07-11 17:07:52 -04:00
|
|
|
)
|
2017-05-07 10:40:31 -04:00
|
|
|
switch p.s[p.i] {
|
|
|
|
case '#':
|
|
|
|
ns, err = p.parseIDSelector()
|
|
|
|
case '.':
|
|
|
|
ns, err = p.parseClassSelector()
|
|
|
|
case '[':
|
|
|
|
ns, err = p.parseAttributeSelector()
|
|
|
|
case ':':
|
2021-06-10 10:44:25 -04:00
|
|
|
ns, newPseudoElement, err = p.parsePseudoclassSelector()
|
2017-05-07 10:40:31 -04:00
|
|
|
default:
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-10 10:44:25 -04:00
|
|
|
// From https://drafts.csswg.org/selectors-3/#pseudo-elements :
|
|
|
|
// "Only one pseudo-element may appear per selector, and if present
|
|
|
|
// it must appear after the sequence of simple selectors that
|
|
|
|
// represents the subjects of the selector.""
|
|
|
|
if ns == nil { // we found a pseudo-element
|
|
|
|
if pseudoElement != "" {
|
|
|
|
return nil, fmt.Errorf("only one pseudo-element is accepted per selector, got %s and %s", pseudoElement, newPseudoElement)
|
|
|
|
}
|
|
|
|
if !p.acceptPseudoElements {
|
|
|
|
return nil, fmt.Errorf("pseudo-element %s found, but pseudo-elements support is disabled", newPseudoElement)
|
|
|
|
}
|
|
|
|
pseudoElement = newPseudoElement
|
|
|
|
} else {
|
|
|
|
if pseudoElement != "" {
|
|
|
|
return nil, fmt.Errorf("pseudo-element %s must be at the end of selector", pseudoElement)
|
|
|
|
}
|
|
|
|
selectors = append(selectors, ns)
|
|
|
|
}
|
2017-05-07 10:40:31 -04:00
|
|
|
|
|
|
|
}
|
2021-06-10 10:44:25 -04:00
|
|
|
if len(selectors) == 1 && pseudoElement == "" { // no need wrap the selectors in compoundSelector
|
2020-07-11 17:07:52 -04:00
|
|
|
return selectors[0], nil
|
|
|
|
}
|
2021-06-10 10:44:25 -04:00
|
|
|
return compoundSelector{selectors: selectors, pseudoElement: pseudoElement}, nil
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseSelector parses a selector that may include combinators.
|
2020-07-11 17:07:52 -04:00
|
|
|
func (p *parser) parseSelector() (Sel, error) {
|
2017-05-07 10:40:31 -04:00
|
|
|
p.skipWhitespace()
|
2020-07-11 17:07:52 -04:00
|
|
|
result, err := p.parseSimpleSelectorSequence()
|
2017-05-07 10:40:31 -04:00
|
|
|
if err != nil {
|
2020-07-11 17:07:52 -04:00
|
|
|
return nil, err
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2020-07-11 17:07:52 -04:00
|
|
|
var (
|
|
|
|
combinator byte
|
|
|
|
c Sel
|
|
|
|
)
|
2017-05-07 10:40:31 -04:00
|
|
|
if p.skipWhitespace() {
|
|
|
|
combinator = ' '
|
|
|
|
}
|
|
|
|
if p.i >= len(p.s) {
|
2020-07-11 17:07:52 -04:00
|
|
|
return result, nil
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch p.s[p.i] {
|
|
|
|
case '+', '>', '~':
|
|
|
|
combinator = p.s[p.i]
|
|
|
|
p.i++
|
|
|
|
p.skipWhitespace()
|
|
|
|
case ',', ')':
|
|
|
|
// These characters can't begin a selector, but they can legally occur after one.
|
2020-07-11 17:07:52 -04:00
|
|
|
return result, nil
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if combinator == 0 {
|
2020-07-11 17:07:52 -04:00
|
|
|
return result, nil
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
2020-07-11 17:07:52 -04:00
|
|
|
c, err = p.parseSimpleSelectorSequence()
|
2017-05-07 10:40:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-11 17:07:52 -04:00
|
|
|
result = combinedSelector{first: result, combinator: combinator, second: c}
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseSelectorGroup parses a group of selectors, separated by commas.
|
2020-07-11 17:07:52 -04:00
|
|
|
func (p *parser) parseSelectorGroup() (SelectorGroup, error) {
|
|
|
|
current, err := p.parseSelector()
|
2017-05-07 10:40:31 -04:00
|
|
|
if err != nil {
|
2020-07-11 17:07:52 -04:00
|
|
|
return nil, err
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
2020-07-11 17:07:52 -04:00
|
|
|
result := SelectorGroup{current}
|
2017-05-07 10:40:31 -04:00
|
|
|
|
|
|
|
for p.i < len(p.s) {
|
|
|
|
if p.s[p.i] != ',' {
|
2020-07-11 17:07:52 -04:00
|
|
|
break
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
p.i++
|
|
|
|
c, err := p.parseSelector()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-11 17:07:52 -04:00
|
|
|
result = append(result, c)
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
2020-07-11 17:07:52 -04:00
|
|
|
return result, nil
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|