Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function handle(Reader $reader, TokenStream $stream)
 {
     $quote = $reader->getSubstring(1);
     if (!in_array($quote, array("'", '"'))) {
         return false;
     }
     $reader->moveForward(1);
     $match = $reader->findPattern($this->patterns->getQuotedStringPattern($quote));
     if (!$match) {
         throw new InternalErrorException(sprintf('Should have found at least an empty match at %s.', $reader->getPosition()));
     }
     // check unclosed strings
     if (strlen($match[0]) === $reader->getRemainingLength()) {
         throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
     }
     // check quotes pairs validity
     if ($quote !== $reader->getSubstring(1, strlen($match[0]))) {
         throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
     }
     $string = $this->escaping->escapeUnicodeAndNewLine($match[0]);
     $stream->push(new Token(Token::TYPE_STRING, $string, $reader->getPosition()));
     $reader->moveForward(strlen($match[0]) + 1);
     return true;
 }
 /**
  * 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());
 }
Exemple #3
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);
 }
Exemple #4
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()));
 }