Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Parser.php

function Parser::parseAttributeNode

1 call to Parser::parseAttributeNode()
Parser::parseSimpleSelector in vendor/symfony/css-selector/Parser/Parser.php
Parses next simple node (hash, class, pseudo, negation).

File

vendor/symfony/css-selector/Parser/Parser.php, line 321

Class

Parser
CSS selector parser.

Namespace

Symfony\Component\CssSelector\Parser

Code

private function parseAttributeNode(Node\NodeInterface $selector, TokenStream $stream) : Node\AttributeNode {
    $stream->skipWhitespace();
    $attribute = $stream->getNextIdentifierOrStar();
    if (null === $attribute && !$stream->getPeek()
        ->isDelimiter([
        '|',
    ])) {
        throw SyntaxErrorException::unexpectedToken('"|"', $stream->getPeek());
    }
    if ($stream->getPeek()
        ->isDelimiter([
        '|',
    ])) {
        $stream->getNext();
        if ($stream->getPeek()
            ->isDelimiter([
            '=',
        ])) {
            $namespace = null;
            $stream->getNext();
            $operator = '|=';
        }
        else {
            $namespace = $attribute;
            $attribute = $stream->getNextIdentifier();
            $operator = null;
        }
    }
    else {
        $namespace = $operator = null;
    }
    if (null === $operator) {
        $stream->skipWhitespace();
        $next = $stream->getNext();
        if ($next->isDelimiter([
            ']',
        ])) {
            return new Node\AttributeNode($selector, $namespace, $attribute, 'exists', null);
        }
        elseif ($next->isDelimiter([
            '=',
        ])) {
            $operator = '=';
        }
        elseif ($next->isDelimiter([
            '^',
            '$',
            '*',
            '~',
            '|',
            '!',
        ]) && $stream->getPeek()
            ->isDelimiter([
            '=',
        ])) {
            $operator = $next->getValue() . '=';
            $stream->getNext();
        }
        else {
            throw SyntaxErrorException::unexpectedToken('operator', $next);
        }
    }
    $stream->skipWhitespace();
    $value = $stream->getNext();
    if ($value->isNumber()) {
        // if the value is a number, it's casted into a string
        $value = new Token(Token::TYPE_STRING, (string) $value->getValue(), $value->getPosition());
    }
    if (!($value->isIdentifier() || $value->isString())) {
        throw SyntaxErrorException::unexpectedToken('string or identifier', $value);
    }
    $stream->skipWhitespace();
    $next = $stream->getNext();
    if (!$next->isDelimiter([
        ']',
    ])) {
        throw SyntaxErrorException::unexpectedToken('"]"', $next);
    }
    return new Node\AttributeNode($selector, $namespace, $attribute, $operator, $value->getValue());
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal