query_test.js 4.8 KB

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