ParserTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\CssSelector\Tests\Parser;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
  13. use Symfony\Component\CssSelector\Node\FunctionNode;
  14. use Symfony\Component\CssSelector\Node\SelectorNode;
  15. use Symfony\Component\CssSelector\Parser\Parser;
  16. use Symfony\Component\CssSelector\Parser\Token;
  17. class ParserTest extends TestCase
  18. {
  19. /** @dataProvider getParserTestData */
  20. public function testParser($source, $representation)
  21. {
  22. $parser = new Parser();
  23. $this->assertEquals($representation, array_map(function (SelectorNode $node) {
  24. return (string) $node->getTree();
  25. }, $parser->parse($source)));
  26. }
  27. /** @dataProvider getParserExceptionTestData */
  28. public function testParserException($source, $message)
  29. {
  30. $parser = new Parser();
  31. try {
  32. $parser->parse($source);
  33. $this->fail('Parser should throw a SyntaxErrorException.');
  34. } catch (SyntaxErrorException $e) {
  35. $this->assertEquals($message, $e->getMessage());
  36. }
  37. }
  38. /** @dataProvider getPseudoElementsTestData */
  39. public function testPseudoElements($source, $element, $pseudo)
  40. {
  41. $parser = new Parser();
  42. $selectors = $parser->parse($source);
  43. $this->assertCount(1, $selectors);
  44. /** @var SelectorNode $selector */
  45. $selector = $selectors[0];
  46. $this->assertEquals($element, (string) $selector->getTree());
  47. $this->assertEquals($pseudo, (string) $selector->getPseudoElement());
  48. }
  49. /** @dataProvider getSpecificityTestData */
  50. public function testSpecificity($source, $value)
  51. {
  52. $parser = new Parser();
  53. $selectors = $parser->parse($source);
  54. $this->assertCount(1, $selectors);
  55. /** @var SelectorNode $selector */
  56. $selector = $selectors[0];
  57. $this->assertEquals($value, $selector->getSpecificity()->getValue());
  58. }
  59. /** @dataProvider getParseSeriesTestData */
  60. public function testParseSeries($series, $a, $b)
  61. {
  62. $parser = new Parser();
  63. $selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
  64. $this->assertCount(1, $selectors);
  65. /** @var FunctionNode $function */
  66. $function = $selectors[0]->getTree();
  67. $this->assertEquals(array($a, $b), Parser::parseSeries($function->getArguments()));
  68. }
  69. /** @dataProvider getParseSeriesExceptionTestData */
  70. public function testParseSeriesException($series)
  71. {
  72. $parser = new Parser();
  73. $selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
  74. $this->assertCount(1, $selectors);
  75. /** @var FunctionNode $function */
  76. $function = $selectors[0]->getTree();
  77. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
  78. Parser::parseSeries($function->getArguments());
  79. }
  80. public function getParserTestData()
  81. {
  82. return array(
  83. array('*', array('Element[*]')),
  84. array('*|*', array('Element[*]')),
  85. array('*|foo', array('Element[foo]')),
  86. array('foo|*', array('Element[foo|*]')),
  87. array('foo|bar', array('Element[foo|bar]')),
  88. array('#foo#bar', array('Hash[Hash[Element[*]#foo]#bar]')),
  89. array('div>.foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
  90. array('div> .foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
  91. array('div >.foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
  92. array('div > .foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
  93. array("div \n> \t \t .foo", array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
  94. array('td.foo,.bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
  95. array('td.foo, .bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
  96. array("td.foo\t\r\n\f ,\t\r\n\f .bar", array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
  97. array('td.foo,.bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
  98. array('td.foo, .bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
  99. array("td.foo\t\r\n\f ,\t\r\n\f .bar", array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
  100. array('div, td.foo, div.bar span', array('Element[div]', 'Class[Element[td].foo]', 'CombinedSelector[Class[Element[div].bar] <followed> Element[span]]')),
  101. array('div > p', array('CombinedSelector[Element[div] > Element[p]]')),
  102. array('td:first', array('Pseudo[Element[td]:first]')),
  103. array('td :first', array('CombinedSelector[Element[td] <followed> Pseudo[Element[*]:first]]')),
  104. array('a[name]', array('Attribute[Element[a][name]]')),
  105. array("a[ name\t]", array('Attribute[Element[a][name]]')),
  106. array('a [name]', array('CombinedSelector[Element[a] <followed> Attribute[Element[*][name]]]')),
  107. array('a[rel="include"]', array("Attribute[Element[a][rel = 'include']]")),
  108. array('a[rel = include]', array("Attribute[Element[a][rel = 'include']]")),
  109. array("a[hreflang |= 'en']", array("Attribute[Element[a][hreflang |= 'en']]")),
  110. array('a[hreflang|=en]', array("Attribute[Element[a][hreflang |= 'en']]")),
  111. array('div:nth-child(10)', array("Function[Element[div]:nth-child(['10'])]")),
  112. array(':nth-child(2n+2)', array("Function[Element[*]:nth-child(['2', 'n', '+2'])]")),
  113. array('div:nth-of-type(10)', array("Function[Element[div]:nth-of-type(['10'])]")),
  114. array('div div:nth-of-type(10) .aclass', array("CombinedSelector[CombinedSelector[Element[div] <followed> Function[Element[div]:nth-of-type(['10'])]] <followed> Class[Element[*].aclass]]")),
  115. array('label:only', array('Pseudo[Element[label]:only]')),
  116. array('a:lang(fr)', array("Function[Element[a]:lang(['fr'])]")),
  117. array('div:contains("foo")', array("Function[Element[div]:contains(['foo'])]")),
  118. array('div#foobar', array('Hash[Element[div]#foobar]')),
  119. array('div:not(div.foo)', array('Negation[Element[div]:not(Class[Element[div].foo])]')),
  120. array('td ~ th', array('CombinedSelector[Element[td] ~ Element[th]]')),
  121. array('.foo[data-bar][data-baz=0]', array("Attribute[Attribute[Class[Element[*].foo][data-bar]][data-baz = '0']]")),
  122. );
  123. }
  124. public function getParserExceptionTestData()
  125. {
  126. return array(
  127. array('attributes(href)/html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()),
  128. array('attributes(href)', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()),
  129. array('html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '/', 4))->getMessage()),
  130. array(' ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 1))->getMessage()),
  131. array('div, ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 5))->getMessage()),
  132. array(' , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 1))->getMessage()),
  133. array('p, , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 3))->getMessage()),
  134. array('div > ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 6))->getMessage()),
  135. array(' > div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '>', 2))->getMessage()),
  136. array('foo|#bar', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_HASH, 'bar', 4))->getMessage()),
  137. array('#.foo', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '#', 0))->getMessage()),
  138. array('.#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()),
  139. array(':#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()),
  140. array('[*]', SyntaxErrorException::unexpectedToken('"|"', new Token(Token::TYPE_DELIMITER, ']', 2))->getMessage()),
  141. array('[foo|]', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_DELIMITER, ']', 5))->getMessage()),
  142. array('[#]', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_DELIMITER, '#', 1))->getMessage()),
  143. array('[foo=#]', SyntaxErrorException::unexpectedToken('string or identifier', new Token(Token::TYPE_DELIMITER, '#', 5))->getMessage()),
  144. array(':nth-child()', SyntaxErrorException::unexpectedToken('at least one argument', new Token(Token::TYPE_DELIMITER, ')', 11))->getMessage()),
  145. array('[href]a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_IDENTIFIER, 'a', 6))->getMessage()),
  146. array('[rel:stylesheet]', SyntaxErrorException::unexpectedToken('operator', new Token(Token::TYPE_DELIMITER, ':', 4))->getMessage()),
  147. array('[rel=stylesheet', SyntaxErrorException::unexpectedToken('"]"', new Token(Token::TYPE_FILE_END, '', 15))->getMessage()),
  148. array(':lang(fr', SyntaxErrorException::unexpectedToken('an argument', new Token(Token::TYPE_FILE_END, '', 8))->getMessage()),
  149. array(':contains("foo', SyntaxErrorException::unclosedString(10)->getMessage()),
  150. array('foo!', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '!', 3))->getMessage()),
  151. );
  152. }
  153. public function getPseudoElementsTestData()
  154. {
  155. return array(
  156. array('foo', 'Element[foo]', ''),
  157. array('*', 'Element[*]', ''),
  158. array(':empty', 'Pseudo[Element[*]:empty]', ''),
  159. array(':BEfore', 'Element[*]', 'before'),
  160. array(':aftER', 'Element[*]', 'after'),
  161. array(':First-Line', 'Element[*]', 'first-line'),
  162. array(':First-Letter', 'Element[*]', 'first-letter'),
  163. array('::befoRE', 'Element[*]', 'before'),
  164. array('::AFter', 'Element[*]', 'after'),
  165. array('::firsT-linE', 'Element[*]', 'first-line'),
  166. array('::firsT-letteR', 'Element[*]', 'first-letter'),
  167. array('::Selection', 'Element[*]', 'selection'),
  168. array('foo:after', 'Element[foo]', 'after'),
  169. array('foo::selection', 'Element[foo]', 'selection'),
  170. array('lorem#ipsum ~ a#b.c[href]:empty::selection', 'CombinedSelector[Hash[Element[lorem]#ipsum] ~ Pseudo[Attribute[Class[Hash[Element[a]#b].c][href]]:empty]]', 'selection'),
  171. );
  172. }
  173. public function getSpecificityTestData()
  174. {
  175. return array(
  176. array('*', 0),
  177. array(' foo', 1),
  178. array(':empty ', 10),
  179. array(':before', 1),
  180. array('*:before', 1),
  181. array(':nth-child(2)', 10),
  182. array('.bar', 10),
  183. array('[baz]', 10),
  184. array('[baz="4"]', 10),
  185. array('[baz^="4"]', 10),
  186. array('#lipsum', 100),
  187. array(':not(*)', 0),
  188. array(':not(foo)', 1),
  189. array(':not(.foo)', 10),
  190. array(':not([foo])', 10),
  191. array(':not(:empty)', 10),
  192. array(':not(#foo)', 100),
  193. array('foo:empty', 11),
  194. array('foo:before', 2),
  195. array('foo::before', 2),
  196. array('foo:empty::before', 12),
  197. array('#lorem + foo#ipsum:first-child > bar:first-line', 213),
  198. );
  199. }
  200. public function getParseSeriesTestData()
  201. {
  202. return array(
  203. array('1n+3', 1, 3),
  204. array('1n +3', 1, 3),
  205. array('1n + 3', 1, 3),
  206. array('1n+ 3', 1, 3),
  207. array('1n-3', 1, -3),
  208. array('1n -3', 1, -3),
  209. array('1n - 3', 1, -3),
  210. array('1n- 3', 1, -3),
  211. array('n-5', 1, -5),
  212. array('odd', 2, 1),
  213. array('even', 2, 0),
  214. array('3n', 3, 0),
  215. array('n', 1, 0),
  216. array('+n', 1, 0),
  217. array('-n', -1, 0),
  218. array('5', 0, 5),
  219. );
  220. }
  221. public function getParseSeriesExceptionTestData()
  222. {
  223. return array(
  224. array('foo'),
  225. array('n+'),
  226. );
  227. }
  228. }