TranslatorTest.php 18 KB

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