| 1 |
goog.setTestOnly('goog.dom.query_test'); |
| 2 |
goog.provide('goog.dom.query_test'); |
| 3 |
|
| 4 |
goog.require('goog.dom'); |
| 5 |
goog.require('goog.dom.query'); |
| 6 |
goog.require('goog.testing.asserts'); |
| 7 |
goog.require('goog.testing.jsunit'); |
| 8 |
goog.require('goog.userAgent'); |
| 9 |
|
| 10 |
function testBasicSelectors() { |
| 11 |
assertQuery(4, 'h3'); |
| 12 |
assertQuery(1, 'h1:first-child'); |
| 13 |
assertQuery(2, 'h3:first-child'); |
| 14 |
assertQuery(1, '#t'); |
| 15 |
assertQuery(1, '#bug'); |
| 16 |
assertQuery(4, '#t h3'); |
| 17 |
assertQuery(1, 'div#t'); |
| 18 |
assertQuery(4, 'div#t h3'); |
| 19 |
assertQuery(0, 'span#t'); |
| 20 |
assertQuery(1, '#t div > h3'); |
| 21 |
assertQuery(2, '.foo'); |
| 22 |
assertQuery(1, '.foo.bar'); |
| 23 |
assertQuery(2, '.baz'); |
| 24 |
assertQuery(3, '#t > h3'); |
| 25 |
} |
| 26 |
|
| 27 |
function testSyntacticEquivalents() { |
| 28 |
// syntactic equivalents |
| 29 |
assertQuery(12, '#t > *'); |
| 30 |
assertQuery(12, '#t >'); |
| 31 |
assertQuery(3, '.foo > *'); |
| 32 |
assertQuery(3, '.foo >'); |
| 33 |
} |
| 34 |
|
| 35 |
function testWithARootById() { |
| 36 |
// Broken in latest chrome. |
| 37 |
if (goog.userAgent.WEBKIT) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
// with a root, by ID |
| 42 |
assertQuery(3, '> *', 'container'); |
| 43 |
assertQuery(3, '> h3', 't'); |
| 44 |
} |
| 45 |
|
| 46 |
function testCompoundQueries() { |
| 47 |
// compound queries |
| 48 |
assertQuery(2, '.foo, .bar'); |
| 49 |
assertQuery(2, '.foo,.bar'); |
| 50 |
} |
| 51 |
|
| 52 |
function testMultipleClassAttributes() { |
| 53 |
// multiple class attribute |
| 54 |
assertQuery(1, '.foo.bar'); |
| 55 |
assertQuery(2, '.foo'); |
| 56 |
assertQuery(2, '.baz'); |
| 57 |
} |
| 58 |
|
| 59 |
function testCaseSensitivity() { |
| 60 |
// case sensitivity |
| 61 |
assertQuery(1, 'span.baz'); |
| 62 |
assertQuery(1, 'sPaN.baz'); |
| 63 |
assertQuery(1, 'SPAN.baz'); |
| 64 |
assertQuery(1, '[class = \"foo bar\"]'); |
| 65 |
assertQuery(2, '[foo~=\"bar\"]'); |
| 66 |
assertQuery(2, '[ foo ~= \"bar\" ]'); |
| 67 |
} |
| 68 |
|
| 69 |
function testAttributes() { |
| 70 |
assertQuery(3, '[foo]'); |
| 71 |
assertQuery(1, '[foo$=\"thud\"]'); |
| 72 |
assertQuery(1, '[foo$=thud]'); |
| 73 |
assertQuery(1, '[foo$=\"thudish\"]'); |
| 74 |
assertQuery(1, '#t [foo$=thud]'); |
| 75 |
assertQuery(1, '#t [ title $= thud ]'); |
| 76 |
assertQuery(0, '#t span[ title $= thud ]'); |
| 77 |
assertQuery(2, '[foo|=\"bar\"]'); |
| 78 |
assertQuery(1, '[foo|=\"bar-baz\"]'); |
| 79 |
assertQuery(0, '[foo|=\"baz\"]'); |
| 80 |
} |
| 81 |
|
| 82 |
function testDescendantSelectors() { |
| 83 |
|
| 84 |
// Broken in latest chrome. |
| 85 |
if (goog.userAgent.WEBKIT) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
assertQuery(3, '>', 'container'); |
| 90 |
assertQuery(3, '> *', 'container'); |
| 91 |
assertQuery(2, '> [qux]', 'container'); |
| 92 |
assertEquals('child1', goog.dom.query('> [qux]', 'container')[0].id); |
| 93 |
assertEquals('child3', goog.dom.query('> [qux]', 'container')[1].id); |
| 94 |
assertQuery(3, '>', 'container'); |
| 95 |
assertQuery(3, '> *', 'container'); |
| 96 |
} |
| 97 |
|
| 98 |
function testSiblingSelectors() { |
| 99 |
assertQuery(1, '+', 'container'); |
| 100 |
assertQuery(3, '~', 'container'); |
| 101 |
assertQuery(1, '.foo + span'); |
| 102 |
assertQuery(4, '.foo ~ span'); |
| 103 |
assertQuery(1, '#foo ~ *'); |
| 104 |
assertQuery(1, '#foo ~'); |
| 105 |
} |
| 106 |
|
| 107 |
function testSubSelectors() { |
| 108 |
// sub-selector parsing |
| 109 |
assertQuery(1, '#t span.foo:not(span:first-child)'); |
| 110 |
assertQuery(1, '#t span.foo:not(:first-child)'); |
| 111 |
} |
| 112 |
|
| 113 |
function testNthChild() { |
| 114 |
assertEquals(goog.dom.$('_foo'), goog.dom.query('.foo:nth-child(2)')[0]); |
| 115 |
assertQuery(2, '#t > h3:nth-child(odd)'); |
| 116 |
assertQuery(3, '#t h3:nth-child(odd)'); |
| 117 |
assertQuery(3, '#t h3:nth-child(2n+1)'); |
| 118 |
assertQuery(1, '#t h3:nth-child(even)'); |
| 119 |
assertQuery(1, '#t h3:nth-child(2n)'); |
| 120 |
assertQuery(1, '#t h3:nth-child(2n+3)'); |
| 121 |
assertQuery(2, '#t h3:nth-child(1)'); |
| 122 |
assertQuery(1, '#t > h3:nth-child(1)'); |
| 123 |
assertQuery(3, '#t :nth-child(3)'); |
| 124 |
assertQuery(0, '#t > div:nth-child(1)'); |
| 125 |
assertQuery(7, '#t span'); |
| 126 |
assertQuery(3, '#t > *:nth-child(n+10)'); |
| 127 |
assertQuery(1, '#t > *:nth-child(n+12)'); |
| 128 |
assertQuery(10, '#t > *:nth-child(-n+10)'); |
| 129 |
assertQuery(5, '#t > *:nth-child(-2n+10)'); |
| 130 |
assertQuery(6, '#t > *:nth-child(2n+2)'); |
| 131 |
assertQuery(5, '#t > *:nth-child(2n+4)'); |
| 132 |
assertQuery(5, '#t > *:nth-child(2n+4)'); |
| 133 |
assertQuery(12, '#t > *:nth-child(n-5)'); |
| 134 |
assertQuery(6, '#t > *:nth-child(2n-5)'); |
| 135 |
} |
| 136 |
|
| 137 |
function testEmptyPseudoSelector() { |
| 138 |
assertQuery(4, '#t > span:empty'); |
| 139 |
assertQuery(6, '#t span:empty'); |
| 140 |
assertQuery(0, 'h3 span:empty'); |
| 141 |
assertQuery(1, 'h3 :not(:empty)'); |
| 142 |
} |
| 143 |
|
| 144 |
function testIdsWithColons() { |
| 145 |
assertQuery(1, '#silly\\:id\\:\\:with\\:colons'); |
| 146 |
} |
| 147 |
|
| 148 |
function testOrder() { |
| 149 |
var els = goog.dom.query('.myupperclass .myclass input'); |
| 150 |
assertEquals('myid1', els[0].id); |
| 151 |
assertEquals('myid2', els[1].id); |
| 152 |
} |
| 153 |
|
| 154 |
function testCorrectDocumentInFrame() { |
| 155 |
var frameDocument = window.frames['ifr'].document; |
| 156 |
frameDocument.body.innerHTML = |
| 157 |
document.getElementById('iframe-test').innerHTML; |
| 158 |
|
| 159 |
var els = goog.dom.query('#if1 .if2 div', document); |
| 160 |
var frameEls = goog.dom.query('#if1 .if2 div', frameDocument); |
| 161 |
|
| 162 |
assertEquals(els.length, frameEls.length); |
| 163 |
assertEquals(1, frameEls.length); |
| 164 |
assertNotEquals(document.getElementById('if3'), |
| 165 |
frameDocument.getElementById('if3')); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* @param {number} expectedNumberOfNodes |
| 171 |
* @param {...*} var_args |
| 172 |
*/ |
| 173 |
function assertQuery(expectedNumberOfNodes, var_args) { |
| 174 |
var args = Array.prototype.slice.call(arguments, 1); |
| 175 |
assertEquals(expectedNumberOfNodes, |
| 176 |
goog.dom.query.apply(null, args).length); |
| 177 |
} |