abstractrange_test.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2009 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.AbstractRangeTest');
  15. goog.setTestOnly('goog.dom.AbstractRangeTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.AbstractRange');
  18. goog.require('goog.dom.Range');
  19. goog.require('goog.dom.TagName');
  20. goog.require('goog.testing.jsunit');
  21. function testCorrectDocument() {
  22. var a = goog.dom.getElement('a').contentWindow;
  23. var b = goog.dom.getElement('b').contentWindow;
  24. a.document.body.focus();
  25. var selection = goog.dom.AbstractRange.getBrowserSelectionForWindow(a);
  26. assertNotNull('Selection must not be null', selection);
  27. var range = goog.dom.Range.createFromBrowserSelection(selection);
  28. assertEquals(
  29. 'getBrowserSelectionForWindow must return selection in the ' +
  30. 'correct document',
  31. a.document, range.getDocument());
  32. // This is intended to trip up Internet Explorer --
  33. // see http://b/2048934
  34. b.document.body.focus();
  35. selection = goog.dom.AbstractRange.getBrowserSelectionForWindow(a);
  36. // Some (non-IE) browsers keep a separate selection state for each document
  37. // in the same browser window. That's fine, as long as the selection object
  38. // requested from the window object is correctly associated with that
  39. // window's document.
  40. if (selection != null && selection.rangeCount != 0) {
  41. range = goog.dom.Range.createFromBrowserSelection(selection);
  42. assertEquals(
  43. 'getBrowserSelectionForWindow must return selection in ' +
  44. 'the correct document',
  45. a.document, range.getDocument());
  46. } else {
  47. assertTrue(selection == null || selection.rangeCount == 0);
  48. }
  49. }
  50. function testSelectionIsControlRange() {
  51. var c = goog.dom.getElement('c').contentWindow;
  52. // Only IE supports control ranges
  53. if (c.document.body.createControlRange) {
  54. var controlRange = c.document.body.createControlRange();
  55. controlRange.add(
  56. goog.dom.getElementsByTagName(goog.dom.TagName.IMG, c.document)[0]);
  57. controlRange.select();
  58. var selection = goog.dom.AbstractRange.getBrowserSelectionForWindow(c);
  59. assertNotNull('Selection must not be null', selection);
  60. }
  61. }