aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/third_party/closure/goog/dojo/dom/query_test.js
blob: 6dd664e3cee975a548bdfa04fc4f716ac37f91c8 (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
goog.require('goog.dom');
goog.require('goog.dom.query');
goog.require('goog.testing.asserts');  // assertThrows

goog.setTestOnly('query_test');

function testBasicSelectors() {
  assertQuery(4, 'h3');
  assertQuery(1, 'h1:first-child');
  assertQuery(2, 'h3:first-child');
  assertQuery(1, '#t');
  assertQuery(1, '#bug');
  assertQuery(4, '#t h3');
  assertQuery(1, 'div#t');
  assertQuery(4, 'div#t h3');
  assertQuery(0, 'span#t');
  assertQuery(1, '#t div > h3');
  assertQuery(2, '.foo');
  assertQuery(1, '.foo.bar');
  assertQuery(2, '.baz');
  assertQuery(3, '#t > h3');
}

function testSyntacticEquivalents() {
  // syntactic equivalents
  assertQuery(12, '#t > *');
  assertQuery(12, '#t >');
  assertQuery(3, '.foo > *');
  assertQuery(3, '.foo >');
}

function testWithARootById() {
  // with a root, by ID
  assertQuery(3, '> *', 'container');
  assertQuery(3, '> h3', 't');
}

function testCompoundQueries() {
  // compound queries
  assertQuery(2, '.foo, .bar');
  assertQuery(2, '.foo,.bar');
}

function testMultipleClassAttributes() {
  // multiple class attribute
  assertQuery(1, '.foo.bar');
  assertQuery(2, '.foo');
  assertQuery(2, '.baz');
}

function testCaseSensitivity() {
  // case sensitivity
  assertQuery(1, 'span.baz');
  assertQuery(1, 'sPaN.baz');
  assertQuery(1, 'SPAN.baz');
  assertQuery(1, '[class = \"foo bar\"]');
  assertQuery(2, '[foo~=\"bar\"]');
  assertQuery(2, '[ foo ~= \"bar\" ]');
}

function testAttributes() {
  assertQuery(3, '[foo]');
  assertQuery(1, '[foo$=\"thud\"]');
  assertQuery(1, '[foo$=thud]');
  assertQuery(1, '[foo$=\"thudish\"]');
  assertQuery(1, '#t [foo$=thud]');
  assertQuery(1, '#t [ title $= thud ]');
  assertQuery(0, '#t span[ title $= thud ]');
  assertQuery(2, '[foo|=\"bar\"]');
  assertQuery(1, '[foo|=\"bar-baz\"]');
  assertQuery(0, '[foo|=\"baz\"]');
}

function testDescendantSelectors() {
  assertQuery(3, '>', 'container');
  assertQuery(3, '> *', 'container');
  assertQuery(2, '> [qux]', 'container');
  assertEquals('child1', goog.dom.query('> [qux]', 'container')[0].id);
  assertEquals('child3', goog.dom.query('> [qux]', 'container')[1].id);
  assertQuery(3, '>', 'container');
  assertQuery(3, '> *', 'container');
}

function testSiblingSelectors() {
  assertQuery(1, '+', 'container');
  assertQuery(3, '~', 'container');
  assertQuery(1, '.foo + span');
  assertQuery(4, '.foo ~ span');
  assertQuery(1, '#foo ~ *');
  assertQuery(1, '#foo ~');
}

function testSubSelectors() {
  // sub-selector parsing
  assertQuery(1, '#t span.foo:not(span:first-child)');
  assertQuery(1, '#t span.foo:not(:first-child)');
}

function testNthChild() {
  assertEquals(goog.dom.$('_foo'), goog.dom.query('.foo:nth-child(2)')[0]);
  assertQuery(2, '#t > h3:nth-child(odd)');
  assertQuery(3, '#t h3:nth-child(odd)');
  assertQuery(3, '#t h3:nth-child(2n+1)');
  assertQuery(1, '#t h3:nth-child(even)');
  assertQuery(1, '#t h3:nth-child(2n)');
  assertQuery(1, '#t h3:nth-child(2n+3)');
  assertQuery(2, '#t h3:nth-child(1)');
  assertQuery(1, '#t > h3:nth-child(1)');
  assertQuery(3, '#t :nth-child(3)');
  assertQuery(0, '#t > div:nth-child(1)');
  assertQuery(7, '#t span');
  assertQuery(3, '#t > *:nth-child(n+10)');
  assertQuery(1, '#t > *:nth-child(n+12)');
  assertQuery(10, '#t > *:nth-child(-n+10)');
  assertQuery(5, '#t > *:nth-child(-2n+10)');
  assertQuery(6, '#t > *:nth-child(2n+2)');
  assertQuery(5, '#t > *:nth-child(2n+4)');
  assertQuery(5, '#t > *:nth-child(2n+4)');
  assertQuery(12, '#t > *:nth-child(n-5)');
  assertQuery(6, '#t > *:nth-child(2n-5)');
}

function testEmptyPseudoSelector() {
  assertQuery(4, '#t > span:empty');
  assertQuery(6, '#t span:empty');
  assertQuery(0, 'h3 span:empty');
  assertQuery(1, 'h3 :not(:empty)');
}

function testIdsWithColons(){
  assertQuery(1, "#silly\\:id\\:\\:with\\:colons");
}

function testOrder() {
  var els = goog.dom.query('.myupperclass .myclass input');
  assertEquals('myid1', els[0].id);
  assertEquals('myid2', els[1].id);
}

function testCorrectDocumentInFrame() {
  var frameDocument = window.frames['ifr'].document;
  frameDocument.body.innerHTML =
      document.getElementById('iframe-test').innerHTML;

  var els = goog.dom.query('#if1 .if2 div', document);
  var frameEls = goog.dom.query('#if1 .if2 div', frameDocument);

  assertEquals(els.length, frameEls.length);
  assertEquals(1, frameEls.length);
  assertNotEquals(document.getElementById('if3'),
                  frameDocument.getElementById('if3'));
}

/**
 * @param {number} expectedNumberOfNodes
 * @param {...*} var_args
 */
function assertQuery(expectedNumberOfNodes, var_args) {
  var args = Array.prototype.slice.call(arguments, 1);
  assertEquals(expectedNumberOfNodes,
               goog.dom.query.apply(null, args).length);
}