nodeoffset_test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.NodeOffsetTest');
  15. goog.setTestOnly('goog.dom.NodeOffsetTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.NodeOffset');
  18. goog.require('goog.dom.NodeType');
  19. goog.require('goog.dom.TagName');
  20. goog.require('goog.testing.jsunit');
  21. var test1;
  22. var test2;
  23. var i;
  24. var empty;
  25. function setUpPage() {
  26. test1 = goog.dom.getElement('test1');
  27. i = goog.dom.getElement('i');
  28. test2 = goog.dom.getElement('test2');
  29. test2.innerHTML = test1.innerHTML;
  30. empty = goog.dom.getElement('empty');
  31. }
  32. function testElementOffset() {
  33. var nodeOffset = new goog.dom.NodeOffset(i, test1);
  34. var recovered = nodeOffset.findTargetNode(test2);
  35. assertNotNull('Should recover a node.', recovered);
  36. assertEquals(
  37. 'Should recover an I node.', String(goog.dom.TagName.I),
  38. recovered.tagName);
  39. assertTrue(
  40. 'Should recover a child of test2', goog.dom.contains(test2, recovered));
  41. assertFalse(
  42. 'Should not recover a child of test1',
  43. goog.dom.contains(test1, recovered));
  44. nodeOffset.dispose();
  45. }
  46. function testNodeOffset() {
  47. var nodeOffset = new goog.dom.NodeOffset(i.firstChild, test1);
  48. var recovered = nodeOffset.findTargetNode(test2);
  49. assertNotNull('Should recover a node.', recovered);
  50. assertEquals(
  51. 'Should recover a text node.', goog.dom.NodeType.TEXT,
  52. recovered.nodeType);
  53. assertEquals('Should have correct contents.', 'text.', recovered.nodeValue);
  54. assertTrue(
  55. 'Should recover a child of test2', goog.dom.contains(test2, recovered));
  56. assertFalse(
  57. 'Should not recover a child of test1',
  58. goog.dom.contains(test1, recovered));
  59. nodeOffset.dispose();
  60. }
  61. function testToString() {
  62. var nodeOffset = new goog.dom.NodeOffset(i.firstChild, test1);
  63. assertEquals(
  64. 'Should have correct string representation', '3,B\n1,I\n0,#text',
  65. nodeOffset.toString());
  66. nodeOffset.dispose();
  67. }
  68. function testBadRecovery() {
  69. var nodeOffset = new goog.dom.NodeOffset(i.firstChild, test1);
  70. var recovered = nodeOffset.findTargetNode(empty);
  71. assertNull('Should recover nothing.', recovered);
  72. nodeOffset.dispose();
  73. }