MCPcopy Create free account
hub / github.com/andybalholm/cascadia / parseAttributeSelector

Method parseAttributeSelector

parser.go:353–439  ·  view source on GitHub ↗

parseAttributeSelector parses a selector that matches by attribute value.

()

Source from the content-addressed store, hash-verified

351
352// parseAttributeSelector parses a selector that matches by attribute value.
353func (p *parser) parseAttributeSelector() (attrSelector, error) {
354 if p.i >= len(p.s) {
355 return attrSelector{}, fmt.Errorf("expected attribute selector ([attribute]), found EOF instead")
356 }
357 if p.s[p.i] != '[' {
358 return attrSelector{}, fmt.Errorf("expected attribute selector ([attribute]), found '%c' instead", p.s[p.i])
359 }
360
361 p.i++
362 p.skipWhitespace()
363 key, err := p.parseIdentifier()
364 if err != nil {
365 return attrSelector{}, err
366 }
367 key = toLowerASCII(key)
368
369 p.skipWhitespace()
370 if p.i >= len(p.s) {
371 return attrSelector{}, errors.New("unexpected EOF in attribute selector")
372 }
373
374 if p.s[p.i] == ']' {
375 p.i++
376 return attrSelector{key: key, operation: ""}, nil
377 }
378
379 if p.i+2 >= len(p.s) {
380 return attrSelector{}, errors.New("unexpected EOF in attribute selector")
381 }
382
383 op := p.s[p.i : p.i+2]
384 if op[0] == '=' {
385 op = "="
386 } else if op[1] != '=' {
387 return attrSelector{}, fmt.Errorf(`expected equality operator, found "%s" instead`, op)
388 }
389 p.i += len(op)
390
391 p.skipWhitespace()
392 if p.i >= len(p.s) {
393 return attrSelector{}, errors.New("unexpected EOF in attribute selector")
394 }
395 var val string
396 var rx *regexp.Regexp
397 if op == "#=" {
398 rx, err = p.parseRegex()
399 } else {
400 switch p.s[p.i] {
401 case '\'', '"':
402 val, err = p.parseString()
403 default:
404 val, err = p.parseIdentifier()
405 }
406 }
407 if err != nil {
408 return attrSelector{}, err
409 }
410

Callers 1

Calls 5

skipWhitespaceMethod · 0.95
parseIdentifierMethod · 0.95
parseRegexMethod · 0.95
parseStringMethod · 0.95
toLowerASCIIFunction · 0.85

Tested by

no test coverage detected