classes_test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Shared code for classes_test.html & classes_quirks_test.html.
  16. */
  17. goog.provide('goog.dom.classes_test');
  18. goog.setTestOnly('goog.dom.classes_test');
  19. goog.require('goog.dom');
  20. goog.require('goog.dom.TagName');
  21. goog.require('goog.dom.classes');
  22. goog.require('goog.testing.jsunit');
  23. var classes = goog.dom.classes;
  24. function testGet() {
  25. var el = goog.dom.createElement(goog.dom.TagName.DIV);
  26. assertArrayEquals([], goog.dom.classes.get(el));
  27. el.className = 'C';
  28. assertArrayEquals(['C'], goog.dom.classes.get(el));
  29. el.className = 'C D';
  30. assertArrayEquals(['C', 'D'], goog.dom.classes.get(el));
  31. el.className = 'C\nD';
  32. assertArrayEquals(['C', 'D'], goog.dom.classes.get(el));
  33. el.className = ' C ';
  34. assertArrayEquals(['C'], goog.dom.classes.get(el));
  35. }
  36. function testSetAddHasRemove() {
  37. var el = goog.dom.getElement('p1');
  38. classes.set(el, 'SOMECLASS');
  39. assertTrue('Should have SOMECLASS', classes.has(el, 'SOMECLASS'));
  40. classes.set(el, 'OTHERCLASS');
  41. assertTrue('Should have OTHERCLASS', classes.has(el, 'OTHERCLASS'));
  42. assertFalse('Should not have SOMECLASS', classes.has(el, 'SOMECLASS'));
  43. classes.add(el, 'WOOCLASS');
  44. assertTrue('Should have OTHERCLASS', classes.has(el, 'OTHERCLASS'));
  45. assertTrue('Should have WOOCLASS', classes.has(el, 'WOOCLASS'));
  46. classes.add(el, 'ACLASS', 'BCLASS', 'CCLASS');
  47. assertTrue('Should have OTHERCLASS', classes.has(el, 'OTHERCLASS'));
  48. assertTrue('Should have WOOCLASS', classes.has(el, 'WOOCLASS'));
  49. assertTrue('Should have ACLASS', classes.has(el, 'ACLASS'));
  50. assertTrue('Should have BCLASS', classes.has(el, 'BCLASS'));
  51. assertTrue('Should have CCLASS', classes.has(el, 'CCLASS'));
  52. classes.remove(el, 'CCLASS');
  53. assertTrue('Should have OTHERCLASS', classes.has(el, 'OTHERCLASS'));
  54. assertTrue('Should have WOOCLASS', classes.has(el, 'WOOCLASS'));
  55. assertTrue('Should have ACLASS', classes.has(el, 'ACLASS'));
  56. assertTrue('Should have BCLASS', classes.has(el, 'BCLASS'));
  57. assertFalse('Should not have CCLASS', classes.has(el, 'CCLASS'));
  58. classes.remove(el, 'ACLASS', 'BCLASS');
  59. assertTrue('Should have OTHERCLASS', classes.has(el, 'OTHERCLASS'));
  60. assertTrue('Should have WOOCLASS', classes.has(el, 'WOOCLASS'));
  61. assertFalse('Should not have ACLASS', classes.has(el, 'ACLASS'));
  62. assertFalse('Should not have BCLASS', classes.has(el, 'BCLASS'));
  63. }
  64. // While support for this isn't implied in the method documentation,
  65. // this is a frequently used pattern.
  66. function testAddWithSpacesInClassName() {
  67. var el = goog.dom.getElement('p1');
  68. classes.add(el, 'CLASS1 CLASS2', 'CLASS3 CLASS4');
  69. assertTrue('Should have CLASS1', classes.has(el, 'CLASS1'));
  70. assertTrue('Should have CLASS2', classes.has(el, 'CLASS2'));
  71. assertTrue('Should have CLASS3', classes.has(el, 'CLASS3'));
  72. assertTrue('Should have CLASS4', classes.has(el, 'CLASS4'));
  73. }
  74. function testSwap() {
  75. var el = goog.dom.getElement('p1');
  76. classes.set(el, 'SOMECLASS FIRST');
  77. assertTrue('Should have FIRST class', classes.has(el, 'FIRST'));
  78. assertTrue('Should have FIRST class', classes.has(el, 'SOMECLASS'));
  79. assertFalse('Should not have second class', classes.has(el, 'second'));
  80. classes.swap(el, 'FIRST', 'second');
  81. assertFalse('Should not have FIRST class', classes.has(el, 'FIRST'));
  82. assertTrue('Should have FIRST class', classes.has(el, 'SOMECLASS'));
  83. assertTrue('Should have second class', classes.has(el, 'second'));
  84. classes.swap(el, 'second', 'FIRST');
  85. assertTrue('Should have FIRST class', classes.has(el, 'FIRST'));
  86. assertTrue('Should have FIRST class', classes.has(el, 'SOMECLASS'));
  87. assertFalse('Should not have second class', classes.has(el, 'second'));
  88. }
  89. function testEnable() {
  90. var el = goog.dom.getElement('p1');
  91. classes.set(el, 'SOMECLASS FIRST');
  92. assertTrue('Should have FIRST class', classes.has(el, 'FIRST'));
  93. assertTrue('Should have SOMECLASS class', classes.has(el, 'SOMECLASS'));
  94. classes.enable(el, 'FIRST', false);
  95. assertFalse('Should not have FIRST class', classes.has(el, 'FIRST'));
  96. assertTrue('Should have SOMECLASS class', classes.has(el, 'SOMECLASS'));
  97. classes.enable(el, 'FIRST', true);
  98. assertTrue('Should have FIRST class', classes.has(el, 'FIRST'));
  99. assertTrue('Should have SOMECLASS class', classes.has(el, 'SOMECLASS'));
  100. }
  101. function testToggle() {
  102. var el = goog.dom.getElement('p1');
  103. classes.set(el, 'SOMECLASS FIRST');
  104. assertTrue('Should have FIRST class', classes.has(el, 'FIRST'));
  105. assertTrue('Should have SOMECLASS class', classes.has(el, 'SOMECLASS'));
  106. classes.toggle(el, 'FIRST');
  107. assertFalse('Should not have FIRST class', classes.has(el, 'FIRST'));
  108. assertTrue('Should have SOMECLASS class', classes.has(el, 'SOMECLASS'));
  109. classes.toggle(el, 'FIRST');
  110. assertTrue('Should have FIRST class', classes.has(el, 'FIRST'));
  111. assertTrue('Should have SOMECLASS class', classes.has(el, 'SOMECLASS'));
  112. }
  113. function testAddNotAddingMultiples() {
  114. var el = goog.dom.getElement('span6');
  115. assertTrue(classes.add(el, 'A'));
  116. assertEquals('A', el.className);
  117. assertFalse(classes.add(el, 'A'));
  118. assertEquals('A', el.className);
  119. assertFalse(classes.add(el, 'B', 'B'));
  120. assertEquals('A B', el.className);
  121. }
  122. function testAddRemoveString() {
  123. var el = goog.dom.getElement('span6');
  124. el.className = 'A';
  125. goog.dom.classes.addRemove(el, 'A', 'B');
  126. assertEquals('B', el.className);
  127. goog.dom.classes.addRemove(el, null, 'C');
  128. assertEquals('B C', el.className);
  129. goog.dom.classes.addRemove(el, 'C', 'D');
  130. assertEquals('B D', el.className);
  131. goog.dom.classes.addRemove(el, 'D', null);
  132. assertEquals('B', el.className);
  133. }
  134. function testAddRemoveArray() {
  135. var el = goog.dom.getElement('span6');
  136. el.className = 'A';
  137. goog.dom.classes.addRemove(el, ['A'], ['B']);
  138. assertEquals('B', el.className);
  139. goog.dom.classes.addRemove(el, [], ['C']);
  140. assertEquals('B C', el.className);
  141. goog.dom.classes.addRemove(el, ['C'], ['D']);
  142. assertEquals('B D', el.className);
  143. goog.dom.classes.addRemove(el, ['D'], []);
  144. assertEquals('B', el.className);
  145. }
  146. function testAddRemoveMultiple() {
  147. var el = goog.dom.getElement('span6');
  148. el.className = 'A';
  149. goog.dom.classes.addRemove(el, ['A'], ['B', 'C', 'D']);
  150. assertEquals('B C D', el.className);
  151. goog.dom.classes.addRemove(el, [], ['E', 'F']);
  152. assertEquals('B C D E F', el.className);
  153. goog.dom.classes.addRemove(el, ['C', 'E'], []);
  154. assertEquals('B D F', el.className);
  155. goog.dom.classes.addRemove(el, ['B'], ['G']);
  156. assertEquals('D F G', el.className);
  157. }
  158. // While support for this isn't implied in the method documentation,
  159. // this is a frequently used pattern.
  160. function testAddRemoveWithSpacesInClassName() {
  161. var el = goog.dom.getElement('p1');
  162. classes.addRemove(el, '', 'CLASS1 CLASS2');
  163. assertTrue('Should have CLASS1', classes.has(el, 'CLASS1'));
  164. assertTrue('Should have CLASS2', classes.has(el, 'CLASS2'));
  165. }
  166. function testHasWithNewlines() {
  167. var el = goog.dom.getElement('p3');
  168. assertTrue('Should have SOMECLASS', classes.has(el, 'SOMECLASS'));
  169. assertTrue('Should also have OTHERCLASS', classes.has(el, 'OTHERCLASS'));
  170. assertFalse('Should not have WEIRDCLASS', classes.has(el, 'WEIRDCLASS'));
  171. }
  172. function testEmptyClassNames() {
  173. var el = goog.dom.getElement('span1');
  174. // At the very least, make sure these do not error out.
  175. assertFalse('Should not have an empty class', classes.has(el, ''));
  176. classes.add(el, '');
  177. classes.toggle(el, '');
  178. assertFalse('Should not remove an empty class', classes.remove(el, ''));
  179. classes.swap(el, '', 'OTHERCLASS');
  180. classes.swap(el, 'TEST1', '');
  181. classes.addRemove(el, '', '');
  182. }