controlrange_test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. goog.provide('goog.dom.ControlRangeTest');
  15. goog.setTestOnly('goog.dom.ControlRangeTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.ControlRange');
  18. goog.require('goog.dom.RangeType');
  19. goog.require('goog.dom.TagName');
  20. goog.require('goog.dom.TextRange');
  21. goog.require('goog.testing.dom');
  22. goog.require('goog.testing.jsunit');
  23. goog.require('goog.userAgent');
  24. var logo;
  25. var table;
  26. function setUpPage() {
  27. logo = goog.dom.getElement('logo');
  28. table = goog.dom.getElement('table');
  29. }
  30. function testCreateFromElement() {
  31. if (!goog.userAgent.IE) {
  32. return;
  33. }
  34. assertNotNull(
  35. 'Control range object can be created for element',
  36. goog.dom.ControlRange.createFromElements(logo));
  37. }
  38. function testCreateFromRange() {
  39. if (!goog.userAgent.IE) {
  40. return;
  41. }
  42. var range = document.body.createControlRange();
  43. range.addElement(table);
  44. assertNotNull(
  45. 'Control range object can be created for element',
  46. goog.dom.ControlRange.createFromBrowserRange(range));
  47. }
  48. function testSelect() {
  49. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher('11')) {
  50. return;
  51. }
  52. var range = goog.dom.ControlRange.createFromElements(table);
  53. range.select();
  54. assertEquals(
  55. 'Control range should be selected', 'Control', document.selection.type);
  56. assertEquals(
  57. 'Control range should have length 1', 1,
  58. document.selection.createRange().length);
  59. assertEquals(
  60. 'Control range should select table', table,
  61. document.selection.createRange().item(0));
  62. }
  63. function testControlRangeIterator() {
  64. if (!goog.userAgent.IE) {
  65. return;
  66. }
  67. var range = goog.dom.ControlRange.createFromElements(logo, table);
  68. // Each node is included twice - once as a start tag, once as an end.
  69. goog.testing.dom.assertNodesMatch(range, [
  70. '#logo', '#logo', '#table', '#tbody', '#tr1', '#td11', 'a', '#td11',
  71. '#td12', 'b', '#td12', '#tr1', '#tr2', '#td21', 'c', '#td21',
  72. '#td22', 'd', '#td22', '#tr2', '#tbody', '#table'
  73. ]);
  74. }
  75. function testBounds() {
  76. if (!goog.userAgent.IE) {
  77. return;
  78. }
  79. // Initialize in both orders.
  80. helpTestBounds(goog.dom.ControlRange.createFromElements(logo, table));
  81. helpTestBounds(goog.dom.ControlRange.createFromElements(table, logo));
  82. }
  83. function helpTestBounds(range) {
  84. assertEquals('Start node is logo', logo, range.getStartNode());
  85. assertEquals('Start offset is 0', 0, range.getStartOffset());
  86. assertEquals('End node is table', table, range.getEndNode());
  87. assertEquals('End offset is 1', 1, range.getEndOffset());
  88. }
  89. function testCollapse() {
  90. if (!goog.userAgent.IE) {
  91. return;
  92. }
  93. var range = goog.dom.ControlRange.createFromElements(logo, table);
  94. assertFalse('Not initially collapsed', range.isCollapsed());
  95. range.collapse();
  96. assertTrue('Successfully collapsed', range.isCollapsed());
  97. }
  98. function testGetContainer() {
  99. if (!goog.userAgent.IE) {
  100. return;
  101. }
  102. var range = goog.dom.ControlRange.createFromElements(logo);
  103. assertEquals(
  104. 'Single element range is contained by itself', logo,
  105. range.getContainer());
  106. range = goog.dom.ControlRange.createFromElements(logo, table);
  107. assertEquals(
  108. 'Two element range is contained by body', document.body,
  109. range.getContainer());
  110. }
  111. function testSave() {
  112. if (!goog.userAgent.IE) {
  113. return;
  114. }
  115. var range = goog.dom.ControlRange.createFromElements(logo, table);
  116. var savedRange = range.saveUsingDom();
  117. range.collapse();
  118. assertTrue('Successfully collapsed', range.isCollapsed());
  119. range = savedRange.restore();
  120. assertEquals(
  121. 'Restored a control range', goog.dom.RangeType.CONTROL, range.getType());
  122. assertFalse('Not collapsed after restore', range.isCollapsed());
  123. helpTestBounds(range);
  124. }
  125. function testRemoveContents() {
  126. if (!goog.userAgent.IE) {
  127. return;
  128. }
  129. var img = goog.dom.createDom(goog.dom.TagName.IMG);
  130. img.src = logo.src;
  131. var div = goog.dom.getElement('test1');
  132. goog.dom.removeChildren(div);
  133. div.appendChild(img);
  134. assertEquals('Div has 1 child', 1, div.childNodes.length);
  135. var range = goog.dom.ControlRange.createFromElements(img);
  136. range.removeContents();
  137. assertEquals('Div has 0 children', 0, div.childNodes.length);
  138. assertTrue('Range is collapsed', range.isCollapsed());
  139. }
  140. function testReplaceContents() {
  141. // Test a control range.
  142. if (!goog.userAgent.IE) {
  143. return;
  144. }
  145. var outer = goog.dom.getElement('test1');
  146. outer.innerHTML = '<div contentEditable="true">' +
  147. 'Hello <input type="text" value="World">' +
  148. '</div>';
  149. range = goog.dom.ControlRange.createFromElements(
  150. goog.dom.getElementsByTagName(goog.dom.TagName.INPUT, outer)[0]);
  151. goog.dom.ControlRange.createFromElements(table);
  152. range.replaceContentsWithNode(goog.dom.createTextNode('World'));
  153. assertEquals('Hello World', outer.firstChild.innerHTML);
  154. }
  155. function testContainsRange() {
  156. if (!goog.userAgent.IE) {
  157. return;
  158. }
  159. var table2 = goog.dom.getElement('table2');
  160. var table2td = goog.dom.getElement('table2td');
  161. var logo2 = goog.dom.getElement('logo2');
  162. var range = goog.dom.ControlRange.createFromElements(logo, table);
  163. var range2 = goog.dom.ControlRange.createFromElements(logo);
  164. assertTrue(
  165. 'Control range contains the other control range',
  166. range.containsRange(range2));
  167. assertTrue(
  168. 'Control range partially contains the other control range',
  169. range2.containsRange(range, true));
  170. range2 = goog.dom.ControlRange.createFromElements(table2);
  171. assertFalse(
  172. 'Control range does not contain the other control range',
  173. range.containsRange(range2));
  174. range = goog.dom.ControlRange.createFromElements(table2);
  175. range2 = goog.dom.TextRange.createFromNodeContents(table2td);
  176. assertTrue('Control range contains text range', range.containsRange(range2));
  177. range2 = goog.dom.TextRange.createFromNodeContents(table);
  178. assertFalse(
  179. 'Control range does not contain text range', range.containsRange(range2));
  180. range = goog.dom.ControlRange.createFromElements(logo2);
  181. range2 = goog.dom.TextRange.createFromNodeContents(table2);
  182. assertFalse(
  183. 'Control range does not fully contain text range',
  184. range.containsRange(range2, false));
  185. range2 = goog.dom.ControlRange.createFromElements(table2);
  186. assertTrue(
  187. 'Control range contains the other control range (2)',
  188. range2.containsRange(range));
  189. }
  190. function testCloneRange() {
  191. if (!goog.userAgent.IE) {
  192. return;
  193. }
  194. var range = goog.dom.ControlRange.createFromElements(logo);
  195. assertNotNull('Control range object created for element', range);
  196. var cloneRange = range.clone();
  197. assertNotNull('Cloned control range object', cloneRange);
  198. assertArrayEquals(
  199. 'Control range and clone have same elements', range.getElements(),
  200. cloneRange.getElements());
  201. }