Beispiel #1
0
 /**
  * Returns nex identifier or star delimiter token.
  *
  * @throws SyntaxErrorException If next token is not an identifier or a star delimiter
  *
  * @return null|string The identifier token value or null if star found
  */
 public function getNextIdentifierOrStar()
 {
     $next = $this->getNext();
     if ($next->isIdentifier()) {
         return $next->getValue();
     }
     if ($next->isDelimiter(array('*'))) {
         return;
     }
     throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next);
 }
 /**
  * Parses next attribute node.
  *
  * @param Node\NodeInterface $selector
  * @param TokenStream        $stream
  *
  * @throws SyntaxErrorException
  *
  * @return Node\AttributeNode
  */
 private function parseAttributeNode(Node\NodeInterface $selector, TokenStream $stream)
 {
     $stream->skipWhitespace();
     $attribute = $stream->getNextIdentifierOrStar();
     if (null === $attribute && !$stream->getPeek()->isDelimiter(array('|'))) {
         throw SyntaxErrorException::unexpectedToken('"|"', $stream->getPeek());
     }
     if ($stream->getPeek()->isDelimiter(array('|'))) {
         $stream->getNext();
         if ($stream->getPeek()->isDelimiter(array('='))) {
             $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(array(']'))) {
             return new Node\AttributeNode($selector, $namespace, $attribute, 'exists', null);
         } elseif ($next->isDelimiter(array('='))) {
             $operator = '=';
         } elseif ($next->isDelimiter(array('^', '$', '*', '~', '|', '!')) && $stream->getPeek()->isDelimiter(array('='))) {
             $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(array(']'))) {
         throw SyntaxErrorException::unexpectedToken('"]"', $next);
     }
     return new Node\AttributeNode($selector, $namespace, $attribute, $operator, $value->getValue());
 }
Beispiel #3
0
 public function getParserExceptionTestData()
 {
     return array(array('attributes(href)/html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()), array('attributes(href)', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()), array('html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '/', 4))->getMessage()), array(' ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 1))->getMessage()), array('div, ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 5))->getMessage()), array(' , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 1))->getMessage()), array('p, , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 3))->getMessage()), array('div > ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 6))->getMessage()), array('  > div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '>', 2))->getMessage()), array('foo|#bar', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_HASH, 'bar', 4))->getMessage()), array('#.foo', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '#', 0))->getMessage()), array('.#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()), array(':#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()), array('[*]', SyntaxErrorException::unexpectedToken('"|"', new Token(Token::TYPE_DELIMITER, ']', 2))->getMessage()), array('[foo|]', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_DELIMITER, ']', 5))->getMessage()), array('[#]', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_DELIMITER, '#', 1))->getMessage()), array('[foo=#]', SyntaxErrorException::unexpectedToken('string or identifier', new Token(Token::TYPE_DELIMITER, '#', 5))->getMessage()), array(':nth-child()', SyntaxErrorException::unexpectedToken('at least one argument', new Token(Token::TYPE_DELIMITER, ')', 11))->getMessage()), array('[href]a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_IDENTIFIER, 'a', 6))->getMessage()), array('[rel:stylesheet]', SyntaxErrorException::unexpectedToken('operator', new Token(Token::TYPE_DELIMITER, ':', 4))->getMessage()), array('[rel=stylesheet', SyntaxErrorException::unexpectedToken('"]"', new Token(Token::TYPE_FILE_END, '', 15))->getMessage()), array(':lang(fr', SyntaxErrorException::unexpectedToken('an argument', new Token(Token::TYPE_FILE_END, '', 8))->getMessage()), array(':contains("foo', SyntaxErrorException::unclosedString(10)->getMessage()), array('foo!', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '!', 3))->getMessage()));
 }