TranslatorTest.php 18 KB

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