/closure/closure-library/closure/goog/dom/selection_test.html

https://code.google.com/p/plovr/ · HTML · 329 lines · 273 code · 50 blank · 6 comment · 0 complexity · 6d38910a8441c5577eaaaa3287def716 MD5 · raw file

  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2007 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  10. <title>Closure Unit Tests - goog.dom.selection</title>
  11. <script src="../base.js"></script>
  12. <script>
  13. goog.require('goog.dom');
  14. goog.require('goog.dom.selection');
  15. goog.require('goog.testing.jsunit');
  16. goog.require('goog.userAgent');
  17. </script>
  18. </head>
  19. <body>
  20. <script>
  21. var input;
  22. var hiddenInput;
  23. var textarea;
  24. var hiddenTextarea;
  25. function setUp() {
  26. input = goog.dom.createDom('input', {type: 'text'});
  27. textarea = goog.dom.createDom('textarea');
  28. hiddenInput = goog.dom.createDom(
  29. 'input', {type: 'text', style: 'display: none'});
  30. hiddenTextarea = goog.dom.createDom(
  31. 'textarea', {style: 'display: none'});
  32. document.body.appendChild(input);
  33. document.body.appendChild(textarea);
  34. document.body.appendChild(hiddenInput);
  35. document.body.appendChild(hiddenTextarea);
  36. }
  37. function tearDown() {
  38. goog.dom.removeNode(input);
  39. goog.dom.removeNode(textarea);
  40. goog.dom.removeNode(hiddenInput);
  41. goog.dom.removeNode(hiddenTextarea);
  42. }
  43. /**
  44. * Tests getStart routine in both input and textarea.
  45. */
  46. function testGetStartInput() {
  47. getStartHelper(input, hiddenInput);
  48. }
  49. function testGetStartTextarea() {
  50. getStartHelper(textarea, hiddenTextarea);
  51. }
  52. function getStartHelper(field, hiddenField) {
  53. assertEquals(0, goog.dom.selection.getStart(field));
  54. assertEquals(0, goog.dom.selection.getStart(hiddenField));
  55. field.focus();
  56. assertEquals(0, goog.dom.selection.getStart(field));
  57. }
  58. /**
  59. * Tests the setText routine for both input and textarea
  60. * with a single line of text.
  61. */
  62. function testSetTextInput() {
  63. setTextHelper(input);
  64. }
  65. function testSetTextTextarea() {
  66. setTextHelper(textarea);
  67. }
  68. function setTextHelper(field) {
  69. // Test one line string only
  70. select(field);
  71. assertEquals('', goog.dom.selection.getText(field));
  72. goog.dom.selection.setText(field, 'Get Behind Me Satan');
  73. assertEquals('Get Behind Me Satan', goog.dom.selection.getText(field));
  74. }
  75. /**
  76. * Tests the setText routine for textarea with multiple lines of text.
  77. */
  78. function testSetTextMultipleLines() {
  79. select(textarea);
  80. assertEquals('', goog.dom.selection.getText(textarea));
  81. var message = goog.userAgent.IE ?
  82. 'Get Behind Me\r\nSatan' :
  83. 'Get Behind Me\nSatan';
  84. goog.dom.selection.setText(textarea, message);
  85. assertEquals(message, goog.dom.selection.getText(textarea));
  86. // Select the text upto the point just after the \r\n combination
  87. // or \n in GECKO.
  88. var endOfNewline = goog.userAgent.IE ? 15 : 14;
  89. var selectedMessage = message.substring(0, endOfNewline);
  90. goog.dom.selection.setStart(textarea, 0);
  91. goog.dom.selection.setEnd(textarea, endOfNewline);
  92. assertEquals(selectedMessage, goog.dom.selection.getText(textarea));
  93. selectedMessage = goog.userAgent.IE ? '\r\n' : '\n';
  94. goog.dom.selection.setStart(textarea, 13);
  95. goog.dom.selection.setEnd(textarea, endOfNewline);
  96. assertEquals(selectedMessage, goog.dom.selection.getText(textarea));
  97. }
  98. /**
  99. * Tests the setCursor routine for both input and textarea.
  100. */
  101. function testSetCursorInput() {
  102. setCursorHelper(input);
  103. }
  104. function testSetCursorTextarea() {
  105. setCursorHelper(textarea);
  106. }
  107. function setCursorHelper(field) {
  108. select(field);
  109. // try to set the cursor beyond the length of the content
  110. goog.dom.selection.setStart(field, 5);
  111. goog.dom.selection.setEnd(field, 15);
  112. assertEquals(0, goog.dom.selection.getStart(field));
  113. assertEquals(0, goog.dom.selection.getEnd(field));
  114. select(field);
  115. var message = 'Get Behind Me Satan';
  116. goog.dom.selection.setText(field, message);
  117. goog.dom.selection.setStart(field, 5);
  118. goog.dom.selection.setEnd(field, message.length);
  119. assertEquals(5, goog.dom.selection.getStart(field));
  120. assertEquals(message.length, goog.dom.selection.getEnd(field));
  121. // Set the end before the start, and see if getEnd returns the start
  122. // position itself.
  123. goog.dom.selection.setStart(field, 5);
  124. goog.dom.selection.setEnd(field, 3);
  125. assertEquals(3, goog.dom.selection.getEnd(field));
  126. }
  127. /**
  128. * Tests the getText and setText routines acting on selected text in
  129. * both input and textarea.
  130. */
  131. function testGetAndSetSelectedTextInput() {
  132. getAndSetSelectedTextHelper(input);
  133. }
  134. function testGetAndSetSelectedTextTextarea() {
  135. getAndSetSelectedTextHelper(textarea);
  136. }
  137. function getAndSetSelectedTextHelper(field) {
  138. select(field);
  139. goog.dom.selection.setText(field, 'Get Behind Me Satan');
  140. // select 'Behind'
  141. goog.dom.selection.setStart(field, 4);
  142. goog.dom.selection.setEnd(field, 10);
  143. assertEquals('Behind', goog.dom.selection.getText(field));
  144. goog.dom.selection.setText(field, 'In Front Of');
  145. goog.dom.selection.setStart(field, 0);
  146. goog.dom.selection.setEnd(field, 100);
  147. assertEquals('Get In Front Of Me Satan', goog.dom.selection.getText(field));
  148. }
  149. /**
  150. * Test setStart on hidden input and hidden textarea.
  151. */
  152. function testSetCursorOnHiddenInput() {
  153. setCursorOnHiddenInputHelper(hiddenInput);
  154. }
  155. function testSetCursorOnHiddenTextarea() {
  156. setCursorOnHiddenInputHelper(hiddenTextarea);
  157. }
  158. function setCursorOnHiddenInputHelper(hiddenField) {
  159. goog.dom.selection.setStart(hiddenField, 0);
  160. assertEquals(0, goog.dom.selection.getStart(hiddenField));
  161. }
  162. /**
  163. * Test setStart, setEnd, getStart and getEnd in textarea with text
  164. * containing line breaks.
  165. */
  166. function testSetAndGetCursorWithLineBreaks() {
  167. select(textarea);
  168. var newline = goog.userAgent.IE ? '\r\n' : '\n';
  169. var message = 'Hello' + newline + 'World';
  170. goog.dom.selection.setText(textarea, message);
  171. // Test setEnd and getEnd, by setting the cursor somewhere after the
  172. // \r\n combination.
  173. goog.dom.selection.setEnd(textarea, 9);
  174. assertEquals(9, goog.dom.selection.getEnd(textarea));
  175. // Test basic setStart and getStart
  176. goog.dom.selection.setStart(textarea, 10);
  177. assertEquals(10, goog.dom.selection.getStart(textarea));
  178. // Test setEnd and getEnd, by setting the cursor exactly after the
  179. // \r\n combination in IE or after \n in GECKO.
  180. var endOfNewline = goog.userAgent.IE ? 7 : 6;
  181. checkSetAndGetTextarea(endOfNewline, endOfNewline);
  182. // Select a \r\n combination in IE or \n in GECKO and see if
  183. // getStart and getEnd work correctly.
  184. clearField(textarea);
  185. message = 'Hello' + newline + newline + 'World';
  186. goog.dom.selection.setText(textarea, message);
  187. var startOfNewline = goog.userAgent.IE ? 7 : 6;
  188. endOfNewline = goog.userAgent.IE ? 9 : 7;
  189. checkSetAndGetTextarea(startOfNewline, endOfNewline);
  190. // Select 2 \r\n combinations in IE or 2 \ns in GECKO and see if getStart
  191. // and getEnd work correctly.
  192. checkSetAndGetTextarea(5, endOfNewline);
  193. // Position cursor b/w 2 \r\n combinations in IE or 2 \ns in GECKO and see
  194. // if getStart and getEnd work correctly.
  195. clearField(textarea);
  196. message = 'Hello' + newline + newline + newline + newline + 'World';
  197. goog.dom.selection.setText(textarea, message);
  198. var middleOfNewlines = goog.userAgent.IE ? 9 : 7;
  199. checkSetAndGetTextarea(middleOfNewlines, middleOfNewlines);
  200. // Position cursor at end of a textarea which ends with \r\n in IE or \n in
  201. // GECKO.
  202. clearField(textarea);
  203. message = 'Hello' + newline + newline;
  204. goog.dom.selection.setText(textarea, message);
  205. var endOfTextarea = message.length;
  206. checkSetAndGetTextarea(endOfTextarea, endOfTextarea);
  207. // Position cursor at the end of the 2 starting \r\ns in IE or \ns in GECKO
  208. // within a textarea.
  209. clearField(textarea);
  210. message = newline + newline + 'World';
  211. goog.dom.selection.setText(textarea, message);
  212. var endOfTwoNewlines = goog.userAgent.IE ? 4 : 2;
  213. checkSetAndGetTextarea(endOfTwoNewlines, endOfTwoNewlines);
  214. // Position cursor at the end of the first \r\n in IE or \n in
  215. // GECKO within a textarea.
  216. endOfOneNewline = goog.userAgent.IE ? 2 : 1;
  217. checkSetAndGetTextarea(endOfOneNewline, endOfOneNewline);
  218. }
  219. /**
  220. * Test to make sure there's no error when getting the range of an unselected
  221. * textarea. See bug 1274027.
  222. */
  223. function testGetStartOnUnfocusedTextarea() {
  224. input.value = 'White Blood Cells';
  225. input.focus();
  226. goog.dom.selection.setCursorPosition(input, 5);
  227. assertEquals('getStart on input should return where we put the cursor',
  228. 5, goog.dom.selection.getStart(input));
  229. assertEquals('getStart on unfocused textarea should succeed without error',
  230. 0, goog.dom.selection.getStart(textarea));
  231. }
  232. /**
  233. * Test to make sure there's no error setting cursor position within a
  234. * textarea after a newline. This is problematic on IE because of the
  235. * '\r\n' vs '\n' issue.
  236. */
  237. function testSetCursorPositionTextareaWithNewlines() {
  238. textarea.value = 'Hello\nWorld';
  239. textarea.focus();
  240. // Set the selection point between 'W' and 'o'. Position is computed this
  241. // way instead of being hard-coded because it's different in IE due to \r\n
  242. // vs \n.
  243. goog.dom.selection.setCursorPosition(textarea, textarea.value.length - 4);
  244. var linebreak = goog.userAgent.IE ? '\r\n' : '\n';
  245. var expectedLeftString = 'Hello' + linebreak + 'W';
  246. assertEquals('getStart on input should return after the newline',
  247. expectedLeftString.length, goog.dom.selection.getStart(textarea));
  248. assertEquals('getEnd on input should return after the newline',
  249. expectedLeftString.length, goog.dom.selection.getEnd(textarea));
  250. goog.dom.selection.setEnd(textarea, textarea.value.length);
  251. assertEquals('orld', goog.dom.selection.getText(textarea));
  252. }
  253. /**
  254. * Helper function to clear the textfield contents.
  255. */
  256. function clearField(field) {
  257. field.value = '';
  258. }
  259. /**
  260. * Helper function to set the start and end and assert the getter values.
  261. */
  262. function checkSetAndGetTextarea(start, end) {
  263. goog.dom.selection.setStart(textarea, start);
  264. goog.dom.selection.setEnd(textarea, end);
  265. assertEquals(start, goog.dom.selection.getStart(textarea));
  266. assertEquals(end, goog.dom.selection.getEnd(textarea));
  267. }
  268. /**
  269. * Helper function to focus and select a field. In IE8, selected
  270. * fields need focus.
  271. */
  272. function select(field) {
  273. field.focus();
  274. field.select();
  275. }
  276. </script>
  277. </body>
  278. </html>