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