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

Method parseAttributeSelector

parser.go:329–415  ·  view source on GitHub ↗

parseAttributeSelector parses a selector that matches by attribute value.

()

Source from the content-addressed store, hash-verified

327
328// parseAttributeSelector parses a selector that matches by attribute value.
329func (p *parser) parseAttributeSelector() (Selector, error) {
330 if p.i >= len(p.s) {
331 return nil, fmt.Errorf("expected attribute selector ([attribute]), found EOF instead")
332 }
333 if p.s[p.i] != '[' {
334 return nil, fmt.Errorf("expected attribute selector ([attribute]), found '%c' instead", p.s[p.i])
335 }
336
337 p.i++
338 p.skipWhitespace()
339 key, err := p.parseIdentifier()
340 if err != nil {
341 return nil, err
342 }
343
344 p.skipWhitespace()
345 if p.i >= len(p.s) {
346 return nil, errors.New("unexpected EOF in attribute selector")
347 }
348
349 if p.s[p.i] == ']' {
350 p.i++
351 return attributeExistsSelector(key), nil
352 }
353
354 if p.i+2 >= len(p.s) {
355 return nil, errors.New("unexpected EOF in attribute selector")
356 }
357
358 op := p.s[p.i : p.i+2]
359 if op[0] == '=' {
360 op = "="
361 } else if op[1] != '=' {
362 return nil, fmt.Errorf(`expected equality operator, found "%s" instead`, op)
363 }
364 p.i += len(op)
365
366 p.skipWhitespace()
367 if p.i >= len(p.s) {
368 return nil, errors.New("unexpected EOF in attribute selector")
369 }
370 var val string
371 var rx *regexp.Regexp
372 if op == "#=" {
373 rx, err = p.parseRegex()
374 } else {
375 switch p.s[p.i] {
376 case '\'', '"':
377 val, err = p.parseString()
378 default:
379 val, err = p.parseIdentifier()
380 }
381 }
382 if err != nil {
383 return nil, err
384 }
385
386 p.skipWhitespace()

Callers 1

Calls 13

skipWhitespaceMethod · 0.95
parseIdentifierMethod · 0.95
parseRegexMethod · 0.95
parseStringMethod · 0.95
attributeExistsSelectorFunction · 0.85
attributeEqualsSelectorFunction · 0.85
attributePrefixSelectorFunction · 0.85
attributeSuffixSelectorFunction · 0.85

Tested by

no test coverage detected