summaryrefslogtreecommitdiff
path: root/lib/querypath/test/Tests/QueryPath/CSS/SelectorTest.php
blob: 495f1f58cc1c3d9b416b73484a7a1a2f0159eac3 (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
<?php
namespace QueryPath\Tests;
require_once __DIR__ . '/../TestCase.php';

use \QueryPath\CSS\Selector,
    \QueryPath\CSS\SimpleSelector,
    \QueryPath\CSS\EventHandler;

class SelectorTest extends TestCase {

  protected function parse($selector) {
    $handler = new \QueryPath\CSS\Selector();
    $parser = new \QueryPath\CSS\Parser($selector, $handler);
    $parser->parse();
    return $handler;
  }

  public function testElement() {
    $selector = $this->parse('test')->toArray();

    $this->assertEquals(1, count($selector));
    $this->assertEquals('test', $selector[0]['0']->element);
  }

  public function testElementNS() {
    $selector = $this->parse('foo|test')->toArray();

    $this->assertEquals(1, count($selector));
    $this->assertEquals('test', $selector[0]['0']->element);
    $this->assertEquals('foo', $selector[0]['0']->ns);
  }

  public function testId() {
    $selector = $this->parse('#test')->toArray();

    $this->assertEquals(1, count($selector));
    $this->assertEquals('test', $selector[0][0]->id);
  }

  public function testClasses() {
    $selector = $this->parse('.test')->toArray();

    $this->assertEquals(1, count($selector));
    $this->assertEquals('test', $selector[0][0]->classes[0]);

    $selector = $this->parse('.test.foo.bar')->toArray();
    $this->assertEquals('test', $selector[0][0]->classes[0]);
    $this->assertEquals('foo', $selector[0][0]->classes[1]);
    $this->assertEquals('bar', $selector[0][0]->classes[2]);

  }

  public function testAttributes() {
    $selector = $this->parse('foo[bar=baz]')->toArray();
    $this->assertEquals(1, count($selector));
    $attrs = $selector[0][0]->attributes;

    $this->assertEquals(1, count($attrs));

    $attr = $attrs[0];
    $this->assertEquals('bar', $attr['name']);
    $this->assertEquals(EventHandler::isExactly, $attr['op']);
    $this->assertEquals('baz', $attr['value']);

    $selector = $this->parse('foo[bar=baz][size=one]')->toArray();
    $attrs = $selector[0][0]->attributes;

    $this->assertEquals('one', $attrs[1]['value']);
  }

  public function testAttributesNS() {
    $selector = $this->parse('[myns|foo=bar]')->toArray();

    $attr = $selector[0][0]->attributes[0];

    $this->assertEquals('myns', $attr['ns']);
    $this->assertEquals('foo', $attr['name']);
  }

  public function testPseudoClasses() {
    $selector = $this->parse('foo:first')->toArray();
    $pseudo = $selector[0][0]->pseudoClasses;

    $this->assertEquals(1, count($pseudo));

    $this->assertEquals('first', $pseudo[0]['name']);
  }

  public function testPseudoElements() {
    $selector = $this->parse('foo::bar')->toArray();
    $pseudo = $selector[0][0]->pseudoElements;

    $this->assertEquals(1, count($pseudo));

    $this->assertEquals('bar', $pseudo[0]);
  }

  public function testCombinators() {
    // This implies *>foo
    $selector = $this->parse('>foo')->toArray();

    $this->assertEquals(SimpleSelector::directDescendant, $selector[0][1]->combinator);

    // This will be a selector with three simples:
    // 'bar'
    // 'foo '
    // '*>'
    $selector = $this->parse('>foo bar')->toArray();
    $this->assertNull($selector[0][0]->combinator);
    $this->assertEquals(SimpleSelector::anyDescendant, $selector[0][1]->combinator);
    $this->assertEquals(SimpleSelector::directDescendant, $selector[0][2]->combinator);
  }

  public function testIterator() {
    $selector = $this->parse('foo::bar');

    $iterator = $selector->getIterator();
    $this->assertInstanceOf('\Iterator', $iterator);
  }
}