summaryrefslogtreecommitdiff
path: root/lib/querypath/test/Tests/QueryPath/CSS/DOMTraverserTest.php
blob: 95424823604ad62033506e0ced2cbbb0df00c388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
 * @file
 * CSS Event handling tests
 */
namespace QueryPath\Tests;

require_once __DIR__ . '/../TestCase.php';

use \QueryPath\CSS\Token;
use \QueryPath\CSS\DOMTraverser;
use \QueryPath\CSS\Parser;
use \QueryPath\CSS\EventHandler;

define('TRAVERSER_XML', __DIR__ . '/../../../DOMTraverserTest.xml');

/**
 * @ingroup querypath_tests
 * @group CSS
 */
class DOMTraverserTest extends TestCase {

  protected $xml_file = TRAVERSER_XML;
  public function debug($msg) {
    fwrite(STDOUT, PHP_EOL . $msg);
  }

  public function testConstructor() {
    $dom = new \DOMDocument('1.0');
    $dom->load($this->xml_file);

    $splos = new \SPLObjectStorage();
    $splos->attach($dom);

    $traverser = new DOMTraverser($splos);

    $this->assertInstanceOf('\QueryPath\CSS\Traverser', $traverser);
    $this->assertInstanceOf('\QueryPath\CSS\DOMTraverser', $traverser);
  }

  protected function traverser() {
    $dom = new \DOMDocument('1.0');
    $dom->load($this->xml_file);

    $splos = new \SPLObjectStorage();
    $splos->attach($dom->documentElement);

    $traverser = new DOMTraverser($splos);

    return $traverser;
  }

  protected function find($selector) {
    return $this->traverser()->find($selector)->matches();
  }

  public function testFind() {
    $res = $this->traverser()->find('root');

    // Ensure that return contract is not violated.
    $this->assertInstanceOf('\QueryPath\CSS\Traverser', $res);
  }

  public function testMatches() {
    $res = $this->traverser()->matches();
    $this->assertEquals(1, count($res));
  }

  public function testMatchElement() {
    // Canary: If element does not exist, must return FALSE.
    $matches = $this->find('NO_SUCH_ELEMENT');
    $this->assertEquals(0, count($matches));

    // Test without namespace
    $matches = $this->find('root');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('crowded');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('outside');
    $this->assertEquals(3, count($matches));

    // Check nested elements.
    $matches = $this->find('a');
    $this->assertEquals(3, count($matches));

    // Test wildcard.
    $traverser = $this->traverser();
    $matches = $traverser->find('*')->matches();
    $actual= $traverser->getDocument()->getElementsByTagName('*');
    $this->assertEquals($actual->length, count($matches));


    // Test with namespace
    //$this->markTestIncomplete();
    $matches = $this->find('ns_test');
    $this->assertEquals(3, count($matches));

    $matches = $this->find('test|ns_test');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('test|ns_test>ns_attr');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('*|ns_test');
    $this->assertEquals(3, count($matches));

    $matches = $this->find('test|*');
    $this->assertEquals(1, count($matches));

    // Test where namespace is declared on the element.
    $matches = $this->find('newns|my_element');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('test|ns_test>newns|my_element');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('test|*>newns|my_element');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('*|ns_test>newns|my_element');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('*|*>newns|my_element');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('*>newns|my_element');
    $this->assertEquals(1, count($matches));
  }

  public function testMatchAttributes() {

    $matches = $this->find('crowded[attr1]');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('crowded[attr1=one]');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('crowded[attr2^=tw]');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('classtest[class~=two]');
    $this->assertEquals(1, count($matches));
    $matches = $this->find('classtest[class~=one]');
    $this->assertEquals(1, count($matches));
    $matches = $this->find('classtest[class~=seven]');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('crowded[attr0]');
    $this->assertEquals(0, count($matches));

    $matches = $this->find('[level=1]');
    $this->assertEquals(3, count($matches));

    $matches = $this->find('[attr1]');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('[test|myattr]');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('[test|myattr=foo]');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('[*|myattr=foo]');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('[|myattr=foo]');
    $this->assertEquals(0, count($matches));

    $matches = $this->find('[|level=1]');
    $this->assertEquals(3, count($matches));

    $matches = $this->find('[*|level=1]');
    $this->assertEquals(4, count($matches));

    // Test namespace on attr where namespace
    // is declared on that element
    $matches = $this->find('[nuther|ping]');
    $this->assertEquals(1, count($matches));

    // Test multiple namespaces on an element.
    $matches = $this->find('[*|ping=3]');
    $this->assertEquals(1, count($matches));

    // Test multiple namespaces on an element.
    $matches = $this->find('[*|ping]');
    $this->assertEquals(1, count($matches));
  }

  public function testMatchId() {
    $matches = $this->find('idtest#idtest-one');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('#idtest-one');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('outter#fake');
    $this->assertEquals(0, count($matches));

    $matches = $this->find('#fake');
    $this->assertEquals(0, count($matches));
  }

  public function testMatchClasses() {
    // Basic test.
    $matches = $this->find('a.a1');
    $this->assertEquals(1, count($matches));

    // Count multiple.
    $matches = $this->find('.first');
    $this->assertEquals(2, count($matches));

    // Grab one in the middle of a list.
    $matches = $this->find('.four');
    $this->assertEquals(1, count($matches));

    // One element with two classes.
    $matches = $this->find('.three.four');
    $this->assertEquals(1, count($matches));
  }

  public function testMatchPseudoClasses() {

    $matches = $this->find('ul>li:first');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('ul>li:not(.first)');
    $this->assertEquals(5, count($matches));

  }

  public function testMatchPseudoElements() {
    $matches = $this->find('p::first-line');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('p::first-letter');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('p::before');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('p::after');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('bottom::after');
    $this->assertEquals(0, count($matches));
  }

  public function testCombineAdjacent() {
    // Simple test
    $matches = $this->find('idtest + p');
    $this->assertEquals(1, count($matches));
    foreach ($matches as $m) {
      $this->assertEquals('p', $m->tagName);
    }

    // Test ignoring PCDATA
    $matches = $this->find('p + one');
    $this->assertEquals(1, count($matches));
    foreach ($matches as $m) {
      $this->assertEquals('one', $m->tagName);
    }

    // Test that non-adjacent elements don't match.
    $matches = $this->find('idtest + one');
    foreach ($matches as $m) {
      $this->assertEquals('one', $m->tagName);
    }
    $this->assertEquals(0, count($matches), 'Non-adjacents should not match.');

    // Test that elements BEFORE don't match
    $matches = $this->find('one + p');
    foreach ($matches as $m) {
      $this->assertEquals('one', $m->tagName);
    }
    $this->assertEquals(0, count($matches), 'Match only if b is after a');
  }

  public function testCombineSibling() {
    // Canary:
    $matches = $this->find('one ~ two');
    $this->assertEquals(0, count($matches));

    // Canary 2:
    $matches = $this->find('NO_SUCH_ELEMENT ~ two');
    $this->assertEquals(0, count($matches));

    // Simple test
    $matches = $this->find('idtest ~ p');
    $this->assertEquals(1, count($matches));
    foreach ($matches as $m) {
      $this->assertEquals('p', $m->tagName);
    }

    // Simple test
    $matches = $this->find('outside ~ p');
    $this->assertEquals(1, count($matches));
    foreach ($matches as $m) {
      $this->assertEquals('p', $m->tagName);
    }

    // Matches only go left, not right.
    $matches = $this->find('p ~ outside');
    $this->assertEquals(0, count($matches));
  }
  public function testCombineDirectDescendant() {
    // Canary:
    $matches = $this->find('one > four');
    $this->assertEquals(0, count($matches));

    $matches = $this->find('two>three');
    $this->assertEquals(1, count($matches));
    foreach ($matches as $m) {
      $this->assertEquals('three', $m->tagName);
    }

    $matches = $this->find('one > two > three');
    $this->assertEquals(1, count($matches));
    foreach ($matches as $m) {
      $this->assertEquals('three', $m->tagName);
    }

    $matches = $this->find('a>a>a');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('a>a');
    $this->assertEquals(2, count($matches));
  }
  public function testCombineAnyDescendant() {
    // Canary
    $matches = $this->find('four one');
    $this->assertEquals(0, count($matches));

    $matches = $this->find('one two');
    $this->assertEquals(1, count($matches));
    foreach ($matches as $m) {
      $this->assertEquals('two', $m->tagName);
    }

    $matches = $this->find('one four');
    $this->assertEquals(1, count($matches));

    $matches = $this->find('a a');
    $this->assertEquals(2, count($matches));

    $matches = $this->find('root two four');
    $this->assertEquals(1, count($matches));
  }
  public function testMultipleSelectors() {
    // fprintf(STDOUT, "=========TEST=========\n\n");
    $matches = $this->find('one, two');
    $this->assertEquals(2, count($matches));
  }

}