CssEventHandler.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. <?php
  2. /** @file
  3. * This file contains a full implementation of the CssEventHandler interface.
  4. *
  5. * The tools in this package initiate a CSS selector parsing routine and then
  6. * handle all of the callbacks.
  7. *
  8. * The implementation provided herein adheres to the CSS 3 Selector specification
  9. * with the following caveats:
  10. *
  11. * - The negation (:not()) and containment (:has()) pseudo-classes allow *full*
  12. * selectors and not just simple selectors.
  13. * - There are a variety of additional pseudo-classes supported by this
  14. * implementation that are not part of the spec. Most of the jQuery
  15. * pseudo-classes are supported. The :x-root pseudo-class is also supported.
  16. * - Pseudo-classes that require a User Agent to function have been disabled.
  17. * Thus there is no :hover pseudo-class.
  18. * - All pseudo-elements require the double-colon (::) notation. This breaks
  19. * backward compatibility with the 2.1 spec, but it makes visible the issue
  20. * that pseudo-elements cannot be effectively used with most of the present
  21. * library. They return <b>stdClass objects with a text property</b> (QP > 1.3)
  22. * instead of elements.
  23. * - The pseudo-classes first-of-type, nth-of-type and last-of-type may or may
  24. * not conform to the specification. The spec is unclear.
  25. * - pseudo-class filters of the form -an+b do not function as described in the
  26. * specification. However, they do behave the same way here as they do in
  27. * jQuery.
  28. * - This library DOES provide XML namespace aware tools. Selectors can use
  29. * namespaces to increase specificity.
  30. * - This library does nothing with the CSS 3 Selector specificity rating. Of
  31. * course specificity is preserved (to the best of our abilities), but there
  32. * is no calculation done.
  33. *
  34. * For detailed examples of how the code works and what selectors are supported,
  35. * see the CssEventTests file, which contains the unit tests used for
  36. * testing this implementation.
  37. *
  38. * @author M Butcher <matt@aleph-null.tv>
  39. * @license http://opensource.org/licenses/lgpl-2.1.php LGPL (The GNU Lesser GPL) or an MIT-like license.
  40. */
  41. /**
  42. * Require the parser library.
  43. */
  44. require_once 'CssParser.php';
  45. /**
  46. * Handler that tracks progress of a query through a DOM.
  47. *
  48. * The main idea is that we keep a copy of the tree, and then use an
  49. * array to keep track of matches. To handle a list of selectors (using
  50. * the comma separator), we have to track both the currently progressing
  51. * match and the previously matched elements.
  52. *
  53. * To use this handler:
  54. * @code
  55. * $filter = '#id'; // Some CSS selector
  56. * $handler = new QueryPathCssParser(DOMNode $dom);
  57. * $parser = new CssParser();
  58. * $parser->parse($filter, $handler);
  59. * $matches = $handler->getMatches();
  60. * @endcode
  61. *
  62. * $matches will be an array of zero or more DOMElement objects.
  63. *
  64. * @ingroup querypath_css
  65. */
  66. class QueryPathCssEventHandler implements CssEventHandler {
  67. protected $dom = NULL; // Always points to the top level.
  68. protected $matches = NULL; // The matches
  69. protected $alreadyMatched = NULL; // Matches found before current selector.
  70. protected $findAnyElement = TRUE;
  71. /**
  72. * Create a new event handler.
  73. */
  74. public function __construct($dom) {
  75. $this->alreadyMatched = new SplObjectStorage();
  76. $matches = new SplObjectStorage();
  77. // Array of DOMElements
  78. if (is_array($dom) || $dom instanceof SplObjectStorage) {
  79. //$matches = array();
  80. foreach($dom as $item) {
  81. if ($item instanceof DOMNode && $item->nodeType == XML_ELEMENT_NODE) {
  82. //$matches[] = $item;
  83. $matches->attach($item);
  84. }
  85. }
  86. //$this->dom = count($matches) > 0 ? $matches[0] : NULL;
  87. if ($matches->count() > 0) {
  88. $matches->rewind();
  89. $this->dom = $matches->current();
  90. }
  91. else {
  92. //throw new Exception("Setting DOM to Null");
  93. $this->dom = NULL;
  94. }
  95. $this->matches = $matches;
  96. }
  97. // DOM Document -- we get the root element.
  98. elseif ($dom instanceof DOMDocument) {
  99. $this->dom = $dom->documentElement;
  100. $matches->attach($dom->documentElement);
  101. }
  102. // DOM Element -- we use this directly
  103. elseif ($dom instanceof DOMElement) {
  104. $this->dom = $dom;
  105. $matches->attach($dom);
  106. }
  107. // NodeList -- We turn this into an array
  108. elseif ($dom instanceof DOMNodeList) {
  109. $a = array(); // Not sure why we are doing this....
  110. foreach ($dom as $item) {
  111. if ($item->nodeType == XML_ELEMENT_NODE) {
  112. $matches->attach($item);
  113. $a[] = $item;
  114. }
  115. }
  116. $this->dom = $a;
  117. }
  118. // FIXME: Handle SimpleXML!
  119. // Uh-oh... we don't support anything else.
  120. else {
  121. throw new Exception("Unhandled type: " . get_class($dom));
  122. }
  123. $this->matches = $matches;
  124. }
  125. /**
  126. * Generic finding method.
  127. *
  128. * This is the primary searching method used throughout QueryPath.
  129. *
  130. * @param string $filter
  131. * A valid CSS 3 filter.
  132. * @return QueryPathCssEventHandler
  133. * Returns itself.
  134. */
  135. public function find($filter) {
  136. $parser = new CssParser($filter, $this);
  137. $parser->parse();
  138. return $this;
  139. }
  140. /**
  141. * Get the elements that match the evaluated selector.
  142. *
  143. * This should be called after the filter has been parsed.
  144. *
  145. * @return array
  146. * The matched items. This is almost always an array of
  147. * {@link DOMElement} objects. It is always an instance of
  148. * {@link DOMNode} objects.
  149. */
  150. public function getMatches() {
  151. //$result = array_merge($this->alreadyMatched, $this->matches);
  152. $result = new SplObjectStorage();
  153. foreach($this->alreadyMatched as $m) $result->attach($m);
  154. foreach($this->matches as $m) $result->attach($m);
  155. return $result;
  156. }
  157. /**
  158. * Find any element with the ID that matches $id.
  159. *
  160. * If this finds an ID, it will immediately quit. Essentially, it doesn't
  161. * enforce ID uniqueness, but it assumes it.
  162. *
  163. * @param $id
  164. * String ID for an element.
  165. */
  166. public function elementID($id) {
  167. $found = new SplObjectStorage();
  168. $matches = $this->candidateList();
  169. foreach ($matches as $item) {
  170. // Check if any of the current items has the desired ID.
  171. if ($item->hasAttribute('id') && $item->getAttribute('id') === $id) {
  172. $found->attach($item);
  173. break;
  174. }
  175. }
  176. $this->matches = $found;
  177. $this->findAnyElement = FALSE;
  178. }
  179. // Inherited
  180. public function element($name) {
  181. $matches = $this->candidateList();
  182. $this->findAnyElement = FALSE;
  183. $found = new SplObjectStorage();
  184. foreach ($matches as $item) {
  185. // Should the existing item be included?
  186. // In some cases (e.g. element is root element)
  187. // it definitely should. But what about other cases?
  188. if ($item->tagName == $name) {
  189. $found->attach($item);
  190. }
  191. // Search for matching kids.
  192. //$nl = $item->getElementsByTagName($name);
  193. //$found = array_merge($found, $this->nodeListToArray($nl));
  194. }
  195. $this->matches = $found;
  196. }
  197. // Inherited
  198. public function elementNS($lname, $namespace = NULL) {
  199. $this->findAnyElement = FALSE;
  200. $found = new SplObjectStorage();
  201. $matches = $this->candidateList();
  202. foreach ($matches as $item) {
  203. // Looking up NS URI only works if the XMLNS attributes are declared
  204. // at a level equal to or above the searching doc. Normalizing a doc
  205. // should fix this, but it doesn't. So we have to use a fallback
  206. // detection scheme which basically searches by lname and then
  207. // does a post hoc check on the tagname.
  208. //$nsuri = $item->lookupNamespaceURI($namespace);
  209. $nsuri = $this->dom->lookupNamespaceURI($namespace);
  210. // XXX: Presumably the base item needs to be checked. Spec isn't
  211. // too clear, but there are three possibilities:
  212. // - base should always be checked (what we do here)
  213. // - base should never be checked (only children)
  214. // - base should only be checked if it is the root node
  215. if ($item instanceof DOMNode
  216. && $item->namespaceURI == $nsuri
  217. && $lname == $item->localName) {
  218. $found->attach($item);
  219. }
  220. if (!empty($nsuri)) {
  221. $nl = $item->getElementsByTagNameNS($nsuri, $lname);
  222. // If something is found, merge them:
  223. //if (!empty($nl)) $found = array_merge($found, $this->nodeListToArray($nl));
  224. if (!empty($nl)) $this->attachNodeList($nl, $found);
  225. }
  226. else {
  227. //$nl = $item->getElementsByTagName($namespace . ':' . $lname);
  228. $nl = $item->getElementsByTagName($lname);
  229. $tagname = $namespace . ':' . $lname;
  230. $nsmatches = array();
  231. foreach ($nl as $node) {
  232. if ($node->tagName == $tagname) {
  233. //$nsmatches[] = $node;
  234. $found->attach($node);
  235. }
  236. }
  237. // If something is found, merge them:
  238. //if (!empty($nsmatches)) $found = array_merge($found, $nsmatches);
  239. }
  240. }
  241. $this->matches = $found;
  242. }
  243. public function anyElement() {
  244. $found = new SplObjectStorage();
  245. //$this->findAnyElement = TRUE;
  246. $matches = $this->candidateList();
  247. foreach ($matches as $item) {
  248. $found->attach($item); // Add self
  249. // See issue #20 or section 6.2 of this:
  250. // http://www.w3.org/TR/2009/PR-css3-selectors-20091215/#universal-selector
  251. //$nl = $item->getElementsByTagName('*');
  252. //$this->attachNodeList($nl, $found);
  253. }
  254. $this->matches = $found;
  255. $this->findAnyElement = FALSE;
  256. }
  257. public function anyElementInNS($ns) {
  258. //$this->findAnyElement = TRUE;
  259. $nsuri = $this->dom->lookupNamespaceURI($ns);
  260. $found = new SplObjectStorage();
  261. if (!empty($nsuri)) {
  262. $matches = $this->candidateList();
  263. foreach ($matches as $item) {
  264. if ($item instanceOf DOMNode && $nsuri == $item->namespaceURI) {
  265. $found->attach($item);
  266. }
  267. }
  268. }
  269. $this->matches = $found;//UniqueElementList::get($found);
  270. $this->findAnyElement = FALSE;
  271. }
  272. public function elementClass($name) {
  273. $found = new SplObjectStorage();
  274. $matches = $this->candidateList();
  275. foreach ($matches as $item) {
  276. if ($item->hasAttribute('class')) {
  277. $classes = explode(' ', $item->getAttribute('class'));
  278. if (in_array($name, $classes)) $found->attach($item);
  279. }
  280. }
  281. $this->matches = $found;//UniqueElementList::get($found);
  282. $this->findAnyElement = FALSE;
  283. }
  284. public function attribute($name, $value = NULL, $operation = CssEventHandler::isExactly) {
  285. $found = new SplObjectStorage();
  286. $matches = $this->candidateList();
  287. foreach ($matches as $item) {
  288. if ($item->hasAttribute($name)) {
  289. if (isset($value)) {
  290. // If a value exists, then we need a match.
  291. if($this->attrValMatches($value, $item->getAttribute($name), $operation)) {
  292. $found->attach($item);
  293. }
  294. }
  295. else {
  296. // If no value exists, then we consider it a match.
  297. $found->attach($item);
  298. }
  299. }
  300. }
  301. $this->matches = $found; //UniqueElementList::get($found);
  302. $this->findAnyElement = FALSE;
  303. }
  304. /**
  305. * Helper function to find all elements with exact matches.
  306. *
  307. * @deprecated All use cases seem to be covered by attribute().
  308. */
  309. protected function searchForAttr($name, $value = NULL) {
  310. $found = new SplObjectStorage();
  311. $matches = $this->candidateList();
  312. foreach ($matches as $candidate) {
  313. if ($candidate->hasAttribute($name)) {
  314. // If value is required, match that, too.
  315. if (isset($value) && $value == $candidate->getAttribute($name)) {
  316. $found->attach($candidate);
  317. }
  318. // Otherwise, it's a match on name alone.
  319. else {
  320. $found->attach($candidate);
  321. }
  322. }
  323. }
  324. $this->matches = $found;
  325. }
  326. public function attributeNS($lname, $ns, $value = NULL, $operation = CssEventHandler::isExactly) {
  327. $matches = $this->candidateList();
  328. $found = new SplObjectStorage();
  329. if (count($matches) == 0) {
  330. $this->matches = $found;
  331. return;
  332. }
  333. // Get the namespace URI for the given label.
  334. //$uri = $matches[0]->lookupNamespaceURI($ns);
  335. $matches->rewind();
  336. $e = $matches->current();
  337. $uri = $e->lookupNamespaceURI($ns);
  338. foreach ($matches as $item) {
  339. //foreach ($item->attributes as $attr) {
  340. // print "$attr->prefix:$attr->localName ($attr->namespaceURI), Value: $attr->nodeValue\n";
  341. //}
  342. if ($item->hasAttributeNS($uri, $lname)) {
  343. if (isset($value)) {
  344. if ($this->attrValMatches($value, $item->getAttributeNS($uri, $lname), $operation)) {
  345. $found->attach($item);
  346. }
  347. }
  348. else {
  349. $found->attach($item);
  350. }
  351. }
  352. }
  353. $this->matches = $found;
  354. $this->findAnyElement = FALSE;
  355. }
  356. /**
  357. * This also supports the following nonstandard pseudo classes:
  358. * - :x-reset/:x-root (reset to the main item passed into the constructor. Less drastic than :root)
  359. * - :odd/:even (shorthand for :nth-child(odd)/:nth-child(even))
  360. */
  361. public function pseudoClass($name, $value = NULL) {
  362. $name = strtolower($name);
  363. // Need to handle known pseudoclasses.
  364. switch($name) {
  365. case 'visited':
  366. case 'hover':
  367. case 'active':
  368. case 'focus':
  369. case 'animated': // Last 3 are from jQuery
  370. case 'visible':
  371. case 'hidden':
  372. // These require a UA, which we don't have.
  373. case 'target':
  374. // This requires a location URL, which we don't have.
  375. $this->matches = new SplObjectStorage();
  376. break;
  377. case 'indeterminate':
  378. // The assumption is that there is a UA and the format is HTML.
  379. // I don't know if this should is useful without a UA.
  380. throw new NotImplementedException(":indeterminate is not implemented.");
  381. break;
  382. case 'lang':
  383. // No value = exception.
  384. if (!isset($value)) {
  385. throw new NotImplementedException("No handler for lang pseudoclass without value.");
  386. }
  387. $this->lang($value);
  388. break;
  389. case 'link':
  390. $this->searchForAttr('href');
  391. break;
  392. case 'root':
  393. $found = new SplObjectStorage();
  394. if (empty($this->dom)) {
  395. $this->matches = $found;
  396. }
  397. elseif (is_array($this->dom)) {
  398. $found->attach($this->dom[0]->ownerDocument->documentElement);
  399. $this->matches = $found;
  400. }
  401. elseif ($this->dom instanceof DOMNode) {
  402. $found->attach($this->dom->ownerDocument->documentElement);
  403. $this->matches = $found;
  404. }
  405. elseif ($this->dom instanceof DOMNodeList && $this->dom->length > 0) {
  406. $found->attach($this->dom->item(0)->ownerDocument->documentElement);
  407. $this->matches = $found;
  408. }
  409. else {
  410. // Hopefully we never get here:
  411. $found->attach($this->dom);
  412. $this->matches = $found;
  413. }
  414. break;
  415. // NON-STANDARD extensions for reseting to the "top" items set in
  416. // the constructor.
  417. case 'x-root':
  418. case 'x-reset':
  419. $this->matches = new SplObjectStorage();
  420. $this->matches->attach($this->dom);
  421. break;
  422. // NON-STANDARD extensions for simple support of even and odd. These
  423. // are supported by jQuery, FF, and other user agents.
  424. case 'even':
  425. $this->nthChild(2, 0);
  426. break;
  427. case 'odd':
  428. $this->nthChild(2, 1);
  429. break;
  430. // Standard child-checking items.
  431. case 'nth-child':
  432. list($aVal, $bVal) = $this->parseAnB($value);
  433. $this->nthChild($aVal, $bVal);
  434. break;
  435. case 'nth-last-child':
  436. list($aVal, $bVal) = $this->parseAnB($value);
  437. $this->nthLastChild($aVal, $bVal);
  438. break;
  439. case 'nth-of-type':
  440. list($aVal, $bVal) = $this->parseAnB($value);
  441. $this->nthOfTypeChild($aVal, $bVal, FALSE);
  442. break;
  443. case 'nth-last-of-type':
  444. list($aVal, $bVal) = $this->parseAnB($value);
  445. $this->nthLastOfTypeChild($aVal, $bVal);
  446. break;
  447. case 'first-child':
  448. $this->nthChild(0, 1);
  449. break;
  450. case 'last-child':
  451. $this->nthLastChild(0, 1);
  452. break;
  453. case 'first-of-type':
  454. $this->firstOfType();
  455. break;
  456. case 'last-of-type':
  457. $this->lastOfType();
  458. break;
  459. case 'only-child':
  460. $this->onlyChild();
  461. break;
  462. case 'only-of-type':
  463. $this->onlyOfType();
  464. break;
  465. case 'empty':
  466. $this->emptyElement();
  467. break;
  468. case 'not':
  469. if (empty($value)) {
  470. throw new CssParseException(":not() requires a value.");
  471. }
  472. $this->not($value);
  473. break;
  474. // Additional pseudo-classes defined in jQuery:
  475. case 'lt':
  476. case 'gt':
  477. case 'nth':
  478. case 'eq':
  479. case 'first':
  480. case 'last':
  481. //case 'even':
  482. //case 'odd':
  483. $this->getByPosition($name, $value);
  484. break;
  485. case 'parent':
  486. $matches = $this->candidateList();
  487. $found = new SplObjectStorage();
  488. foreach ($matches as $match) {
  489. if (!empty($match->firstChild)) {
  490. $found->attach($match);
  491. }
  492. }
  493. $this->matches = $found;
  494. break;
  495. case 'enabled':
  496. case 'disabled':
  497. case 'checked':
  498. $this->attribute($name);
  499. break;
  500. case 'text':
  501. case 'radio':
  502. case 'checkbox':
  503. case 'file':
  504. case 'password':
  505. case 'submit':
  506. case 'image':
  507. case 'reset':
  508. case 'button':
  509. $this->attribute('type', $name);
  510. break;
  511. case 'header':
  512. $matches = $this->candidateList();
  513. $found = new SplObjectStorage();
  514. foreach ($matches as $item) {
  515. $tag = $item->tagName;
  516. $f = strtolower(substr($tag, 0, 1));
  517. if ($f == 'h' && strlen($tag) == 2 && ctype_digit(substr($tag, 1, 1))) {
  518. $found->attach($item);
  519. }
  520. }
  521. $this->matches = $found;
  522. break;
  523. case 'has':
  524. $this->has($value);
  525. break;
  526. // Contains == text matches.
  527. // In QP 2.1, this was changed.
  528. case 'contains':
  529. $value = $this->removeQuotes($value);
  530. $matches = $this->candidateList();
  531. $found = new SplObjectStorage();
  532. foreach ($matches as $item) {
  533. if (strpos($item->textContent, $value) !== FALSE) {
  534. $found->attach($item);
  535. }
  536. }
  537. $this->matches = $found;
  538. break;
  539. // Since QP 2.1
  540. case 'contains-exactly':
  541. $value = $this->removeQuotes($value);
  542. $matches = $this->candidateList();
  543. $found = new SplObjectStorage();
  544. foreach ($matches as $item) {
  545. if ($item->textContent == $value) {
  546. $found->attach($item);
  547. }
  548. }
  549. $this->matches = $found;
  550. break;
  551. default:
  552. throw new CssParseException("Unknown Pseudo-Class: " . $name);
  553. }
  554. $this->findAnyElement = FALSE;
  555. }
  556. /**
  557. * Remove leading and trailing quotes.
  558. */
  559. private function removeQuotes($str) {
  560. $f = substr($str, 0, 1);
  561. $l = substr($str, -1);
  562. if ($f === $l && ($f == '"' || $f == "'")) {
  563. $str = substr($str, 1, -1);
  564. }
  565. return $str;
  566. }
  567. /**
  568. * Pseudo-class handler for a variety of jQuery pseudo-classes.
  569. * Handles lt, gt, eq, nth, first, last pseudo-classes.
  570. */
  571. private function getByPosition($operator, $pos) {
  572. $matches = $this->candidateList();
  573. $found = new SplObjectStorage();
  574. if ($matches->count() == 0) {
  575. return;
  576. }
  577. switch ($operator) {
  578. case 'nth':
  579. case 'eq':
  580. if ($matches->count() >= $pos) {
  581. //$found[] = $matches[$pos -1];
  582. foreach ($matches as $match) {
  583. // CSS is 1-based, so we pre-increment.
  584. if ($matches->key() + 1 == $pos) {
  585. $found->attach($match);
  586. break;
  587. }
  588. }
  589. }
  590. break;
  591. case 'first':
  592. if ($matches->count() > 0) {
  593. $matches->rewind(); // This is necessary to init.
  594. $found->attach($matches->current());
  595. }
  596. break;
  597. case 'last':
  598. if ($matches->count() > 0) {
  599. // Spin through iterator.
  600. foreach ($matches as $item) {};
  601. $found->attach($item);
  602. }
  603. break;
  604. // case 'even':
  605. // for ($i = 1; $i <= count($matches); ++$i) {
  606. // if ($i % 2 == 0) {
  607. // $found[] = $matches[$i];
  608. // }
  609. // }
  610. // break;
  611. // case 'odd':
  612. // for ($i = 1; $i <= count($matches); ++$i) {
  613. // if ($i % 2 == 0) {
  614. // $found[] = $matches[$i];
  615. // }
  616. // }
  617. // break;
  618. case 'lt':
  619. $i = 0;
  620. foreach ($matches as $item) {
  621. if (++$i < $pos) {
  622. $found->attach($item);
  623. }
  624. }
  625. break;
  626. case 'gt':
  627. $i = 0;
  628. foreach ($matches as $item) {
  629. if (++$i > $pos) {
  630. $found->attach($item);
  631. }
  632. }
  633. break;
  634. }
  635. $this->matches = $found;
  636. }
  637. /**
  638. * Parse an an+b rule for CSS pseudo-classes.
  639. * @param $rule
  640. * Some rule in the an+b format.
  641. * @return
  642. * Array (list($aVal, $bVal)) of the two values.
  643. * @throws CssParseException
  644. * If the rule does not follow conventions.
  645. */
  646. protected function parseAnB($rule) {
  647. if ($rule == 'even') {
  648. return array(2, 0);
  649. }
  650. elseif ($rule == 'odd') {
  651. return array(2, 1);
  652. }
  653. elseif ($rule == 'n') {
  654. return array(1, 0);
  655. }
  656. elseif (is_numeric($rule)) {
  657. return array(0, (int)$rule);
  658. }
  659. $rule = explode('n', $rule);
  660. if (count($rule) == 0) {
  661. throw new CssParseException("nth-child value is invalid.");
  662. }
  663. // Each of these is legal: 1, -1, and -. '-' is shorthand for -1.
  664. $aVal = trim($rule[0]);
  665. $aVal = ($aVal == '-') ? -1 : (int)$aVal;
  666. $bVal = !empty($rule[1]) ? (int)trim($rule[1]) : 0;
  667. return array($aVal, $bVal);
  668. }
  669. /**
  670. * Pseudo-class handler for nth-child and all related pseudo-classes.
  671. *
  672. * @param int $groupSize
  673. * The size of the group (in an+b, this is a).
  674. * @param int $elementInGroup
  675. * The offset in a group. (in an+b this is b).
  676. * @param boolean $lastChild
  677. * Whether counting should begin with the last child. By default, this is false.
  678. * Pseudo-classes that start with the last-child can set this to true.
  679. */
  680. protected function nthChild($groupSize, $elementInGroup, $lastChild = FALSE) {
  681. // EXPERIMENTAL: New in Quark. This should be substantially faster
  682. // than the old (jQuery-ish) version. It still has E_STRICT violations
  683. // though.
  684. $parents = new SplObjectStorage();
  685. $matches = new SplObjectStorage();
  686. $i = 0;
  687. foreach ($this->matches as $item) {
  688. $parent = $item->parentNode;
  689. // Build up an array of all of children of this parent, and store the
  690. // index of each element for reference later. We only need to do this
  691. // once per parent, though.
  692. if (!$parents->contains($parent)) {
  693. $c = 0;
  694. foreach ($parent->childNodes as $child) {
  695. // We only want nodes, and if this call is preceded by an element
  696. // selector, we only want to match elements with the same tag name.
  697. // !!! This last part is a grey area in the CSS 3 Selector spec. It seems
  698. // necessary to make the implementation match the examples in the spec. However,
  699. // jQuery 1.2 does not do this.
  700. if ($child->nodeType == XML_ELEMENT_NODE && ($this->findAnyElement || $child->tagName == $item->tagName)) {
  701. // This may break E_STRICT.
  702. $child->nodeIndex = ++$c;
  703. }
  704. }
  705. // This may break E_STRICT.
  706. $parent->numElements = $c;
  707. $parents->attach($parent);
  708. }
  709. // If we are looking for the last child, we count from the end of a list.
  710. // Note that we add 1 because CSS indices begin at 1, not 0.
  711. if ($lastChild) {
  712. $indexToMatch = $item->parentNode->numElements - $item->nodeIndex + 1;
  713. }
  714. // Otherwise we count from the beginning of the list.
  715. else {
  716. $indexToMatch = $item->nodeIndex;
  717. }
  718. // If group size is 0, then we return element at the right index.
  719. if ($groupSize == 0) {
  720. if ($indexToMatch == $elementInGroup)
  721. $matches->attach($item);
  722. }
  723. // If group size != 0, then we grab nth element from group offset by
  724. // element in group.
  725. else {
  726. if (($indexToMatch - $elementInGroup) % $groupSize == 0
  727. && ($indexToMatch - $elementInGroup) / $groupSize >= 0) {
  728. $matches->attach($item);
  729. }
  730. }
  731. // Iterate.
  732. ++$i;
  733. }
  734. $this->matches = $matches;
  735. }
  736. /**
  737. * Reverse a set of matches.
  738. *
  739. * This is now necessary because internal matches are no longer represented
  740. * as arrays.
  741. * @since QueryPath 2.0
  742. *//*
  743. private function reverseMatches() {
  744. // Reverse the candidate list. There must be a better way of doing
  745. // this.
  746. $arr = array();
  747. foreach ($this->matches as $m) array_unshift($arr, $m);
  748. $this->found = new SplObjectStorage();
  749. foreach ($arr as $item) $this->found->attach($item);
  750. }*/
  751. /**
  752. * Pseudo-class handler for :nth-last-child and related pseudo-classes.
  753. */
  754. protected function nthLastChild($groupSize, $elementInGroup) {
  755. // New in Quark.
  756. $this->nthChild($groupSize, $elementInGroup, TRUE);
  757. }
  758. /**
  759. * Get a list of peer elements.
  760. * If $requireSameTag is TRUE, then only peer elements with the same
  761. * tagname as the given element will be returned.
  762. *
  763. * @param $element
  764. * A DomElement.
  765. * @param $requireSameTag
  766. * Boolean flag indicating whether all matches should have the same
  767. * element name (tagName) as $element.
  768. * @return
  769. * Array of peer elements.
  770. *//*
  771. protected function listPeerElements($element, $requireSameTag = FALSE) {
  772. $peers = array();
  773. $parent = $element->parentNode;
  774. foreach ($parent->childNodes as $node) {
  775. if ($node->nodeType == XML_ELEMENT_NODE) {
  776. if ($requireSameTag) {
  777. // Need to make sure that the tag matches:
  778. if ($element->tagName == $node->tagName) {
  779. $peers[] = $node;
  780. }
  781. }
  782. else {
  783. $peers[] = $node;
  784. }
  785. }
  786. }
  787. return $peers;
  788. }
  789. */
  790. /**
  791. * Get the nth child (by index) from matching candidates.
  792. *
  793. * This is used by pseudo-class handlers.
  794. */
  795. /*
  796. protected function childAtIndex($index, $tagName = NULL) {
  797. $restrictToElement = !$this->findAnyElement;
  798. $matches = $this->candidateList();
  799. $defaultTagName = $tagName;
  800. // XXX: Added in Quark: I believe this should return an empty
  801. // match set if no child was found tat the index.
  802. $this->matches = new SplObjectStorage();
  803. foreach ($matches as $item) {
  804. $parent = $item->parentNode;
  805. // If a default tag name is supplied, we always use it.
  806. if (!empty($defaultTagName)) {
  807. $tagName = $defaultTagName;
  808. }
  809. // If we are inside of an element selector, we use the
  810. // tag name of the given elements.
  811. elseif ($restrictToElement) {
  812. $tagName = $item->tagName;
  813. }
  814. // Otherwise, we skip the tag name match.
  815. else {
  816. $tagName = NULL;
  817. }
  818. // Loop through all children looking for matches.
  819. $i = 0;
  820. foreach ($parent->childNodes as $child) {
  821. if ($child->nodeType !== XML_ELEMENT_NODE) {
  822. break; // Skip non-elements
  823. }
  824. // If type is set, then we do type comparison
  825. if (!empty($tagName)) {
  826. // Check whether tag name matches the type.
  827. if ($child->tagName == $tagName) {
  828. // See if this is the index we are looking for.
  829. if ($i == $index) {
  830. //$this->matches = new SplObjectStorage();
  831. $this->matches->attach($child);
  832. return;
  833. }
  834. // If it's not the one we are looking for, increment.
  835. ++$i;
  836. }
  837. }
  838. // We don't care about type. Any tagName will match.
  839. else {
  840. if ($i == $index) {
  841. $this->matches->attach($child);
  842. return;
  843. }
  844. ++$i;
  845. }
  846. } // End foreach
  847. }
  848. }*/
  849. /**
  850. * Pseudo-class handler for nth-of-type-child.
  851. * Not implemented.
  852. */
  853. protected function nthOfTypeChild($groupSize, $elementInGroup, $lastChild) {
  854. // EXPERIMENTAL: New in Quark. This should be substantially faster
  855. // than the old (jQuery-ish) version. It still has E_STRICT violations
  856. // though.
  857. $parents = new SplObjectStorage();
  858. $matches = new SplObjectStorage();
  859. $i = 0;
  860. foreach ($this->matches as $item) {
  861. $parent = $item->parentNode;
  862. // Build up an array of all of children of this parent, and store the
  863. // index of each element for reference later. We only need to do this
  864. // once per parent, though.
  865. if (!$parents->contains($parent)) {
  866. $c = 0;
  867. foreach ($parent->childNodes as $child) {
  868. // This doesn't totally make sense, since the CSS 3 spec does not require that
  869. // this pseudo-class be adjoined to an element (e.g. ' :nth-of-type' is allowed).
  870. if ($child->nodeType == XML_ELEMENT_NODE && $child->tagName == $item->tagName) {
  871. // This may break E_STRICT.
  872. $child->nodeIndex = ++$c;
  873. }
  874. }
  875. // This may break E_STRICT.
  876. $parent->numElements = $c;
  877. $parents->attach($parent);
  878. }
  879. // If we are looking for the last child, we count from the end of a list.
  880. // Note that we add 1 because CSS indices begin at 1, not 0.
  881. if ($lastChild) {
  882. $indexToMatch = $item->parentNode->numElements - $item->nodeIndex + 1;
  883. }
  884. // Otherwise we count from the beginning of the list.
  885. else {
  886. $indexToMatch = $item->nodeIndex;
  887. }
  888. // If group size is 0, then we return element at the right index.
  889. if ($groupSize == 0) {
  890. if ($indexToMatch == $elementInGroup)
  891. $matches->attach($item);
  892. }
  893. // If group size != 0, then we grab nth element from group offset by
  894. // element in group.
  895. else {
  896. if (($indexToMatch - $elementInGroup) % $groupSize == 0
  897. && ($indexToMatch - $elementInGroup) / $groupSize >= 0) {
  898. $matches->attach($item);
  899. }
  900. }
  901. // Iterate.
  902. ++$i;
  903. }
  904. $this->matches = $matches;
  905. }
  906. /**
  907. * Pseudo-class handler for nth-last-of-type-child.
  908. * Not implemented.
  909. */
  910. protected function nthLastOfTypeChild($groupSize, $elementInGroup) {
  911. $this->nthOfTypeChild($groupSize, $elementInGroup, TRUE);
  912. }
  913. /**
  914. * Pseudo-class handler for :lang
  915. */
  916. protected function lang($value) {
  917. // TODO: This checks for cases where an explicit language is
  918. // set. The spec seems to indicate that an element should inherit
  919. // language from the parent... but this is unclear.
  920. $operator = (strpos($value, '-') !== FALSE) ? self::isExactly : self::containsWithHyphen;
  921. $orig = $this->matches;
  922. $origDepth = $this->findAnyElement;
  923. // Do first pass: attributes in default namespace
  924. $this->attribute('lang', $value, $operator);
  925. $lang = $this->matches; // Temp array for merging.
  926. // Reset
  927. $this->matches = $orig;
  928. $this->findAnyElement = $origDepth;
  929. // Do second pass: attributes in 'xml' namespace.
  930. $this->attributeNS('lang', 'xml', $value, $operator);
  931. // Merge results.
  932. // FIXME: Note that we lose natural ordering in
  933. // the document because we search for xml:lang separately
  934. // from lang.
  935. foreach ($this->matches as $added) $lang->attach($added);
  936. $this->matches = $lang;
  937. }
  938. /**
  939. * Pseudo-class handler for :not(filter).
  940. *
  941. * This does not follow the specification in the following way: The CSS 3
  942. * selector spec says the value of not() must be a simple selector. This
  943. * function allows complex selectors.
  944. *
  945. * @param string $filter
  946. * A CSS selector.
  947. */
  948. protected function not($filter) {
  949. $matches = $this->candidateList();
  950. //$found = array();
  951. $found = new SplObjectStorage();
  952. foreach ($matches as $item) {
  953. $handler = new QueryPathCssEventHandler($item);
  954. $not_these = $handler->find($filter)->getMatches();
  955. if ($not_these->count() == 0) {
  956. $found->attach($item);
  957. }
  958. }
  959. // No need to check for unique elements, since the list
  960. // we began from already had no duplicates.
  961. $this->matches = $found;
  962. }
  963. /**
  964. * Pseudo-class handler for :has(filter).
  965. * This can also be used as a general filtering routine.
  966. */
  967. public function has($filter) {
  968. $matches = $this->candidateList();
  969. //$found = array();
  970. $found = new SplObjectStorage();
  971. foreach ($matches as $item) {
  972. $handler = new QueryPathCssEventHandler($item);
  973. $these = $handler->find($filter)->getMatches();
  974. if (count($these) > 0) {
  975. $found->attach($item);
  976. }
  977. }
  978. $this->matches = $found;
  979. return $this;
  980. }
  981. /**
  982. * Pseudo-class handler for :first-of-type.
  983. */
  984. protected function firstOfType() {
  985. $matches = $this->candidateList();
  986. $found = new SplObjectStorage();
  987. foreach ($matches as $item) {
  988. $type = $item->tagName;
  989. $parent = $item->parentNode;
  990. foreach ($parent->childNodes as $kid) {
  991. if ($kid->nodeType == XML_ELEMENT_NODE && $kid->tagName == $type) {
  992. if (!$found->contains($kid)) {
  993. $found->attach($kid);
  994. }
  995. break;
  996. }
  997. }
  998. }
  999. $this->matches = $found;
  1000. }
  1001. /**
  1002. * Pseudo-class handler for :last-of-type.
  1003. */
  1004. protected function lastOfType() {
  1005. $matches = $this->candidateList();
  1006. $found = new SplObjectStorage();
  1007. foreach ($matches as $item) {
  1008. $type = $item->tagName;
  1009. $parent = $item->parentNode;
  1010. for ($i = $parent->childNodes->length - 1; $i >= 0; --$i) {
  1011. $kid = $parent->childNodes->item($i);
  1012. if ($kid->nodeType == XML_ELEMENT_NODE && $kid->tagName == $type) {
  1013. if (!$found->contains($kid)) {
  1014. $found->attach($kid);
  1015. }
  1016. break;
  1017. }
  1018. }
  1019. }
  1020. $this->matches = $found;
  1021. }
  1022. /**
  1023. * Pseudo-class handler for :only-child.
  1024. */
  1025. protected function onlyChild() {
  1026. $matches = $this->candidateList();
  1027. $found = new SplObjectStorage();
  1028. foreach($matches as $item) {
  1029. $parent = $item->parentNode;
  1030. $kids = array();
  1031. foreach($parent->childNodes as $kid) {
  1032. if ($kid->nodeType == XML_ELEMENT_NODE) {
  1033. $kids[] = $kid;
  1034. }
  1035. }
  1036. // There should be only one child element, and
  1037. // it should be the one being tested.
  1038. if (count($kids) == 1 && $kids[0] === $item) {
  1039. $found->attach($kids[0]);
  1040. }
  1041. }
  1042. $this->matches = $found;
  1043. }
  1044. /**
  1045. * Pseudo-class handler for :empty.
  1046. */
  1047. protected function emptyElement() {
  1048. $found = new SplObjectStorage();
  1049. $matches = $this->candidateList();
  1050. foreach ($matches as $item) {
  1051. $empty = TRUE;
  1052. foreach($item->childNodes as $kid) {
  1053. // From the spec: Elements and Text nodes are the only ones to
  1054. // affect emptiness.
  1055. if ($kid->nodeType == XML_ELEMENT_NODE || $kid->nodeType == XML_TEXT_NODE) {
  1056. $empty = FALSE;
  1057. break;
  1058. }
  1059. }
  1060. if ($empty) {
  1061. $found->attach($item);
  1062. }
  1063. }
  1064. $this->matches = $found;
  1065. }
  1066. /**
  1067. * Pseudo-class handler for :only-of-type.
  1068. */
  1069. protected function onlyOfType() {
  1070. $matches = $this->candidateList();
  1071. $found = new SplObjectStorage();
  1072. foreach ($matches as $item) {
  1073. if (!$item->parentNode) {
  1074. $this->matches = new SplObjectStorage();
  1075. }
  1076. $parent = $item->parentNode;
  1077. $onlyOfType = TRUE;
  1078. // See if any peers are of the same type
  1079. foreach($parent->childNodes as $kid) {
  1080. if ($kid->nodeType == XML_ELEMENT_NODE
  1081. && $kid->tagName == $item->tagName
  1082. && $kid !== $item) {
  1083. //$this->matches = new SplObjectStorage();
  1084. $onlyOfType = FALSE;
  1085. break;
  1086. }
  1087. }
  1088. // If no others were found, attach this one.
  1089. if ($onlyOfType) $found->attach($item);
  1090. }
  1091. $this->matches = $found;
  1092. }
  1093. /**
  1094. * Check for attr value matches based on an operation.
  1095. */
  1096. protected function attrValMatches($needle, $haystack, $operation) {
  1097. if (strlen($haystack) < strlen($needle)) return FALSE;
  1098. // According to the spec:
  1099. // "The case-sensitivity of attribute names in selectors depends on the document language."
  1100. // (6.3.2)
  1101. // To which I say, "huh?". We assume case sensitivity.
  1102. switch ($operation) {
  1103. case CssEventHandler::isExactly:
  1104. return $needle == $haystack;
  1105. case CssEventHandler::containsWithSpace:
  1106. return in_array($needle, explode(' ', $haystack));
  1107. case CssEventHandler::containsWithHyphen:
  1108. return in_array($needle, explode('-', $haystack));
  1109. case CssEventHandler::containsInString:
  1110. return strpos($haystack, $needle) !== FALSE;
  1111. case CssEventHandler::beginsWith:
  1112. return strpos($haystack, $needle) === 0;
  1113. case CssEventHandler::endsWith:
  1114. //return strrpos($haystack, $needle) === strlen($needle) - 1;
  1115. return preg_match('/' . $needle . '$/', $haystack) == 1;
  1116. }
  1117. return FALSE; // Shouldn't be able to get here.
  1118. }
  1119. /**
  1120. * As the spec mentions, these must be at the end of a selector or
  1121. * else they will cause errors. Most selectors return elements. Pseudo-elements
  1122. * do not.
  1123. */
  1124. public function pseudoElement($name) {
  1125. // process the pseudoElement
  1126. switch ($name) {
  1127. // XXX: Should this return an array -- first line of
  1128. // each of the matched elements?
  1129. case 'first-line':
  1130. $matches = $this->candidateList();
  1131. $found = new SplObjectStorage();
  1132. $o = new stdClass();
  1133. foreach ($matches as $item) {
  1134. $str = $item->textContent;
  1135. $lines = explode("\n", $str);
  1136. if (!empty($lines)) {
  1137. $line = trim($lines[0]);
  1138. if (!empty($line))
  1139. $o->textContent = $line;
  1140. $found->attach($o);//trim($lines[0]);
  1141. }
  1142. }
  1143. $this->matches = $found;
  1144. break;
  1145. // XXX: Should this return an array -- first letter of each
  1146. // of the matched elements?
  1147. case 'first-letter':
  1148. $matches = $this->candidateList();
  1149. $found = new SplObjectStorage();
  1150. $o = new stdClass();
  1151. foreach ($matches as $item) {
  1152. $str = $item->textContent;
  1153. if (!empty($str)) {
  1154. $str = substr($str,0, 1);
  1155. $o->textContent = $str;
  1156. $found->attach($o);
  1157. }
  1158. }
  1159. $this->matches = $found;
  1160. break;
  1161. case 'before':
  1162. case 'after':
  1163. // There is nothing in a DOM to return for the before and after
  1164. // selectors.
  1165. case 'selection':
  1166. // With no user agent, we don't have a concept of user selection.
  1167. throw new NotImplementedException("The $name pseudo-element is not implemented.");
  1168. break;
  1169. }
  1170. $this->findAnyElement = FALSE;
  1171. }
  1172. public function directDescendant() {
  1173. $this->findAnyElement = FALSE;
  1174. $kids = new SplObjectStorage();
  1175. foreach ($this->matches as $item) {
  1176. $kidsNL = $item->childNodes;
  1177. foreach ($kidsNL as $kidNode) {
  1178. if ($kidNode->nodeType == XML_ELEMENT_NODE) {
  1179. $kids->attach($kidNode);
  1180. }
  1181. }
  1182. }
  1183. $this->matches = $kids;
  1184. }
  1185. /**
  1186. * For an element to be adjacent to another, it must be THE NEXT NODE
  1187. * in the node list. So if an element is surrounded by pcdata, there are
  1188. * no adjacent nodes. E.g. in <a/>FOO<b/>, the a and b elements are not
  1189. * adjacent.
  1190. *
  1191. * In a strict DOM parser, line breaks and empty spaces are nodes. That means
  1192. * nodes like this will not be adjacent: <test/> <test/>. The space between
  1193. * them makes them non-adjacent. If this is not the desired behavior, pass
  1194. * in the appropriate flags to your parser. Example:
  1195. * <code>
  1196. * $doc = new DomDocument();
  1197. * $doc->loadXML('<test/> <test/>', LIBXML_NOBLANKS);
  1198. * </code>
  1199. */
  1200. public function adjacent() {
  1201. $this->findAnyElement = FALSE;
  1202. // List of nodes that are immediately adjacent to the current one.
  1203. //$found = array();
  1204. $found = new SplObjectStorage();
  1205. foreach ($this->matches as $item) {
  1206. while (isset($item->nextSibling)) {
  1207. if (isset($item->nextSibling) && $item->nextSibling->nodeType === XML_ELEMENT_NODE) {
  1208. $found->attach($item->nextSibling);
  1209. break;
  1210. }
  1211. $item = $item->nextSibling;
  1212. }
  1213. }
  1214. $this->matches = $found;
  1215. }
  1216. public function anotherSelector() {
  1217. $this->findAnyElement = FALSE;
  1218. // Copy old matches into buffer.
  1219. if ($this->matches->count() > 0) {
  1220. //$this->alreadyMatched = array_merge($this->alreadyMatched, $this->matches);
  1221. foreach ($this->matches as $item) $this->alreadyMatched->attach($item);
  1222. }
  1223. // Start over at the top of the tree.
  1224. $this->findAnyElement = TRUE; // Reset depth flag.
  1225. $this->matches = new SplObjectStorage();
  1226. $this->matches->attach($this->dom);
  1227. }
  1228. /**
  1229. * Get all nodes that are siblings to currently selected nodes.
  1230. *
  1231. * If two passed in items are siblings of each other, neither will
  1232. * be included in the list of siblings. Their status as being candidates
  1233. * excludes them from being considered siblings.
  1234. */
  1235. public function sibling() {
  1236. $this->findAnyElement = FALSE;
  1237. // Get the nodes at the same level.
  1238. if ($this->matches->count() > 0) {
  1239. $sibs = new SplObjectStorage();
  1240. foreach ($this->matches as $item) {
  1241. /*$candidates = $item->parentNode->childNodes;
  1242. foreach ($candidates as $candidate) {
  1243. if ($candidate->nodeType === XML_ELEMENT_NODE && $candidate !== $item) {
  1244. $sibs->attach($candidate);
  1245. }
  1246. }
  1247. */
  1248. while ($item->nextSibling != NULL) {
  1249. $item = $item->nextSibling;
  1250. if ($item->nodeType === XML_ELEMENT_NODE) $sibs->attach($item);
  1251. }
  1252. }
  1253. $this->matches = $sibs;
  1254. }
  1255. }
  1256. /**
  1257. * Get any descendant.
  1258. */
  1259. public function anyDescendant() {
  1260. // Get children:
  1261. $found = new SplObjectStorage();
  1262. foreach ($this->matches as $item) {
  1263. $kids = $item->getElementsByTagName('*');
  1264. //$found = array_merge($found, $this->nodeListToArray($kids));
  1265. $this->attachNodeList($kids, $found);
  1266. }
  1267. $this->matches = $found;
  1268. // Set depth flag:
  1269. $this->findAnyElement = TRUE;
  1270. }
  1271. /**
  1272. * Determine what candidates are in the current scope.
  1273. *
  1274. * This is a utility method that gets the list of elements
  1275. * that should be evaluated in the context. If $this->findAnyElement
  1276. * is TRUE, this will return a list of every element that appears in
  1277. * the subtree of $this->matches. Otherwise, it will just return
  1278. * $this->matches.
  1279. */
  1280. private function candidateList() {
  1281. if ($this->findAnyElement) {
  1282. return $this->getAllCandidates($this->matches);
  1283. }
  1284. return $this->matches;
  1285. }
  1286. /**
  1287. * Get a list of all of the candidate elements.
  1288. *
  1289. * This is used when $this->findAnyElement is TRUE.
  1290. * @param $elements
  1291. * A list of current elements (usually $this->matches).
  1292. *
  1293. * @return
  1294. * A list of all candidate elements.
  1295. */
  1296. private function getAllCandidates($elements) {
  1297. $found = new SplObjectStorage();
  1298. foreach ($elements as $item) {
  1299. $found->attach($item); // put self in
  1300. $nl = $item->getElementsByTagName('*');
  1301. //foreach ($nl as $node) $found[] = $node;
  1302. $this->attachNodeList($nl, $found);
  1303. }
  1304. return $found;
  1305. }
  1306. /*
  1307. public function nodeListToArray($nodeList) {
  1308. $array = array();
  1309. foreach ($nodeList as $node) {
  1310. if ($node->nodeType == XML_ELEMENT_NODE) {
  1311. $array[] = $node;
  1312. }
  1313. }
  1314. return $array;
  1315. }
  1316. */
  1317. /**
  1318. * Attach all nodes in a node list to the given SplObjectStorage.
  1319. */
  1320. public function attachNodeList(DOMNodeList $nodeList, SplObjectStorage $splos) {
  1321. foreach ($nodeList as $item) $splos->attach($item);
  1322. }
  1323. }
  1324. /**
  1325. * Exception thrown for unimplemented CSS.
  1326. *
  1327. * This is thrown in cases where some feature is expected, but the current
  1328. * implementation does not support that feature.
  1329. *
  1330. * @ingroup querypath_css
  1331. */
  1332. class NotImplementedException extends Exception {}