PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/CssSelector/Tests/Parser/ParserTest.php

https://bitbucket.org/vladap/symfony
PHP | 247 lines | 208 code | 21 blank | 18 comment | 0 complexity | 034ee9768ea1910c2af1003b2d39c654 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->assertEquals(1, count($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->assertEquals(1, count($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->assertEquals(1, count($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->assertEquals(1, count($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. );
  121. }
  122. public function getParserExceptionTestData()
  123. {
  124. return array(
  125. array('attributes(href)/html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()),
  126. array('attributes(href)', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()),
  127. array('html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '/', 4))->getMessage()),
  128. array(' ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 1))->getMessage()),
  129. array('div, ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 5))->getMessage()),
  130. array(' , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 1))->getMessage()),
  131. array('p, , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 3))->getMessage()),
  132. array('div > ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 6))->getMessage()),
  133. array(' > div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '>', 2))->getMessage()),
  134. array('foo|#bar', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_HASH, 'bar', 4))->getMessage()),
  135. array('#.foo', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '#', 0))->getMessage()),
  136. array('.#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()),
  137. array(':#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()),
  138. array('[*]', SyntaxErrorException::unexpectedToken('"|"', new Token(Token::TYPE_DELIMITER, ']', 2))->getMessage()),
  139. array('[foo|]', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_DELIMITER, ']', 5))->getMessage()),
  140. array('[#]', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_DELIMITER, '#', 1))->getMessage()),
  141. array('[foo=#]', SyntaxErrorException::unexpectedToken('string or identifier', new Token(Token::TYPE_DELIMITER, '#', 5))->getMessage()),
  142. array(':nth-child()', SyntaxErrorException::unexpectedToken('at least one argument', new Token(Token::TYPE_DELIMITER, ')', 11))->getMessage()),
  143. array('[href]a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_IDENTIFIER, 'a', 6))->getMessage()),
  144. array('[rel:stylesheet]', SyntaxErrorException::unexpectedToken('operator', new Token(Token::TYPE_DELIMITER, ':', 4))->getMessage()),
  145. array('[rel=stylesheet', SyntaxErrorException::unexpectedToken('"]"', new Token(Token::TYPE_FILE_END, '', 15))->getMessage()),
  146. array(':lang(fr', SyntaxErrorException::unexpectedToken('an argument', new Token(Token::TYPE_FILE_END, '', 8))->getMessage()),
  147. array(':contains("foo', SyntaxErrorException::unclosedString(10)->getMessage()),
  148. array('foo!', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '!', 3))->getMessage()),
  149. );
  150. }
  151. public function getPseudoElementsTestData()
  152. {
  153. return array(
  154. array('foo', 'Element[foo]', ''),
  155. array('*', 'Element[*]', ''),
  156. array(':empty', 'Pseudo[Element[*]:empty]', ''),
  157. array(':BEfore', 'Element[*]', 'before'),
  158. array(':aftER', 'Element[*]', 'after'),
  159. array(':First-Line', 'Element[*]', 'first-line'),
  160. array(':First-Letter', 'Element[*]', 'first-letter'),
  161. array('::befoRE', 'Element[*]', 'before'),
  162. array('::AFter', 'Element[*]', 'after'),
  163. array('::firsT-linE', 'Element[*]', 'first-line'),
  164. array('::firsT-letteR', 'Element[*]', 'first-letter'),
  165. array('::Selection', 'Element[*]', 'selection'),
  166. array('foo:after', 'Element[foo]', 'after'),
  167. array('foo::selection', 'Element[foo]', 'selection'),
  168. 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'),
  169. );
  170. }
  171. public function getSpecificityTestData()
  172. {
  173. return array(
  174. array('*', 0),
  175. array(' foo', 1),
  176. array(':empty ', 10),
  177. array(':before', 1),
  178. array('*:before', 1),
  179. array(':nth-child(2)', 10),
  180. array('.bar', 10),
  181. array('[baz]', 10),
  182. array('[baz="4"]', 10),
  183. array('[baz^="4"]', 10),
  184. array('#lipsum', 100),
  185. array(':not(*)', 0),
  186. array(':not(foo)', 1),
  187. array(':not(.foo)', 10),
  188. array(':not([foo])', 10),
  189. array(':not(:empty)', 10),
  190. array(':not(#foo)', 100),
  191. array('foo:empty', 11),
  192. array('foo:before', 2),
  193. array('foo::before', 2),
  194. array('foo:empty::before', 12),
  195. array('#lorem + foo#ipsum:first-child > bar:first-line', 213),
  196. );
  197. }
  198. public function getParseSeriesTestData()
  199. {
  200. return array(
  201. array('1n+3', 1, 3),
  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('n-5', 1, -5),
  210. array('odd', 2, 1),
  211. array('even', 2, 0),
  212. array('3n', 3, 0),
  213. array('n', 1, 0),
  214. array('+n', 1, 0),
  215. array('-n', -1, 0),
  216. array('5', 0, 5),
  217. );
  218. }
  219. public function getParseSeriesExceptionTestData()
  220. {
  221. return array(
  222. array('foo'),
  223. array('n+'),
  224. );
  225. }
  226. }