textrangeiterator_test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.TextRangeIteratorTest');
  15. goog.setTestOnly('goog.dom.TextRangeIteratorTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.dom.TextRangeIterator');
  19. goog.require('goog.iter.StopIteration');
  20. goog.require('goog.testing.dom');
  21. goog.require('goog.testing.jsunit');
  22. var test;
  23. var test2;
  24. function setUpPage() {
  25. test = goog.dom.getElement('test');
  26. test2 = goog.dom.getElement('test2');
  27. }
  28. function testBasic() {
  29. goog.testing.dom.assertNodesMatch(
  30. new goog.dom.TextRangeIterator(test, 0, test, 2),
  31. ['#a1', 'T', '#b1', 'e', '#b1', 'xt', '#a1', '#span1', '#span1', '#p1']);
  32. }
  33. function testAdjustStart() {
  34. var iterator = new goog.dom.TextRangeIterator(test, 0, test, 2);
  35. iterator.setStartNode(goog.dom.getElement('span1'));
  36. goog.testing.dom.assertNodesMatch(iterator, ['#span1', '#span1', '#p1']);
  37. }
  38. function testAdjustEnd() {
  39. var iterator = new goog.dom.TextRangeIterator(test, 0, test, 2);
  40. iterator.setEndNode(goog.dom.getElement('span1'));
  41. goog.testing.dom.assertNodesMatch(
  42. iterator, ['#a1', 'T', '#b1', 'e', '#b1', 'xt', '#a1', '#span1']);
  43. }
  44. function testOffsets() {
  45. var iterator =
  46. new goog.dom.TextRangeIterator(test2.firstChild, 1, test2.lastChild, 2);
  47. // foo
  48. var node = iterator.next();
  49. assertEquals(
  50. 'Should have start offset at iteration step 1', 1,
  51. iterator.getStartTextOffset());
  52. assertEquals(
  53. 'Should not have end offset at iteration step 1', node.nodeValue.length,
  54. iterator.getEndTextOffset());
  55. // <br>
  56. node = iterator.next();
  57. assertEquals(
  58. 'Should not have start offset at iteration step 2', -1,
  59. iterator.getStartTextOffset());
  60. assertEquals(
  61. 'Should not have end offset at iteration step 2', -1,
  62. iterator.getEndTextOffset());
  63. // </br>
  64. node = iterator.next();
  65. assertEquals(
  66. 'Should not have start offset at iteration step 3', -1,
  67. iterator.getStartTextOffset());
  68. assertEquals(
  69. 'Should not have end offset at iteration step 3', -1,
  70. iterator.getEndTextOffset());
  71. // bar
  72. node = iterator.next();
  73. assertEquals(
  74. 'Should not have start offset at iteration step 4', 0,
  75. iterator.getStartTextOffset());
  76. assertEquals(
  77. 'Should have end offset at iteration step 4', 2,
  78. iterator.getEndTextOffset());
  79. }
  80. function testSingleNodeOffsets() {
  81. var iterator =
  82. new goog.dom.TextRangeIterator(test2.firstChild, 1, test2.firstChild, 2);
  83. iterator.next();
  84. assertEquals('Should have start offset', 1, iterator.getStartTextOffset());
  85. assertEquals('Should have end offset', 2, iterator.getEndTextOffset());
  86. }
  87. function testEndNodeOffsetAtEnd() {
  88. var iterator = new goog.dom.TextRangeIterator(
  89. goog.dom.getElement('b1').firstChild, 0, goog.dom.getElement('b1'), 1);
  90. goog.testing.dom.assertNodesMatch(iterator, ['e', '#b1']);
  91. }
  92. function testSkipTagDoesNotSkipEnd() {
  93. // Iterate over 'Tex'.
  94. var iterator = new goog.dom.TextRangeIterator(
  95. test.firstChild.firstChild, 0, test.firstChild.lastChild, 1);
  96. var node = iterator.next();
  97. assertEquals('T', node.nodeValue);
  98. node = iterator.next();
  99. assertEquals(String(goog.dom.TagName.B), node.tagName);
  100. iterator.skipTag();
  101. node = iterator.next();
  102. assertEquals('xt', node.nodeValue);
  103. }
  104. function testSkipTagSkipsEnd() {
  105. // Iterate over 'Te'.
  106. var iterator = new goog.dom.TextRangeIterator(
  107. test.firstChild.firstChild, 0,
  108. goog.dom.getElementsByTagName(goog.dom.TagName.B, test)[0].firstChild, 1);
  109. var node = iterator.next();
  110. assertEquals('T', node.nodeValue);
  111. node = iterator.next();
  112. assertEquals(String(goog.dom.TagName.B), node.tagName);
  113. var ex = assertThrows('Should stop iteration when skipping B', function() {
  114. iterator.skipTag();
  115. });
  116. assertEquals(goog.iter.StopIteration, ex);
  117. }