classlist_test.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Copyright 2012 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 classlist_test.html.
  16. */
  17. goog.provide('goog.dom.classlist_test');
  18. goog.setTestOnly('goog.dom.classlist_test');
  19. goog.require('goog.dom');
  20. goog.require('goog.dom.TagName');
  21. goog.require('goog.dom.classlist');
  22. goog.require('goog.testing.ExpectedFailures');
  23. goog.require('goog.testing.jsunit');
  24. var expectedFailures = new goog.testing.ExpectedFailures();
  25. var classlist = goog.dom.classlist;
  26. function tearDown() {
  27. expectedFailures.handleTearDown();
  28. }
  29. function testGet() {
  30. var el = goog.dom.createElement(goog.dom.TagName.DIV);
  31. assertTrue(classlist.get(el).length == 0);
  32. el.className = 'C';
  33. assertElementsEquals(['C'], classlist.get(el));
  34. el.className = 'C D';
  35. assertElementsEquals(['C', 'D'], classlist.get(el));
  36. el.className = 'C\nD';
  37. assertElementsEquals(['C', 'D'], classlist.get(el));
  38. el.className = ' C ';
  39. assertElementsEquals(['C'], classlist.get(el));
  40. }
  41. function testContainsWithNewlines() {
  42. var el = goog.dom.getElement('p1');
  43. assertTrue('Should not have SOMECLASS', classlist.contains(el, 'SOMECLASS'));
  44. assertTrue(
  45. 'Should also have OTHERCLASS', classlist.contains(el, 'OTHERCLASS'));
  46. assertFalse(
  47. 'Should not have WEIRDCLASS', classlist.contains(el, 'WEIRDCLASS'));
  48. }
  49. function testContainsCaseSensitive() {
  50. var el = goog.dom.getElement('p2');
  51. assertFalse('Should not have camelcase', classlist.contains(el, 'camelcase'));
  52. assertFalse('Should not have CAMELCASE', classlist.contains(el, 'CAMELCASE'));
  53. assertTrue('Should have camelCase', classlist.contains(el, 'camelCase'));
  54. }
  55. function testAddNotAddingMultiples() {
  56. var el = goog.dom.createElement(goog.dom.TagName.DIV);
  57. classlist.add(el, 'A');
  58. assertEquals('A', el.className);
  59. classlist.add(el, 'A');
  60. assertEquals('A', el.className);
  61. classlist.add(el, 'B', 'B');
  62. assertEquals('A B', el.className);
  63. }
  64. function testAddCaseSensitive() {
  65. var el = goog.dom.createElement(goog.dom.TagName.DIV);
  66. classlist.add(el, 'A');
  67. assertTrue(classlist.contains(el, 'A'));
  68. assertFalse(classlist.contains(el, 'a'));
  69. classlist.add(el, 'a');
  70. assertTrue(classlist.contains(el, 'A'));
  71. assertTrue(classlist.contains(el, 'a'));
  72. assertEquals('A a', el.className);
  73. }
  74. function testAddAll() {
  75. var elem = goog.dom.createElement(goog.dom.TagName.DIV);
  76. elem.className = 'foo goog-bar';
  77. goog.dom.classlist.addAll(elem, ['goog-baz', 'foo']);
  78. assertEquals(3, classlist.get(elem).length);
  79. assertTrue(goog.dom.classlist.contains(elem, 'foo'));
  80. assertTrue(goog.dom.classlist.contains(elem, 'goog-bar'));
  81. assertTrue(goog.dom.classlist.contains(elem, 'goog-baz'));
  82. }
  83. function testAddAllEmpty() {
  84. var classes = 'foo bar';
  85. var elem = goog.dom.createElement(goog.dom.TagName.DIV);
  86. elem.className = classes;
  87. goog.dom.classlist.addAll(elem, []);
  88. assertEquals(elem.className, classes);
  89. }
  90. function testRemove() {
  91. var el = goog.dom.createElement(goog.dom.TagName.DIV);
  92. el.className = 'A B C';
  93. classlist.remove(el, 'B');
  94. assertEquals('A C', el.className);
  95. }
  96. function testRemoveCaseSensitive() {
  97. var el = goog.dom.createElement(goog.dom.TagName.DIV);
  98. el.className = 'A B C';
  99. classlist.remove(el, 'b');
  100. assertEquals('A B C', el.className);
  101. }
  102. function testRemoveAll() {
  103. var elem = goog.dom.createElement(goog.dom.TagName.DIV);
  104. elem.className = 'foo bar baz';
  105. goog.dom.classlist.removeAll(elem, ['bar', 'foo']);
  106. assertFalse(goog.dom.classlist.contains(elem, 'foo'));
  107. assertFalse(goog.dom.classlist.contains(elem, 'bar'));
  108. assertTrue(goog.dom.classlist.contains(elem, 'baz'));
  109. }
  110. function testRemoveAllOne() {
  111. var elem = goog.dom.createElement(goog.dom.TagName.DIV);
  112. elem.className = 'foo bar baz';
  113. goog.dom.classlist.removeAll(elem, ['bar']);
  114. assertFalse(goog.dom.classlist.contains(elem, 'bar'));
  115. assertTrue(goog.dom.classlist.contains(elem, 'foo'));
  116. assertTrue(goog.dom.classlist.contains(elem, 'baz'));
  117. }
  118. function testRemoveAllSomeNotPresent() {
  119. var elem = goog.dom.createElement(goog.dom.TagName.DIV);
  120. elem.className = 'foo bar baz';
  121. goog.dom.classlist.removeAll(elem, ['a', 'bar']);
  122. assertTrue(goog.dom.classlist.contains(elem, 'foo'));
  123. assertFalse(goog.dom.classlist.contains(elem, 'bar'));
  124. assertTrue(goog.dom.classlist.contains(elem, 'baz'));
  125. }
  126. function testRemoveAllCaseSensitive() {
  127. var elem = goog.dom.createElement(goog.dom.TagName.DIV);
  128. elem.className = 'foo bar baz';
  129. goog.dom.classlist.removeAll(elem, ['BAR', 'foo']);
  130. assertFalse(goog.dom.classlist.contains(elem, 'foo'));
  131. assertTrue(goog.dom.classlist.contains(elem, 'bar'));
  132. assertTrue(goog.dom.classlist.contains(elem, 'baz'));
  133. }
  134. function testEnable() {
  135. var el = goog.dom.getElement('p1');
  136. classlist.set(el, 'SOMECLASS FIRST');
  137. assertTrue('Should have FIRST class', classlist.contains(el, 'FIRST'));
  138. assertTrue(
  139. 'Should have SOMECLASS class', classlist.contains(el, 'SOMECLASS'));
  140. classlist.enable(el, 'FIRST', false);
  141. assertFalse('Should not have FIRST class', classlist.contains(el, 'FIRST'));
  142. assertTrue(
  143. 'Should have SOMECLASS class', classlist.contains(el, 'SOMECLASS'));
  144. classlist.enable(el, 'FIRST', true);
  145. assertTrue('Should have FIRST class', classlist.contains(el, 'FIRST'));
  146. assertTrue(
  147. 'Should have SOMECLASS class', classlist.contains(el, 'SOMECLASS'));
  148. }
  149. function testEnableNotAddingMultiples() {
  150. var el = goog.dom.createElement(goog.dom.TagName.DIV);
  151. classlist.enable(el, 'A', true);
  152. assertEquals('A', el.className);
  153. classlist.enable(el, 'A', true);
  154. assertEquals('A', el.className);
  155. classlist.enable(el, 'B', 'B', true);
  156. assertEquals('A B', el.className);
  157. }
  158. function testEnableAllRemove() {
  159. var elem = goog.dom.createElement(goog.dom.TagName.DIV);
  160. elem.className = 'foo bar baz';
  161. // Test removing some classes (some not present).
  162. goog.dom.classlist.enableAll(elem, ['a', 'bar'], false /* enable */);
  163. assertTrue(goog.dom.classlist.contains(elem, 'foo'));
  164. assertFalse(goog.dom.classlist.contains(elem, 'bar'));
  165. assertTrue(goog.dom.classlist.contains(elem, 'baz'));
  166. assertFalse(goog.dom.classlist.contains(elem, 'a'));
  167. }
  168. function testEnableAllAdd() {
  169. var elem = goog.dom.createElement(goog.dom.TagName.DIV);
  170. elem.className = 'foo bar';
  171. // Test adding some classes (some duplicate).
  172. goog.dom.classlist.enableAll(elem, ['a', 'bar', 'baz'], true /* enable */);
  173. assertTrue(goog.dom.classlist.contains(elem, 'foo'));
  174. assertTrue(goog.dom.classlist.contains(elem, 'bar'));
  175. assertTrue(goog.dom.classlist.contains(elem, 'baz'));
  176. assertTrue(goog.dom.classlist.contains(elem, 'a'));
  177. }
  178. function testSwap() {
  179. var el = goog.dom.getElement('p1');
  180. classlist.set(el, 'SOMECLASS FIRST');
  181. assertTrue('Should have FIRST class', classlist.contains(el, 'FIRST'));
  182. assertTrue('Should have FIRST class', classlist.contains(el, 'SOMECLASS'));
  183. assertFalse('Should not have second class', classlist.contains(el, 'second'));
  184. classlist.swap(el, 'FIRST', 'second');
  185. assertFalse('Should not have FIRST class', classlist.contains(el, 'FIRST'));
  186. assertTrue('Should have FIRST class', classlist.contains(el, 'SOMECLASS'));
  187. assertTrue('Should have second class', classlist.contains(el, 'second'));
  188. classlist.swap(el, 'second', 'FIRST');
  189. assertTrue('Should have FIRST class', classlist.contains(el, 'FIRST'));
  190. assertTrue('Should have FIRST class', classlist.contains(el, 'SOMECLASS'));
  191. assertFalse('Should not have second class', classlist.contains(el, 'second'));
  192. }
  193. function testToggle() {
  194. var el = goog.dom.getElement('p1');
  195. classlist.set(el, 'SOMECLASS FIRST');
  196. assertTrue('Should have FIRST class', classlist.contains(el, 'FIRST'));
  197. assertTrue(
  198. 'Should have SOMECLASS class', classlist.contains(el, 'SOMECLASS'));
  199. var ret = classlist.toggle(el, 'FIRST');
  200. assertFalse('Should not have FIRST class', classlist.contains(el, 'FIRST'));
  201. assertTrue(
  202. 'Should have SOMECLASS class', classlist.contains(el, 'SOMECLASS'));
  203. assertFalse('Return value should have been false', ret);
  204. ret = classlist.toggle(el, 'FIRST');
  205. assertTrue('Should have FIRST class', classlist.contains(el, 'FIRST'));
  206. assertTrue(
  207. 'Should have SOMECLASS class', classlist.contains(el, 'SOMECLASS'));
  208. assertTrue('Return value should have been true', ret);
  209. }
  210. function testAddRemoveString() {
  211. var el = goog.dom.createElement(goog.dom.TagName.DIV);
  212. el.className = 'A';
  213. classlist.addRemove(el, 'A', 'B');
  214. assertEquals('B', el.className);
  215. classlist.addRemove(el, 'Z', 'C');
  216. assertEquals('B C', el.className);
  217. classlist.addRemove(el, 'C', 'D');
  218. assertEquals('B D', el.className);
  219. classlist.addRemove(el, 'D', 'B');
  220. assertEquals('B', el.className);
  221. }