TranslatorTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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\XPath;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\CssSelector\XPath\Extension\HtmlExtension;
  13. use Symfony\Component\CssSelector\XPath\Translator;
  14. class TranslatorTest extends TestCase
  15. {
  16. /** @dataProvider getXpathLiteralTestData */
  17. public function testXpathLiteral($value, $literal)
  18. {
  19. $this->assertEquals($literal, Translator::getXpathLiteral($value));
  20. }
  21. /** @dataProvider getCssToXPathTestData */
  22. public function testCssToXPath($css, $xpath)
  23. {
  24. $translator = new Translator();
  25. $translator->registerExtension(new HtmlExtension($translator));
  26. $this->assertEquals($xpath, $translator->cssToXPath($css, ''));
  27. }
  28. /** @dataProvider getXmlLangTestData */
  29. public function testXmlLang($css, array $elementsId)
  30. {
  31. $translator = new Translator();
  32. $document = new \SimpleXMLElement(file_get_contents(__DIR__.'/Fixtures/lang.xml'));
  33. $elements = $document->xpath($translator->cssToXPath($css));
  34. $this->assertEquals(count($elementsId), count($elements));
  35. foreach ($elements as $element) {
  36. $this->assertTrue(in_array($element->attributes()->id, $elementsId));
  37. }
  38. }
  39. /** @dataProvider getHtmlIdsTestData */
  40. public function testHtmlIds($css, array $elementsId)
  41. {
  42. $translator = new Translator();
  43. $translator->registerExtension(new HtmlExtension($translator));
  44. $document = new \DOMDocument();
  45. $document->strictErrorChecking = false;
  46. $internalErrors = libxml_use_internal_errors(true);
  47. $document->loadHTMLFile(__DIR__.'/Fixtures/ids.html');
  48. $document = simplexml_import_dom($document);
  49. $elements = $document->xpath($translator->cssToXPath($css));
  50. $this->assertCount(count($elementsId), $elementsId);
  51. foreach ($elements as $element) {
  52. if (null !== $element->attributes()->id) {
  53. $this->assertTrue(in_array($element->attributes()->id, $elementsId));
  54. }
  55. }
  56. libxml_clear_errors();
  57. libxml_use_internal_errors($internalErrors);
  58. }
  59. /** @dataProvider getHtmlShakespearTestData */
  60. public function testHtmlShakespear($css, $count)
  61. {
  62. $translator = new Translator();
  63. $translator->registerExtension(new HtmlExtension($translator));
  64. $document = new \DOMDocument();
  65. $document->strictErrorChecking = false;
  66. $document->loadHTMLFile(__DIR__.'/Fixtures/shakespear.html');
  67. $document = simplexml_import_dom($document);
  68. $bodies = $document->xpath('//body');
  69. $elements = $bodies[0]->xpath($translator->cssToXPath($css));
  70. $this->assertCount($count, $elements);
  71. }
  72. public function getXpathLiteralTestData()
  73. {
  74. return array(
  75. array('foo', "'foo'"),
  76. array("foo's bar", '"foo\'s bar"'),
  77. array("foo's \"middle\" bar", 'concat(\'foo\', "\'", \'s "middle" bar\')'),
  78. array("foo's 'middle' \"bar\"", 'concat(\'foo\', "\'", \'s \', "\'", \'middle\', "\'", \' "bar"\')'),
  79. );
  80. }
  81. public function getCssToXPathTestData()
  82. {
  83. return array(
  84. array('*', '*'),
  85. array('e', 'e'),
  86. array('*|e', 'e'),
  87. array('e|f', 'e:f'),
  88. array('e[foo]', 'e[@foo]'),
  89. array('e[foo|bar]', 'e[@foo:bar]'),
  90. array('e[foo="bar"]', "e[@foo = 'bar']"),
  91. array('e[foo~="bar"]', "e[@foo and contains(concat(' ', normalize-space(@foo), ' '), ' bar ')]"),
  92. array('e[foo^="bar"]', "e[@foo and starts-with(@foo, 'bar')]"),
  93. array('e[foo$="bar"]', "e[@foo and substring(@foo, string-length(@foo)-2) = 'bar']"),
  94. array('e[foo*="bar"]', "e[@foo and contains(@foo, 'bar')]"),
  95. array('e[hreflang|="en"]', "e[@hreflang and (@hreflang = 'en' or starts-with(@hreflang, 'en-'))]"),
  96. array('e:nth-child(1)', "*/*[name() = 'e' and (position() = 1)]"),
  97. array('e:nth-last-child(1)', "*/*[name() = 'e' and (position() = last() - 0)]"),
  98. array('e:nth-last-child(2n+2)', "*/*[name() = 'e' and (last() - position() - 1 >= 0 and (last() - position() - 1) mod 2 = 0)]"),
  99. array('e:nth-of-type(1)', '*/e[position() = 1]'),
  100. array('e:nth-last-of-type(1)', '*/e[position() = last() - 0]'),
  101. array('div e:nth-last-of-type(1) .aclass', "div/descendant-or-self::*/e[position() = last() - 0]/descendant-or-self::*/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' aclass ')]"),
  102. array('e:first-child', "*/*[name() = 'e' and (position() = 1)]"),
  103. array('e:last-child', "*/*[name() = 'e' and (position() = last())]"),
  104. array('e:first-of-type', '*/e[position() = 1]'),
  105. array('e:last-of-type', '*/e[position() = last()]'),
  106. array('e:only-child', "*/*[name() = 'e' and (last() = 1)]"),
  107. array('e:only-of-type', 'e[last() = 1]'),
  108. array('e:empty', 'e[not(*) and not(string-length())]'),
  109. array('e:EmPTY', 'e[not(*) and not(string-length())]'),
  110. array('e:root', 'e[not(parent::*)]'),
  111. array('e:hover', 'e[0]'),
  112. array('e:contains("foo")', "e[contains(string(.), 'foo')]"),
  113. array('e:ConTains(foo)', "e[contains(string(.), 'foo')]"),
  114. array('e.warning', "e[@class and contains(concat(' ', normalize-space(@class), ' '), ' warning ')]"),
  115. array('e#myid', "e[@id = 'myid']"),
  116. array('e:not(:nth-child(odd))', 'e[not(position() - 1 >= 0 and (position() - 1) mod 2 = 0)]'),
  117. array('e:nOT(*)', 'e[0]'),
  118. array('e f', 'e/descendant-or-self::*/f'),
  119. array('e > f', 'e/f'),
  120. array('e + f', "e/following-sibling::*[name() = 'f' and (position() = 1)]"),
  121. array('e ~ f', 'e/following-sibling::f'),
  122. array('div#container p', "div[@id = 'container']/descendant-or-self::*/p"),
  123. );
  124. }
  125. public function getXmlLangTestData()
  126. {
  127. return array(
  128. array(':lang("EN")', array('first', 'second', 'third', 'fourth')),
  129. array(':lang("en-us")', array('second', 'fourth')),
  130. array(':lang(en-nz)', array('third')),
  131. array(':lang(fr)', array('fifth')),
  132. array(':lang(ru)', array('sixth')),
  133. array(":lang('ZH')", array('eighth')),
  134. array(':lang(de) :lang(zh)', array('eighth')),
  135. array(':lang(en), :lang(zh)', array('first', 'second', 'third', 'fourth', 'eighth')),
  136. array(':lang(es)', array()),
  137. );
  138. }
  139. public function getHtmlIdsTestData()
  140. {
  141. return array(
  142. array('div', array('outer-div', 'li-div', 'foobar-div')),
  143. array('DIV', array('outer-div', 'li-div', 'foobar-div')), // case-insensitive in HTML
  144. array('div div', array('li-div')),
  145. array('div, div div', array('outer-div', 'li-div', 'foobar-div')),
  146. array('a[name]', array('name-anchor')),
  147. array('a[NAme]', array('name-anchor')), // case-insensitive in HTML:
  148. array('a[rel]', array('tag-anchor', 'nofollow-anchor')),
  149. array('a[rel="tag"]', array('tag-anchor')),
  150. array('a[href*="localhost"]', array('tag-anchor')),
  151. array('a[href*=""]', array()),
  152. array('a[href^="http"]', array('tag-anchor', 'nofollow-anchor')),
  153. array('a[href^="http:"]', array('tag-anchor')),
  154. array('a[href^=""]', array()),
  155. array('a[href$="org"]', array('nofollow-anchor')),
  156. array('a[href$=""]', array()),
  157. array('div[foobar~="bc"]', array('foobar-div')),
  158. array('div[foobar~="cde"]', array('foobar-div')),
  159. array('[foobar~="ab bc"]', array('foobar-div')),
  160. array('[foobar~=""]', array()),
  161. array('[foobar~=" \t"]', array()),
  162. array('div[foobar~="cd"]', array()),
  163. array('*[lang|="En"]', array('second-li')),
  164. array('[lang|="En-us"]', array('second-li')),
  165. // Attribute values are case sensitive
  166. array('*[lang|="en"]', array()),
  167. array('[lang|="en-US"]', array()),
  168. array('*[lang|="e"]', array()),
  169. // ... :lang() is not.
  170. array(':lang("EN")', array('second-li', 'li-div')),
  171. array('*:lang(en-US)', array('second-li', 'li-div')),
  172. array(':lang("e")', array()),
  173. array('li:nth-child(3)', array('third-li')),
  174. array('li:nth-child(10)', array()),
  175. array('li:nth-child(2n)', array('second-li', 'fourth-li', 'sixth-li')),
  176. array('li:nth-child(even)', array('second-li', 'fourth-li', 'sixth-li')),
  177. array('li:nth-child(2n+0)', array('second-li', 'fourth-li', 'sixth-li')),
  178. array('li:nth-child(+2n+1)', array('first-li', 'third-li', 'fifth-li', 'seventh-li')),
  179. array('li:nth-child(odd)', array('first-li', 'third-li', 'fifth-li', 'seventh-li')),
  180. array('li:nth-child(2n+4)', array('fourth-li', 'sixth-li')),
  181. array('li:nth-child(3n+1)', array('first-li', 'fourth-li', 'seventh-li')),
  182. array('li:nth-child(n)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
  183. array('li:nth-child(n-1)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
  184. array('li:nth-child(n+1)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
  185. array('li:nth-child(n+3)', array('third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
  186. array('li:nth-child(-n)', array()),
  187. array('li:nth-child(-n-1)', array()),
  188. array('li:nth-child(-n+1)', array('first-li')),
  189. array('li:nth-child(-n+3)', array('first-li', 'second-li', 'third-li')),
  190. array('li:nth-last-child(0)', array()),
  191. array('li:nth-last-child(2n)', array('second-li', 'fourth-li', 'sixth-li')),
  192. array('li:nth-last-child(even)', array('second-li', 'fourth-li', 'sixth-li')),
  193. array('li:nth-last-child(2n+2)', array('second-li', 'fourth-li', 'sixth-li')),
  194. array('li:nth-last-child(n)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
  195. array('li:nth-last-child(n-1)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
  196. array('li:nth-last-child(n-3)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
  197. array('li:nth-last-child(n+1)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
  198. array('li:nth-last-child(n+3)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li')),
  199. array('li:nth-last-child(-n)', array()),
  200. array('li:nth-last-child(-n-1)', array()),
  201. array('li:nth-last-child(-n+1)', array('seventh-li')),
  202. array('li:nth-last-child(-n+3)', array('fifth-li', 'sixth-li', 'seventh-li')),
  203. array('ol:first-of-type', array('first-ol')),
  204. array('ol:nth-child(1)', array('first-ol')),
  205. array('ol:nth-of-type(2)', array('second-ol')),
  206. array('ol:nth-last-of-type(1)', array('second-ol')),
  207. array('span:only-child', array('foobar-span')),
  208. array('li div:only-child', array('li-div')),
  209. array('div *:only-child', array('li-div', 'foobar-span')),
  210. array('p:only-of-type', array('paragraph')),
  211. array('a:empty', array('name-anchor')),
  212. array('a:EMpty', array('name-anchor')),
  213. array('li:empty', array('third-li', 'fourth-li', 'fifth-li', 'sixth-li')),
  214. array(':root', array('html')),
  215. array('html:root', array('html')),
  216. array('li:root', array()),
  217. array('* :root', array()),
  218. array('*:contains("link")', array('html', 'outer-div', 'tag-anchor', 'nofollow-anchor')),
  219. array(':CONtains("link")', array('html', 'outer-div', 'tag-anchor', 'nofollow-anchor')),
  220. array('*:contains("LInk")', array()), // case sensitive
  221. array('*:contains("e")', array('html', 'nil', 'outer-div', 'first-ol', 'first-li', 'paragraph', 'p-em')),
  222. array('*:contains("E")', array()), // case-sensitive
  223. array('.a', array('first-ol')),
  224. array('.b', array('first-ol')),
  225. array('*.a', array('first-ol')),
  226. array('ol.a', array('first-ol')),
  227. array('.c', array('first-ol', 'third-li', 'fourth-li')),
  228. array('*.c', array('first-ol', 'third-li', 'fourth-li')),
  229. array('ol *.c', array('third-li', 'fourth-li')),
  230. array('ol li.c', array('third-li', 'fourth-li')),
  231. array('li ~ li.c', array('third-li', 'fourth-li')),
  232. array('ol > li.c', array('third-li', 'fourth-li')),
  233. array('#first-li', array('first-li')),
  234. array('li#first-li', array('first-li')),
  235. array('*#first-li', array('first-li')),
  236. array('li div', array('li-div')),
  237. array('li > div', array('li-div')),
  238. array('div div', array('li-div')),
  239. array('div > div', array()),
  240. array('div>.c', array('first-ol')),
  241. array('div > .c', array('first-ol')),
  242. array('div + div', array('foobar-div')),
  243. array('a ~ a', array('tag-anchor', 'nofollow-anchor')),
  244. array('a[rel="tag"] ~ a', array('nofollow-anchor')),
  245. array('ol#first-ol li:last-child', array('seventh-li')),
  246. array('ol#first-ol *:last-child', array('li-div', 'seventh-li')),
  247. array('#outer-div:first-child', array('outer-div')),
  248. array('#outer-div :first-child', array('name-anchor', 'first-li', 'li-div', 'p-b', 'checkbox-fieldset-disabled', 'area-href')),
  249. array('a[href]', array('tag-anchor', 'nofollow-anchor')),
  250. array(':not(*)', array()),
  251. array('a:not([href])', array('name-anchor')),
  252. array('ol :Not(li[class])', array('first-li', 'second-li', 'li-div', 'fifth-li', 'sixth-li', 'seventh-li')),
  253. // HTML-specific
  254. array(':link', array('link-href', 'tag-anchor', 'nofollow-anchor', 'area-href')),
  255. array(':visited', array()),
  256. array(':enabled', array('link-href', 'tag-anchor', 'nofollow-anchor', 'checkbox-unchecked', 'text-checked', 'checkbox-checked', 'area-href')),
  257. array(':disabled', array('checkbox-disabled', 'checkbox-disabled-checked', 'fieldset', 'checkbox-fieldset-disabled')),
  258. array(':checked', array('checkbox-checked', 'checkbox-disabled-checked')),
  259. );
  260. }
  261. public function getHtmlShakespearTestData()
  262. {
  263. return array(
  264. array('*', 246),
  265. array('div:contains(CELIA)', 26),
  266. array('div:only-child', 22), // ?
  267. array('div:nth-child(even)', 106),
  268. array('div:nth-child(2n)', 106),
  269. array('div:nth-child(odd)', 137),
  270. array('div:nth-child(2n+1)', 137),
  271. array('div:nth-child(n)', 243),
  272. array('div:last-child', 53),
  273. array('div:first-child', 51),
  274. array('div > div', 242),
  275. array('div + div', 190),
  276. array('div ~ div', 190),
  277. array('body', 1),
  278. array('body div', 243),
  279. array('div', 243),
  280. array('div div', 242),
  281. array('div div div', 241),
  282. array('div, div, div', 243),
  283. array('div, a, span', 243),
  284. array('.dialog', 51),
  285. array('div.dialog', 51),
  286. array('div .dialog', 51),
  287. array('div.character, div.dialog', 99),
  288. array('div.direction.dialog', 0),
  289. array('div.dialog.direction', 0),
  290. array('div.dialog.scene', 1),
  291. array('div.scene.scene', 1),
  292. array('div.scene .scene', 0),
  293. array('div.direction .dialog ', 0),
  294. array('div .dialog .direction', 4),
  295. array('div.dialog .dialog .direction', 4),
  296. array('#speech5', 1),
  297. array('div#speech5', 1),
  298. array('div #speech5', 1),
  299. array('div.scene div.dialog', 49),
  300. array('div#scene1 div.dialog div', 142),
  301. array('#scene1 #speech1', 1),
  302. array('div[class]', 103),
  303. array('div[class=dialog]', 50),
  304. array('div[class^=dia]', 51),
  305. array('div[class$=log]', 50),
  306. array('div[class*=sce]', 1),
  307. array('div[class|=dialog]', 50), // ? Seems right
  308. array('div[class!=madeup]', 243), // ? Seems right
  309. array('div[class~=dialog]', 51), // ? Seems right
  310. );
  311. }
  312. }