savedrange_test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2007 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.SavedRangeTest');
  15. goog.setTestOnly('goog.dom.SavedRangeTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.Range');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.userAgent');
  20. function testSaved() {
  21. var node = goog.dom.getElement('test1');
  22. var range = goog.dom.Range.createFromNodeContents(node);
  23. var savedRange = range.saveUsingDom();
  24. range = savedRange.restore(true);
  25. assertEquals('Restored range should select "Text"', 'Text', range.getText());
  26. assertFalse('Restored range should not be reversed.', range.isReversed());
  27. assertFalse(
  28. 'Range should not have disposed itself.', savedRange.isDisposed());
  29. goog.dom.Range.clearSelection();
  30. assertFalse(goog.dom.Range.hasSelection(window));
  31. range = savedRange.restore();
  32. assertTrue('Range should have auto-disposed.', savedRange.isDisposed());
  33. assertEquals('Restored range should select "Text"', 'Text', range.getText());
  34. assertFalse('Restored range should not be reversed.', range.isReversed());
  35. }
  36. function testReversedSave() {
  37. var node = goog.dom.getElement('test1').firstChild;
  38. var range = goog.dom.Range.createFromNodes(node, 4, node, 0);
  39. var savedRange = range.saveUsingDom();
  40. range = savedRange.restore();
  41. assertEquals('Restored range should select "Text"', 'Text', range.getText());
  42. if (!goog.userAgent.IE) {
  43. assertTrue('Restored range should be reversed.', range.isReversed());
  44. }
  45. }