PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/backend-blogtamsu/vendor/symfony/css-selector/Tests/Parser/ParserTest.php

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