savedcaretrange_test.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.SavedCaretRangeTest');
  15. goog.setTestOnly('goog.dom.SavedCaretRangeTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.Range');
  18. goog.require('goog.dom.SavedCaretRange');
  19. goog.require('goog.testing.dom');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.userAgent');
  22. function setUp() {
  23. document.body.normalize();
  24. }
  25. /** @bug 1480638 */
  26. function testSavedCaretRangeDoesntChangeSelection() {
  27. // NOTE(nicksantos): We cannot detect this bug programatically. The only
  28. // way to detect it is to run this test manually and look at the selection
  29. // when it ends.
  30. var div = goog.dom.getElement('bug1480638');
  31. var range =
  32. goog.dom.Range.createFromNodes(div.firstChild, 0, div.lastChild, 1);
  33. range.select();
  34. // Observe visible selection. Then move to next line and see it change.
  35. // If the bug exists, it starts with "foo" selected and ends with
  36. // it not selected.
  37. // debugger;
  38. var saved = range.saveUsingCarets();
  39. }
  40. function testSavedCaretRange() {
  41. if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(8)) {
  42. // testSavedCaretRange fails in IE7 unless the source files are loaded in a
  43. // certain order. Adding goog.require('goog.dom.classes') to dom.js or
  44. // goog.require('goog.array') to savedcaretrange_test.js after the
  45. // goog.require('goog.dom') line fixes the test, but it's better to not
  46. // rely on such hacks without understanding the reason of the failure.
  47. return;
  48. }
  49. var parent = goog.dom.getElement('caretRangeTest');
  50. var def = goog.dom.getElement('def');
  51. var jkl = goog.dom.getElement('jkl');
  52. var range =
  53. goog.dom.Range.createFromNodes(def.firstChild, 1, jkl.firstChild, 2);
  54. assertFalse(range.isReversed());
  55. range.select();
  56. var saved = range.saveUsingCarets();
  57. assertHTMLEquals(
  58. 'd<span id="' + saved.startCaretId_ + '"></span>ef', def.innerHTML);
  59. assertHTMLEquals(
  60. 'jk<span id="' + saved.endCaretId_ + '"></span>l', jkl.innerHTML);
  61. goog.testing.dom.assertRangeEquals(
  62. def.childNodes[1], 0, jkl.childNodes[1], 0, saved.toAbstractRange());
  63. def = goog.dom.getElement('def');
  64. jkl = goog.dom.getElement('jkl');
  65. var restoredRange = clearSelectionAndRestoreSaved(parent, saved);
  66. assertFalse(restoredRange.isReversed());
  67. goog.testing.dom.assertRangeEquals(def, 1, jkl, 1, restoredRange);
  68. var selection = goog.dom.Range.createFromWindow(window);
  69. assertHTMLEquals('def', def.innerHTML);
  70. assertHTMLEquals('jkl', jkl.innerHTML);
  71. // def and jkl now contain fragmented text nodes.
  72. var endNode = selection.getEndNode();
  73. if (endNode == jkl.childNodes[0]) {
  74. // Webkit (up to Chrome 57) and IE < 9.
  75. goog.testing.dom.assertRangeEquals(
  76. def.childNodes[1], 0, jkl.childNodes[0], 2, selection);
  77. } else if (endNode == jkl.childNodes[1]) {
  78. // Opera
  79. goog.testing.dom.assertRangeEquals(
  80. def.childNodes[1], 0, jkl.childNodes[1], 0, selection);
  81. } else {
  82. // Gecko, newer Chromes
  83. goog.testing.dom.assertRangeEquals(def, 1, jkl, 1, selection);
  84. }
  85. }
  86. function testReversedSavedCaretRange() {
  87. var parent = goog.dom.getElement('caretRangeTest');
  88. var def = goog.dom.getElement('def-5');
  89. var jkl = goog.dom.getElement('jkl-5');
  90. var range =
  91. goog.dom.Range.createFromNodes(jkl.firstChild, 1, def.firstChild, 2);
  92. assertTrue(range.isReversed());
  93. range.select();
  94. var saved = range.saveUsingCarets();
  95. var restoredRange = clearSelectionAndRestoreSaved(parent, saved);
  96. assertTrue(restoredRange.isReversed());
  97. goog.testing.dom.assertRangeEquals(def, 1, jkl, 1, restoredRange);
  98. }
  99. /*
  100. TODO(user): Look into why removeCarets test doesn't pass.
  101. function testRemoveCarets() {
  102. var def = goog.dom.getElement('def');
  103. var jkl = goog.dom.getElement('jkl');
  104. var range = goog.dom.Range.createFromNodes(
  105. def.firstChild, 1, jkl.firstChild, 2);
  106. range.select();
  107. var saved = range.saveUsingCarets();
  108. assertHTMLEquals(
  109. "d<span id="" + saved.startCaretId_ + ""></span>ef", def.innerHTML);
  110. assertHTMLEquals(
  111. "jk<span id="" + saved.endCaretId_ + ""></span>l", jkl.innerHTML);
  112. saved.removeCarets();
  113. assertHTMLEquals("def", def.innerHTML);
  114. assertHTMLEquals("jkl", jkl.innerHTML);
  115. var selection = goog.dom.Range.createFromWindow(window);
  116. assertEquals('Wrong start node', def.firstChild, selection.getStartNode());
  117. assertEquals('Wrong end node', jkl.firstChild, selection.getEndNode());
  118. assertEquals('Wrong start offset', 1, selection.getStartOffset());
  119. assertEquals('Wrong end offset', 2, selection.getEndOffset());
  120. }
  121. */
  122. function testRemoveContents() {
  123. var def = goog.dom.getElement('def-4');
  124. var jkl = goog.dom.getElement('jkl-4');
  125. // Sanity check.
  126. var container = goog.dom.getElement('removeContentsTest');
  127. assertEquals(7, container.childNodes.length);
  128. assertEquals('def', def.innerHTML);
  129. assertEquals('jkl', jkl.innerHTML);
  130. var range =
  131. goog.dom.Range.createFromNodes(def.firstChild, 1, jkl.firstChild, 2);
  132. range.select();
  133. var saved = range.saveUsingCarets();
  134. var restored = saved.restore();
  135. restored.removeContents();
  136. assertEquals(6, container.childNodes.length);
  137. assertEquals('d', def.innerHTML);
  138. assertEquals('l', jkl.innerHTML);
  139. }
  140. function testHtmlEqual() {
  141. var parent = goog.dom.getElement('caretRangeTest-2');
  142. var def = goog.dom.getElement('def-2');
  143. var jkl = goog.dom.getElement('jkl-2');
  144. var range =
  145. goog.dom.Range.createFromNodes(def.firstChild, 1, jkl.firstChild, 2);
  146. range.select();
  147. var saved = range.saveUsingCarets();
  148. var html1 = parent.innerHTML;
  149. saved.removeCarets();
  150. var saved2 = range.saveUsingCarets();
  151. var html2 = parent.innerHTML;
  152. saved2.removeCarets();
  153. assertNotEquals(
  154. 'Same selection with different saved caret range carets ' +
  155. 'must have different html.',
  156. html1, html2);
  157. assertTrue(
  158. 'Same selection with different saved caret range carets must ' +
  159. 'be considered equal by htmlEqual',
  160. goog.dom.SavedCaretRange.htmlEqual(html1, html2));
  161. saved.dispose();
  162. saved2.dispose();
  163. }
  164. function testStartCaretIsAtEndOfParent() {
  165. var parent = goog.dom.getElement('caretRangeTest-3');
  166. var def = goog.dom.getElement('def-3');
  167. var jkl = goog.dom.getElement('jkl-3');
  168. var range = goog.dom.Range.createFromNodes(def, 1, jkl, 1);
  169. range.select();
  170. var saved = range.saveUsingCarets();
  171. clearSelectionAndRestoreSaved(parent, saved);
  172. range = goog.dom.Range.createFromWindow();
  173. assertEquals('ghijkl', range.getText().replace(/\s/g, ''));
  174. }
  175. /**
  176. * Clear the selection by re-parsing the DOM. Then restore the saved
  177. * selection.
  178. * @param {Node} parent The node containing the current selection.
  179. * @param {goog.dom.SavedRange} saved The saved range.
  180. * @return {goog.dom.AbstractRange} Restored range.
  181. */
  182. function clearSelectionAndRestoreSaved(parent, saved) {
  183. goog.dom.Range.clearSelection();
  184. assertFalse(goog.dom.Range.hasSelection(window));
  185. var range = saved.restore();
  186. assertTrue(goog.dom.Range.hasSelection(window));
  187. return range;
  188. }