ParserTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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([$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->expectException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
  78. Parser::parseSeries($function->getArguments());
  79. }
  80. public function getParserTestData()
  81. {
  82. return [
  83. ['*', ['Element[*]']],
  84. ['*|*', ['Element[*]']],
  85. ['*|foo', ['Element[foo]']],
  86. ['foo|*', ['Element[foo|*]']],
  87. ['foo|bar', ['Element[foo|bar]']],
  88. ['#foo#bar', ['Hash[Hash[Element[*]#foo]#bar]']],
  89. ['div>.foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
  90. ['div> .foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
  91. ['div >.foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
  92. ['div > .foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
  93. ["div \n> \t \t .foo", ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
  94. ['td.foo,.bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
  95. ['td.foo, .bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
  96. ["td.foo\t\r\n\f ,\t\r\n\f .bar", ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
  97. ['td.foo,.bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
  98. ['td.foo, .bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
  99. ["td.foo\t\r\n\f ,\t\r\n\f .bar", ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
  100. ['div, td.foo, div.bar span', ['Element[div]', 'Class[Element[td].foo]', 'CombinedSelector[Class[Element[div].bar] <followed> Element[span]]']],
  101. ['div > p', ['CombinedSelector[Element[div] > Element[p]]']],
  102. ['td:first', ['Pseudo[Element[td]:first]']],
  103. ['td :first', ['CombinedSelector[Element[td] <followed> Pseudo[Element[*]:first]]']],
  104. ['a[name]', ['Attribute[Element[a][name]]']],
  105. ["a[ name\t]", ['Attribute[Element[a][name]]']],
  106. ['a [name]', ['CombinedSelector[Element[a] <followed> Attribute[Element[*][name]]]']],
  107. ['[name="foo"]', ["Attribute[Element[*][name = 'foo']]"]],
  108. ["[name='foo[1]']", ["Attribute[Element[*][name = 'foo[1]']]"]],
  109. ["[name='foo[0][bar]']", ["Attribute[Element[*][name = 'foo[0][bar]']]"]],
  110. ['a[rel="include"]', ["Attribute[Element[a][rel = 'include']]"]],
  111. ['a[rel = include]', ["Attribute[Element[a][rel = 'include']]"]],
  112. ["a[hreflang |= 'en']", ["Attribute[Element[a][hreflang |= 'en']]"]],
  113. ['a[hreflang|=en]', ["Attribute[Element[a][hreflang |= 'en']]"]],
  114. ['div:nth-child(10)', ["Function[Element[div]:nth-child(['10'])]"]],
  115. [':nth-child(2n+2)', ["Function[Element[*]:nth-child(['2', 'n', '+2'])]"]],
  116. ['div:nth-of-type(10)', ["Function[Element[div]:nth-of-type(['10'])]"]],
  117. ['div div:nth-of-type(10) .aclass', ["CombinedSelector[CombinedSelector[Element[div] <followed> Function[Element[div]:nth-of-type(['10'])]] <followed> Class[Element[*].aclass]]"]],
  118. ['label:only', ['Pseudo[Element[label]:only]']],
  119. ['a:lang(fr)', ["Function[Element[a]:lang(['fr'])]"]],
  120. ['div:contains("foo")', ["Function[Element[div]:contains(['foo'])]"]],
  121. ['div#foobar', ['Hash[Element[div]#foobar]']],
  122. ['div:not(div.foo)', ['Negation[Element[div]:not(Class[Element[div].foo])]']],
  123. ['td ~ th', ['CombinedSelector[Element[td] ~ Element[th]]']],
  124. ['.foo[data-bar][data-baz=0]', ["Attribute[Attribute[Class[Element[*].foo][data-bar]][data-baz = '0']]"]],
  125. ];
  126. }
  127. public function getParserExceptionTestData()
  128. {
  129. return [
  130. ['attributes(href)/html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()],
  131. ['attributes(href)', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()],
  132. ['html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '/', 4))->getMessage()],
  133. [' ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 1))->getMessage()],
  134. ['div, ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 5))->getMessage()],
  135. [' , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 1))->getMessage()],
  136. ['p, , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 3))->getMessage()],
  137. ['div > ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 6))->getMessage()],
  138. [' > div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '>', 2))->getMessage()],
  139. ['foo|#bar', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_HASH, 'bar', 4))->getMessage()],
  140. ['#.foo', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '#', 0))->getMessage()],
  141. ['.#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()],
  142. [':#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()],
  143. ['[*]', SyntaxErrorException::unexpectedToken('"|"', new Token(Token::TYPE_DELIMITER, ']', 2))->getMessage()],
  144. ['[foo|]', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_DELIMITER, ']', 5))->getMessage()],
  145. ['[#]', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_DELIMITER, '#', 1))->getMessage()],
  146. ['[foo=#]', SyntaxErrorException::unexpectedToken('string or identifier', new Token(Token::TYPE_DELIMITER, '#', 5))->getMessage()],
  147. [':nth-child()', SyntaxErrorException::unexpectedToken('at least one argument', new Token(Token::TYPE_DELIMITER, ')', 11))->getMessage()],
  148. ['[href]a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_IDENTIFIER, 'a', 6))->getMessage()],
  149. ['[rel:stylesheet]', SyntaxErrorException::unexpectedToken('operator', new Token(Token::TYPE_DELIMITER, ':', 4))->getMessage()],
  150. ['[rel=stylesheet', SyntaxErrorException::unexpectedToken('"]"', new Token(Token::TYPE_FILE_END, '', 15))->getMessage()],
  151. [':lang(fr', SyntaxErrorException::unexpectedToken('an argument', new Token(Token::TYPE_FILE_END, '', 8))->getMessage()],
  152. [':contains("foo', SyntaxErrorException::unclosedString(10)->getMessage()],
  153. ['foo!', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '!', 3))->getMessage()],
  154. ];
  155. }
  156. public function getPseudoElementsTestData()
  157. {
  158. return [
  159. ['foo', 'Element[foo]', ''],
  160. ['*', 'Element[*]', ''],
  161. [':empty', 'Pseudo[Element[*]:empty]', ''],
  162. [':BEfore', 'Element[*]', 'before'],
  163. [':aftER', 'Element[*]', 'after'],
  164. [':First-Line', 'Element[*]', 'first-line'],
  165. [':First-Letter', 'Element[*]', 'first-letter'],
  166. ['::befoRE', 'Element[*]', 'before'],
  167. ['::AFter', 'Element[*]', 'after'],
  168. ['::firsT-linE', 'Element[*]', 'first-line'],
  169. ['::firsT-letteR', 'Element[*]', 'first-letter'],
  170. ['::Selection', 'Element[*]', 'selection'],
  171. ['foo:after', 'Element[foo]', 'after'],
  172. ['foo::selection', 'Element[foo]', 'selection'],
  173. ['lorem#ipsum ~ a#b.c[href]:empty::selection', 'CombinedSelector[Hash[Element[lorem]#ipsum] ~ Pseudo[Attribute[Class[Hash[Element[a]#b].c][href]]:empty]]', 'selection'],
  174. ['video::-webkit-media-controls', 'Element[video]', '-webkit-media-controls'],
  175. ];
  176. }
  177. public function getSpecificityTestData()
  178. {
  179. return [
  180. ['*', 0],
  181. [' foo', 1],
  182. [':empty ', 10],
  183. [':before', 1],
  184. ['*:before', 1],
  185. [':nth-child(2)', 10],
  186. ['.bar', 10],
  187. ['[baz]', 10],
  188. ['[baz="4"]', 10],
  189. ['[baz^="4"]', 10],
  190. ['#lipsum', 100],
  191. [':not(*)', 0],
  192. [':not(foo)', 1],
  193. [':not(.foo)', 10],
  194. [':not([foo])', 10],
  195. [':not(:empty)', 10],
  196. [':not(#foo)', 100],
  197. ['foo:empty', 11],
  198. ['foo:before', 2],
  199. ['foo::before', 2],
  200. ['foo:empty::before', 12],
  201. ['#lorem + foo#ipsum:first-child > bar:first-line', 213],
  202. ];
  203. }
  204. public function getParseSeriesTestData()
  205. {
  206. return [
  207. ['1n+3', 1, 3],
  208. ['1n +3', 1, 3],
  209. ['1n + 3', 1, 3],
  210. ['1n+ 3', 1, 3],
  211. ['1n-3', 1, -3],
  212. ['1n -3', 1, -3],
  213. ['1n - 3', 1, -3],
  214. ['1n- 3', 1, -3],
  215. ['n-5', 1, -5],
  216. ['odd', 2, 1],
  217. ['even', 2, 0],
  218. ['3n', 3, 0],
  219. ['n', 1, 0],
  220. ['+n', 1, 0],
  221. ['-n', -1, 0],
  222. ['5', 0, 5],
  223. ];
  224. }
  225. public function getParseSeriesExceptionTestData()
  226. {
  227. return [
  228. ['foo'],
  229. ['n+'],
  230. ];
  231. }
  232. }