Skip to content
This repository
Browse code

DOMQuery now acts like jQuery.

  • Loading branch information...
commit 83ded34eaf40d081e3c1dd8f3c30c70fe04b0f23 1 parent a2bfb27
Matt Butcher authored August 30, 2012
228  src/QueryPath/DOMQuery.php
@@ -119,8 +119,19 @@ public function __construct($document = NULL, $string = NULL, $options = array()
119 119
     // Figure out if document is DOM, HTML/XML, or a filename
120 120
     elseif (is_object($document)) {
121 121
 
122  
-      if ($document instanceof DOMQuery) {
123  
-        $this->matches = $document->get(NULL, TRUE);
  122
+      // This is the most frequent object type.
  123
+      if ($document instanceof \SplObjectStorage) {
  124
+        $this->matches = $document;
  125
+        if ($document->count() != 0) {
  126
+          $first = $this->getFirstMatch();
  127
+          if (!empty($first->ownerDocument)) {
  128
+            $this->document = $first->ownerDocument;
  129
+          }
  130
+        }
  131
+      }
  132
+      elseif ($document instanceof DOMQuery) {
  133
+        //$this->matches = $document->get(NULL, TRUE);
  134
+        $this->setMatches($document->get(NULL, TRUE));
124 135
         if ($this->matches->count() > 0)
125 136
           $this->document = $this->getFirstMatch()->ownerDocument;
126 137
       }
@@ -140,13 +151,6 @@ public function __construct($document = NULL, $string = NULL, $options = array()
140 151
         //$this->matches = array($import);
141 152
         $this->setMatches($import);
142 153
       }
143  
-      elseif ($document instanceof \SplObjectStorage) {
144  
-        if ($document->count() == 0) {
145  
-          throw new \QueryPath\Exception('Cannot initialize QueryPath from an empty SplObjectStore');
146  
-        }
147  
-        $this->matches = $document;
148  
-        $this->document = $this->getFirstMatch()->ownerDocument;
149  
-      }
150 154
       else {
151 155
         throw new \QueryPath\Exception('Unsupported class type: ' . get_class($document));
152 156
       }
@@ -184,7 +188,12 @@ public function __construct($document = NULL, $string = NULL, $options = array()
184 188
 
185 189
     // Do a find if the second param was set.
186 190
     if (isset($string) && strlen($string) > 0) {
187  
-      $this->find($string);
  191
+      // We don't issue a find because that creates a new DOMQuery.
  192
+      //$this->find($string);
  193
+
  194
+      $query = new \QueryPath\CSS\DOMTraverser($this->matches);
  195
+      $query->find($string);
  196
+      $this->setMatches($query->matches());
188 197
     }
189 198
   }
190 199
 
@@ -240,8 +249,9 @@ public function getOptions() {
240 249
    *  for the current document.
241 250
    */
242 251
   public function top($selector = NULL) {
243  
-    $this->setMatches($this->document->documentElement);
244  
-    return !empty($selector) ? $this->find($selector) : $this;
  252
+    //$this->setMatches($this->document->documentElement);
  253
+    //return !empty($selector) ? $this->find($selector) : $this;
  254
+    return $this->inst($this->document->documentElement, $selector, $this->options);
245 255
   }
246 256
 
247 257
   /**
@@ -264,6 +274,13 @@ public function find($selector) {
264 274
     //$query = new QueryPathEventHandler($this->matches);
265 275
     $query = new \QueryPath\CSS\DOMTraverser($this->matches);
266 276
     $query->find($selector);
  277
+    //$this->setMatches($query->matches());
  278
+    //return $this;
  279
+    return $this->inst($query->matches(), NULL , $this->options);
  280
+  }
  281
+  public function findInPlace($selector) {
  282
+    $query = new \QueryPath\CSS\DOMTraverser($this->matches);
  283
+    $query->find($selector);
267 284
     $this->setMatches($query->matches());
268 285
     return $this;
269 286
   }
@@ -309,8 +326,9 @@ public function xpath($query, $options = array()) {
309 326
         for ($i = 0; $i < $nl->length; ++$i) $found->attach($nl->item($i));
310 327
       }
311 328
     }
312  
-    $this->setMatches($found);
313  
-    return $this;
  329
+    return $this->inst($found, NULL, $this->options);
  330
+    //$this->setMatches($found);
  331
+    //return $this;
314 332
   }
315 333
 
316 334
   /**
@@ -693,9 +711,10 @@ public function removeAttr($name) {
693 711
    * @see end()
694 712
    */
695 713
   public function eq($index) {
  714
+    return $this->inst($this->getNthMatch($index), NULL, $this->options);
696 715
     // XXX: Might there be a more efficient way of doing this?
697  
-    $this->setMatches($this->getNthMatch($index));
698  
-    return $this;
  716
+    //$this->setMatches($this->getNthMatch($index));
  717
+    //return $this;
699 718
   }
700 719
   /**
701 720
    * Given a selector, this checks to see if the current set has one or more matches.
@@ -737,6 +756,7 @@ public function is($selector) {
737 756
     }
738 757
 
739 758
     // Testing based on Issue #70.
  759
+    //fprintf(STDOUT, __FUNCTION__  .' found %d', $this->find($selector)->count());
740 760
     return $this->branch($selector)->count() > 0;
741 761
 
742 762
     // Old version:
@@ -765,9 +785,16 @@ public function is($selector) {
765 785
    */
766 786
   public function filter($selector) {
767 787
     $found = new \SplObjectStorage();
768  
-    foreach ($this->matches as $m) if (QueryPath::with($m, NULL, $this->options)->is($selector)) $found->attach($m);
769  
-    $this->setMatches($found);
770  
-    return $this;
  788
+    foreach ($this->matches as $m) {
  789
+      if (QueryPath::with($m, NULL, $this->options)->is($selector)) {
  790
+        //fprintf(STDOUT, 'Attaching  %s for %s', $m->tagName, $selector);
  791
+        $found->attach($m);
  792
+      }
  793
+    }
  794
+
  795
+    return $this->inst($found, NULL, $this->options);
  796
+    //$this->setMatches($found);
  797
+    //return $this;
771 798
   }
772 799
   /**
773 800
    * Sort the contents of the QueryPath object.
@@ -844,7 +871,7 @@ public function sort($comparator, $modifyDOM = FALSE) {
844 871
     foreach ($list as $node) {
845 872
       $found->attach($node);
846 873
     }
847  
-    $this->setMatches($found);
  874
+    //$this->setMatches($found);
848 875
 
849 876
 
850 877
     // Do DOM modifications only if necessary.
@@ -860,7 +887,7 @@ public function sort($comparator, $modifyDOM = FALSE) {
860 887
       $placeholder->parentNode->removeChild($placeholder);
861 888
     }
862 889
 
863  
-    return $this;
  890
+    return $this->inst($found, NULL, $this->options);
864 891
   }
865 892
   /**
866 893
    * Filter based on a lambda function.
@@ -895,8 +922,7 @@ public function filterLambda($fn) {
895 922
     foreach ($this->matches as $item)
896 923
       if ($function($i++, $item) !== FALSE) $found->attach($item);
897 924
 
898  
-    $this->setMatches($found);
899  
-    return $this;
  925
+    return $this->inst($found, NULL, $this->options);
900 926
   }
901 927
 
902 928
   /**
@@ -942,9 +968,7 @@ public function filterPreg($regex) {
942 968
         $found->attach($item);
943 969
       }
944 970
     }
945  
-    $this->setMatches($found);
946  
-
947  
-    return $this;
  971
+    return $this->inst($found, NULL, $this->options);
948 972
   }
949 973
   /**
950 974
    * Filter based on a callback function.
@@ -983,8 +1007,7 @@ public function filterCallback($callback) {
983 1007
     else {
984 1008
       throw new \QueryPath\Exception('The specified callback is not callable.');
985 1009
     }
986  
-    $this->setMatches($found);
987  
-    return $this;
  1010
+    return $this->inst($found, NULL, $this->options);
988 1011
   }
989 1012
   /**
990 1013
    * Filter a list to contain only items that do NOT match.
@@ -1012,8 +1035,7 @@ public function not($selector) {
1012 1035
     else {
1013 1036
       foreach ($this->matches as $m) if (!QueryPath::with($m, NULL, $this->options)->is($selector)) $found->attach($m);
1014 1037
     }
1015  
-    $this->setMatches($found);
1016  
-    return $this;
  1038
+    return $this->inst($found, NULL, $this->options);
1017 1039
   }
1018 1040
   /**
1019 1041
    * Get an item's index.
@@ -1102,8 +1124,7 @@ public function map($callback) {
1102 1124
     else {
1103 1125
       throw new \QueryPath\Exception('Callback is not callable.');
1104 1126
     }
1105  
-    $this->setMatches($found, FALSE);
1106  
-    return $this;
  1127
+    return $this->inst($found, NULL, $this->options);
1107 1128
   }
1108 1129
   /**
1109 1130
    * Narrow the items in this object down to only a slice of the starting items.
@@ -1121,8 +1142,7 @@ public function slice($start, $length = 0) {
1121 1142
     $end = $length;
1122 1143
     $found = new \SplObjectStorage();
1123 1144
     if ($start >= $this->size()) {
1124  
-      $this->setMatches($found);
1125  
-      return $this;
  1145
+      return $this->inst($found, NULL, $this->options);
1126 1146
     }
1127 1147
 
1128 1148
     $i = $j = 0;
@@ -1137,8 +1157,7 @@ public function slice($start, $length = 0) {
1137 1157
       ++$i;
1138 1158
     }
1139 1159
 
1140  
-    $this->setMatches($found);
1141  
-    return $this;
  1160
+    return $this->inst($found, NULL, $this->options);
1142 1161
   }
1143 1162
   /**
1144 1163
    * Run a callback on each item in the list of items.
@@ -1440,8 +1459,7 @@ public function replaceWith($new) {
1440 1459
       $parent->insertBefore($data->cloneNode(TRUE), $m);
1441 1460
       $found->attach($parent->removeChild($m));
1442 1461
     }
1443  
-    $this->setMatches($found);
1444  
-    return $this;
  1462
+    return $this->inst($found, NULL, $this->options);
1445 1463
   }
1446 1464
   /**
1447 1465
    * Remove the parent element from the selected node or nodes.
@@ -1671,8 +1689,7 @@ public function deepest() {
1671 1689
           $winner->attach($lele);
1672 1690
       }
1673 1691
     }
1674  
-    $this->setMatches($winner);
1675  
-    return $this;
  1692
+    return $this->inst($winner, NULL, $this->options);
1676 1693
   }
1677 1694
 
1678 1695
   /**
@@ -1895,6 +1912,8 @@ public function add($selector) {
1895 1912
   /**
1896 1913
    * Revert to the previous set of matches.
1897 1914
    *
  1915
+   * <b>DEPRECATED</b> Do not use.
  1916
+   *
1898 1917
    * This will revert back to the last set of matches (before the last
1899 1918
    * "destructive" set of operations). This undoes any change made to the set of
1900 1919
    * matched objects. Functions like find() and filter() change the
@@ -1924,12 +1943,13 @@ public function add($selector) {
1924 1943
    *  operation.
1925 1944
    * @see andSelf()
1926 1945
    * @see add()
  1946
+   * @deprecated This function will be removed.
1927 1947
    */
1928 1948
   public function end() {
1929 1949
     // Note that this does not use setMatches because it must set the previous
1930 1950
     // set of matches to empty array.
1931  
-    $this->matches = $this->last;
1932  
-    $this->last = new \SplObjectStorage();
  1951
+    //$this->matches = $this->last;
  1952
+    //$this->last = new \SplObjectStorage();
1933 1953
     return $this;
1934 1954
   }
1935 1955
   /**
@@ -2002,14 +2022,11 @@ public function children($selector = NULL) {
2002 2022
         if ($c->nodeType == XML_ELEMENT_NODE) $found->attach($c);
2003 2023
       }
2004 2024
     }
2005  
-    if (empty($selector)) {
2006  
-      $this->setMatches($found);
2007  
-    }
2008  
-    else {
2009  
-      $this->matches = $found; // Don't buffer this. It is temporary.
2010  
-      $this->filter($selector);
  2025
+    $new = $this->inst($found, NULL, $this->options);
  2026
+    if (!empty($selector)) {
  2027
+      return $new->filter($selector);
2011 2028
     }
2012  
-    return $this;
  2029
+    return $new;
2013 2030
   }
2014 2031
   /**
2015 2032
    * Get all child nodes (not just elements) of all items in the matched set.
@@ -2037,8 +2054,7 @@ public function contents() {
2037 2054
         $found->attach($c);
2038 2055
       }
2039 2056
     }
2040  
-    $this->setMatches($found);
2041  
-    return $this;
  2057
+    return $this->inst($found, NULL, $this->options);
2042 2058
   }
2043 2059
   /**
2044 2060
    * Get a list of siblings for elements currently wrapped by this object.
@@ -2073,13 +2089,11 @@ public function siblings($selector = NULL) {
2073 2089
       }
2074 2090
     }
2075 2091
     if (empty($selector)) {
2076  
-      $this->setMatches($found);
  2092
+      return $this->inst($found, NULL, $this->options);
2077 2093
     }
2078 2094
     else {
2079  
-      $this->matches = $found; // Don't buffer this. It is temporary.
2080  
-      $this->filter($selector);
  2095
+      return $this->inst($found, NULL, $this->options)->filter($selector);
2081 2096
     }
2082  
-    return $this;
2083 2097
   }
2084 2098
   /**
2085 2099
    * Find the closest element matching the selector.
@@ -2114,8 +2128,10 @@ public function closest($selector) {
2114 2128
       }
2115 2129
 
2116 2130
     }
2117  
-    $this->setMatches($found);
2118  
-    return $this;
  2131
+    // XXX: Should this be an in-place modification?
  2132
+    return $this->inst($found, NULL, $this->options);
  2133
+    //$this->setMatches($found);
  2134
+    //return $this;
2119 2135
   }
2120 2136
   /**
2121 2137
    * Get the immediate parent of each element in the DOMQuery.
@@ -2151,8 +2167,7 @@ public function parent($selector = NULL) {
2151 2167
         }
2152 2168
       }
2153 2169
     }
2154  
-    $this->setMatches($found);
2155  
-    return $this;
  2170
+    return $this->inst($found, NULL, $this->options);
2156 2171
   }
2157 2172
   /**
2158 2173
    * Get all ancestors of each element in the DOMQuery.
@@ -2183,8 +2198,7 @@ public function parents($selector = NULL) {
2183 2198
         }
2184 2199
       }
2185 2200
     }
2186  
-    $this->setMatches($found);
2187  
-    return $this;
  2201
+    return $this->inst($found, NULL, $this->options);
2188 2202
   }
2189 2203
   /**
2190 2204
    * Set or get the markup for an element.
@@ -2789,8 +2803,7 @@ public function next($selector = NULL) {
2789 2803
         }
2790 2804
       }
2791 2805
     }
2792  
-    $this->setMatches($found);
2793  
-    return $this;
  2806
+    return $this->inst($found, NULL, $this->options);
2794 2807
   }
2795 2808
   /**
2796 2809
    * Get all siblings after an element.
@@ -2825,8 +2838,7 @@ public function nextAll($selector = NULL) {
2825 2838
         }
2826 2839
       }
2827 2840
     }
2828  
-    $this->setMatches($found);
2829  
-    return $this;
  2841
+    return $this->inst($found, NULL, $this->options);
2830 2842
   }
2831 2843
   /**
2832 2844
    * Get the next sibling before each element in the DOMQuery.
@@ -2864,8 +2876,7 @@ public function prev($selector = NULL) {
2864 2876
         }
2865 2877
       }
2866 2878
     }
2867  
-    $this->setMatches($found);
2868  
-    return $this;
  2879
+    return $this->inst($found, NULL, $this->options);
2869 2880
   }
2870 2881
   /**
2871 2882
    * Get the previous siblings for each element in the DOMQuery.
@@ -2900,30 +2911,7 @@ public function prevAll($selector = NULL) {
2900 2911
         }
2901 2912
       }
2902 2913
     }
2903  
-    $this->setMatches($found);
2904  
-    return $this;
2905  
-  }
2906  
-  /**
2907  
-   * @deprecated Use {@link siblings()}.
2908  
-   */
2909  
-  public function peers($selector = NULL) {
2910  
-    $found = new \SplObjectStorage();
2911  
-    foreach ($this->matches as $m) {
2912  
-      foreach ($m->parentNode->childNodes as $kid) {
2913  
-        if ($kid->nodeType == XML_ELEMENT_NODE && $m !== $kid) {
2914  
-          if (!empty($selector)) {
2915  
-            if (QueryPath::with($kid, NULL, $this->options)->is($selector)) {
2916  
-              $found->attach($kid);
2917  
-            }
2918  
-          }
2919  
-          else {
2920  
-            $found->attach($kid);
2921  
-          }
2922  
-        }
2923  
-      }
2924  
-    }
2925  
-    $this->setMatches($found);
2926  
-    return $this;
  2914
+    return $this->inst($found, NULL, $this->options);
2927 2915
   }
2928 2916
   /**
2929 2917
    * Add a class to all elements in the current DOMQuery.
@@ -3100,12 +3088,24 @@ public function hasClass($class) {
3100 3088
    */
3101 3089
   public function branch($selector = NULL) {
3102 3090
     $temp = \QueryPath::with($this->matches, NULL, $this->options);
3103  
-    if (isset($selector)) $temp->find($selector);
  3091
+    //if (isset($selector)) $temp->find($selector);
  3092
+    $temp->document = $this->document;
  3093
+    if (isset($selector)) $temp->findInPlace($selector);
  3094
+    return $temp;
  3095
+  }
  3096
+  protected function inst($matches, $selector, $options) {
  3097
+    $temp = \QueryPath::with($matches, NULL, $options);
  3098
+    //if (isset($selector)) $temp->find($selector);
  3099
+    $temp->document = $this->document;
  3100
+    if (isset($selector)) $temp->findInPlace($selector);
3104 3101
     return $temp;
3105 3102
   }
3106 3103
   /**
3107 3104
    * Perform a deep clone of each node in the DOMQuery.
3108 3105
    *
  3106
+   * @attention
  3107
+   *   This is an in-place modification of the current QueryPath object.
  3108
+   *
3109 3109
    * This does not clone the DOMQuery object, but instead clones the
3110 3110
    * list of nodes wrapped by the DOMQuery. Every element is deeply
3111 3111
    * cloned.
@@ -3120,7 +3120,8 @@ public function branch($selector = NULL) {
3120 3120
   public function cloneAll() {
3121 3121
     $found = new \SplObjectStorage();
3122 3122
     foreach ($this->matches as $m) $found->attach($m->cloneNode(TRUE));
3123  
-    $this->setMatches($found, FALSE);
  3123
+    //return $this->inst($found, NULL, $this->options);
  3124
+    $this->setMatches($found);
3124 3125
     return $this;
3125 3126
   }
3126 3127
 
@@ -3142,7 +3143,7 @@ public function __clone() {
3142 3143
   /**
3143 3144
    * Detach any items from the list if they match the selector.
3144 3145
    *
3145  
-   * In other words, each item that matches the selector will be remove
  3146
+   * In other words, each item that matches the selector will be removed
3146 3147
    * from the DOM document. The returned DOMQuery wraps the list of
3147 3148
    * removed elements.
3148 3149
    *
@@ -3171,8 +3172,7 @@ public function detach($selector = NULL) {
3171 3172
       // the one passed in, so we have to re-store it.
3172 3173
       $found->attach($item->parentNode->removeChild($item));
3173 3174
     }
3174  
-    $this->setMatches($found);
3175  
-    return $this;
  3175
+    return $this->inst($found, NULL, $this->options);
3176 3176
   }
3177 3177
 
3178 3178
   /**
@@ -3247,8 +3247,7 @@ public function has($contained) {
3247 3247
       }
3248 3248
     }
3249 3249
 
3250  
-    $this->setMatches($found);
3251  
-    return $this;
  3250
+    return $this->inst($found, NULL, $this->options);
3252 3251
   }
3253 3252
 
3254 3253
   /**
@@ -3292,9 +3291,7 @@ public function even() {
3292 3291
       if ($even && $m->nodeType == XML_ELEMENT_NODE) $found->attach($m);
3293 3292
       $even = ($even) ? false : true;
3294 3293
     }
3295  
-    $this->setMatches($found);
3296  
-    $this->matches = $found; // Don't buffer this. It is temporary.
3297  
-    return $this;
  3294
+    return $this->inst($found, NULL, $this->options);
3298 3295
   }
3299 3296
 
3300 3297
   /**
@@ -3319,9 +3316,7 @@ public function odd() {
3319 3316
       if ($odd && $m->nodeType == XML_ELEMENT_NODE) $found->attach($m);
3320 3317
       $odd = ($odd) ? false : true;
3321 3318
     }
3322  
-    $this->setMatches($found);
3323  
-    $this->matches = $found; // Don't buffer this. It is temporary.
3324  
-    return $this;
  3319
+    return $this->inst($found, NULL, $this->options);
3325 3320
   }
3326 3321
 
3327 3322
   /**
@@ -3343,9 +3338,7 @@ public function first() {
3343 3338
         break;
3344 3339
       }
3345 3340
     }
3346  
-    $this->setMatches($found);
3347  
-    $this->matches = $found; // Don't buffer this. It is temporary.
3348  
-    return $this;
  3341
+    return $this->inst($found, NULL, $this->options);
3349 3342
   }
3350 3343
 
3351 3344
   /**
@@ -3373,9 +3366,7 @@ public function firstChild() {
3373 3366
       }
3374 3367
       if($flag) break;
3375 3368
     }
3376  
-    $this->setMatches($found);
3377  
-    $this->matches = $found; // Don't buffer this. It is temporary.
3378  
-    return $this;
  3369
+    return $this->inst($found, NULL, $this->options);
3379 3370
   }
3380 3371
 
3381 3372
   /**
@@ -3400,9 +3391,7 @@ public function last() {
3400 3391
     if ($item) {
3401 3392
       $found->attach($item);
3402 3393
     }
3403  
-    $this->setMatches($found);
3404  
-    $this->matches = $found; // Don't buffer this. It is temporary.
3405  
-    return $this;
  3394
+    return $this->inst($found, NULL, $this->options);
3406 3395
   }
3407 3396
 
3408 3397
   /**
@@ -3430,9 +3419,7 @@ public function lastChild() {
3430 3419
         $item = null;
3431 3420
       }
3432 3421
     }
3433  
-    $this->setMatches($found);
3434  
-    $this->matches = $found; // Don't buffer this. It is temporary.
3435  
-    return $this;
  3422
+    return $this->inst($found, NULL, $this->options);
3436 3423
   }
3437 3424
 
3438 3425
   /**
@@ -3473,8 +3460,7 @@ public function nextUntil($selector = NULL) {
3473 3460
         }
3474 3461
       }
3475 3462
     }
3476  
-    $this->setMatches($found);
3477  
-    return $this;
  3463
+    return $this->inst($found, NULL, $this->options);
3478 3464
   }
3479 3465
 
3480 3466
   /**
@@ -3509,8 +3495,7 @@ public function prevUntil($selector = NULL) {
3509 3495
         }
3510 3496
       }
3511 3497
     }
3512  
-    $this->setMatches($found);
3513  
-    return $this;
  3498
+    return $this->inst($found, NULL, $this->options);
3514 3499
   }
3515 3500
 
3516 3501
   /**
@@ -3546,8 +3531,7 @@ public function parentsUntil($selector = NULL) {
3546 3531
         }
3547 3532
       }
3548 3533
     }
3549  
-    $this->setMatches($found);
3550  
-    return $this;
  3534
+    return $this->inst($found, NULL, $this->options);
3551 3535
   }
3552 3536
 
3553 3537
   /////// INTERNAL FUNCTIONS ////////
57  test/Tests/QueryPath/DOMQueryTest.php
@@ -322,6 +322,24 @@ public function testFind() {
322 322
     $this->assertEquals(1, count($qp->get()), 'Check class.');
323 323
     $this->assertEquals($qp->get(0)->tagName, 'root');
324 324
   }
  325
+  public function testFindInPlace() {
  326
+    $file = DATA_FILE;
  327
+    $qp = qp($file)->find('#head');
  328
+    $this->assertEquals(1, count($qp->get()));
  329
+    $this->assertEquals($qp->get(0)->tagName, 'head');
  330
+
  331
+    $this->assertEquals('inner', qp($file)->find('.innerClass')->tag());
  332
+
  333
+    $string = '<?xml version="1.0"?><root><a/>Test</root>';
  334
+    $qp = qp($string)->find('root');
  335
+    $this->assertEquals(1, count($qp->get()), 'Check tag.');
  336
+    $this->assertEquals($qp->get(0)->tagName, 'root');
  337
+
  338
+    $string = '<?xml version="1.0"?><root class="findme">Test</root>';
  339
+    $qp = qp($string)->find('.findme');
  340
+    $this->assertEquals(1, count($qp->get()), 'Check class.');
  341
+    $this->assertEquals($qp->get(0)->tagName, 'root');
  342
+  }
325 343
 
326 344
   /**
327 345
    * @group basic
@@ -658,8 +676,10 @@ public function testAppend() {
658 676
     $file = DATA_FILE;
659 677
     $this->assertEquals(1, qp($file,'unary')->append('<test/>')->find(':root > unary > test')->size());
660 678
     $qp = qp($file,'#inner-one')->append('<li id="appended"/>');
661  
-    $this->assertEquals(1, $qp->find('#appended')->size());
662  
-    $this->assertNull($qp->get(0)->nextSibling);
  679
+
  680
+    $appended = $qp->find('#appended');
  681
+    $this->assertEquals(1, $appended->size());
  682
+    $this->assertNull($appended->get(0)->nextSibling);
663 683
 
664 684
     $this->assertEquals(2, qp($file, 'inner')->append('<test/>')->top()->find('test')->size());
665 685
     $this->assertEquals(2, qp($file, 'inner')->append(qp('<?xml version="1.0"?><test/>'))->top()->find('test')->size());
@@ -916,8 +936,8 @@ public function testAdd() {
916 936
   }
917 937
 
918 938
   public function testEnd() {
919  
-    $file = DATA_FILE;
920  
-    $this->assertEquals(2, qp($file, 'inner')->find('li')->end()->size());
  939
+    //$file = DATA_FILE;
  940
+    //$this->assertEquals(2, qp($file, 'inner')->find('li')->end()->size());
921 941
   }
922 942
 
923 943
   public function testAndSelf() {
@@ -928,6 +948,9 @@ public function testAndSelf() {
928 948
   public function testChildren() {
929 949
     $file = DATA_FILE;
930 950
     $this->assertEquals(5, qp($file, 'inner')->children()->size());
  951
+    foreach (qp($file, 'inner')->children('li') as $kid) {
  952
+      $this->assertEquals('li', $kid->tag());
  953
+    }
931 954
     $this->assertEquals(5, qp($file, 'inner')->children('li')->size());
932 955
     $this->assertEquals(1, qp($file, ':root')->children('unary')->size());
933 956
   }
@@ -952,7 +975,8 @@ public function testContents() {
952 975
         </div>
953 976
       </div>
954 977
     </body></html>';
955  
-    $this->assertEquals(7, count($this->contentsRecurse(qp($xml))));
  978
+    $cr = $this->contentsRecurse(qp($xml));
  979
+    $this->assertEquals(7, count($cr), implode("\n", $cr));
956 980
   }
957 981
 
958 982
   /**
@@ -960,8 +984,9 @@ public function testContents() {
960 984
    * Based on problem reported in issue 51.
961 985
    */
962 986
   private function contentsRecurse($source, &$pack = array()) {
  987
+    static $i = 0;
963 988
     $children = $source->contents();
964  
-    $pack[] = $source->html();
  989
+    $pack[] = ++$i . ' "' . $children->html() . '" ';
965 990
 
966 991
     foreach ($children as $child) {
967 992
       $pack += $this->contentsRecurse($child, $pack);
@@ -1355,7 +1380,7 @@ public function testNext() {
1355 1380
 
1356 1381
     // Regression test for issue eabrand identified:
1357 1382
 
1358  
-    $qp = qp(\QueryPath::HTML_STUB)->append('<div></div><p>Hello</p><p>Goodbye</p>')
  1383
+    $qp = qp(\QueryPath::HTML_STUB, 'body')->append('<div></div><p>Hello</p><p>Goodbye</p>')
1359 1384
       ->children('p')
1360 1385
       ->after('<p>new paragraph</p>');
1361 1386
 
@@ -1363,10 +1388,13 @@ public function testNext() {
1363 1388
 
1364 1389
     //throw new Exception($qp->top()->xml());
1365 1390
 
1366  
-    $this->assertEquals('Hello', $qp->top('p:first-of-type')->text(), "Test First P ");
  1391
+    $qp = $qp->top('p:first-of-type');
  1392
+    $this->assertEquals('Hello', $qp->text(), "Test First P " . $qp->top()->html());
1367 1393
     $i = 0;
1368 1394
     while($qp->next('p')->html() != null) {
1369  
-      $this->assertEquals($testarray[$i], $qp->text(), $i." didn't match");
  1395
+      $qp = $qp->next('p');
  1396
+      $this->assertEquals(1, count($qp));
  1397
+      $this->assertEquals($testarray[$i], $qp->text(), $i . " didn't match " . $qp->top()->xml() );
1370 1398
       $i++;
1371 1399
     }
1372 1400
     $this->assertEquals(3, $i);
@@ -1390,11 +1418,6 @@ public function testPrevAll() {
1390 1418
     $this->assertEquals(3, qp($file, '#four')->prevAll()->size());
1391 1419
     $this->assertEquals(2, qp($file, 'foot')->prevAll('inner')->size());
1392 1420
   }
1393  
-  public function testPeers() {
1394  
-    $file = DATA_FILE;
1395  
-    $this->assertEquals(3, qp($file, '#two')->peers()->size());
1396  
-    $this->assertEquals(2, qp($file, 'foot')->peers('inner')->size());
1397  
-  }
1398 1421
   public function testParent() {
1399 1422
     $file = DATA_FILE;
1400 1423
     $this->assertEquals('root', qp($file, 'unary')->parent()->tag());
@@ -1469,7 +1492,7 @@ public function test__clone() {
1469 1492
     $qp = qp($file, 'inner:first-of-type');
1470 1493
     $qp2 = clone $qp;
1471 1494
     $this->assertFalse($qp === $qp2);
1472  
-    $qp2->find('li')->attr('foo', 'bar');
  1495
+    $qp2->findInPlace('li')->attr('foo', 'bar');
1473 1496
     $this->assertEquals('', $qp->find('li')->attr('foo'));
1474 1497
     $this->assertEquals('bar', $qp2->attr('foo'), $qp2->top()->xml());
1475 1498
   }
@@ -1484,7 +1507,7 @@ public function testIterator() {
1484 1507
 
1485 1508
     $this->assertEquals(4, $qp->find('li')->size());
1486 1509
     $i = 0;
1487  
-    foreach ($qp as $li) {
  1510
+    foreach ($qp->find('li') as $li) {
1488 1511
       ++$i;
1489 1512
       $li->text('foo');
1490 1513
     }
@@ -1599,7 +1622,7 @@ public function testHas() {
1599 1622
     // Test with DOMNode object
1600 1623
     $qp = qp($file, 'foot');
1601 1624
     $selector = $qp->get(0);
1602  
-    $qp->top('root')->has($selector);
  1625
+    $qp = $qp->top('root')->has($selector);
1603 1626
 
1604 1627
     // This should have one element named 'root'.
1605 1628
     $this->assertEquals(1, $qp->size(), 'One element is a parent of foot');

0 notes on commit 83ded34

Please sign in to comment.
Something went wrong with that request. Please try again.