diff --git a/src/js/core/wrappedselection.js b/src/js/core/wrappedselection.js index 927c6a3e..29ead7df 100644 --- a/src/js/core/wrappedselection.js +++ b/src/js/core/wrappedselection.js @@ -82,8 +82,8 @@ rangy.createModule("WrappedSelection", function(api, module) { var body = dom.getBody(document); // Obtaining a range from a selection - var selectionHasAnchorAndFocus = util.areHostObjects(testSelection, ["anchorNode", "focusNode"] && - util.areHostProperties(testSelection, ["anchorOffset", "focusOffset"])); + var selectionHasAnchorAndFocus = util.areHostObjects(testSelection, ["anchorNode", "focusNode"]) && + util.areHostProperties(testSelection, ["anchorOffset", "focusOffset"]); api.features.selectionHasAnchorAndFocus = selectionHasAnchorAndFocus; // Test for existence of native selection extend() method @@ -101,24 +101,23 @@ rangy.createModule("WrappedSelection", function(api, module) { typeof testSelection.rangeCount == "number" && api.features.implementsDomRange) { (function() { - var iframe = document.createElement("iframe"); - iframe.frameBorder = 0; - iframe.style.position = "absolute"; - iframe.style.left = "-10000px"; - body.appendChild(iframe); - - var iframeDoc = dom.getIframeDocument(iframe); - iframeDoc.open(); - iframeDoc.write("12"); - iframeDoc.close(); - - var sel = dom.getIframeWindow(iframe).getSelection(); + // Previously an iframe was used but this caused problems in some circumatsances in IE, so tests are + // performed on the current document's selection. See issue 109. + + // Note also that if a selection previously existed, it is wiped by these tests. This should usually be fine + // because initialization usually happens when the document loads, but could be a problem for a script that + // loads and initializes Rangy later. If anyone complains, code could be added to the selection could be + // saved and restored. + var sel = window.getSelection(); if (sel) { - var docEl = iframeDoc.documentElement; - var iframeBody = docEl.lastChild, textNode = iframeBody.firstChild; + var body = dom.getBody(document); + var testEl = body.appendChild( document.createElement("div") ); + testEl.contentEditable = "false"; + var textNode = testEl.appendChild( document.createTextNode("\u00a0\u00a0\u00a0") ); // Test whether the native selection will allow a collapsed selection within a non-editable element - var r1 = iframeDoc.createRange(); + var r1 = document.createRange(); + r1.setStart(textNode, 1); r1.collapse(true); sel.addRange(r1); @@ -128,18 +127,19 @@ rangy.createModule("WrappedSelection", function(api, module) { // Test whether the native selection is capable of supporting multiple ranges var r2 = r1.cloneRange(); r1.setStart(textNode, 0); - r2.setEnd(textNode, 2); + r2.setEnd(textNode, 3); + r2.setStart(textNode, 2); sel.addRange(r1); sel.addRange(r2); selectionSupportsMultipleRanges = (sel.rangeCount == 2); // Clean up + body.removeChild(testEl); + sel.removeAllRanges(); r1.detach(); r2.detach(); } - - body.removeChild(iframe); })(); } diff --git a/test/featuretests.html b/test/featuretests.html new file mode 100644 index 00000000..c763e487 --- /dev/null +++ b/test/featuretests.html @@ -0,0 +1,36 @@ + + + + Rangy - TextRange-to-Range Performace Tests + + + + + + + + + + + + + + + + + + +
+
+ + diff --git a/test/featuretests.js b/test/featuretests.js new file mode 100644 index 00000000..369ba8df --- /dev/null +++ b/test/featuretests.js @@ -0,0 +1,53 @@ +xn.test.suite("Browser feature tests", function(s) { + rangy.init(); + + // Detect browser version roughly. It doesn't matter too much: these are only rough tests designed to test whether + // Rangy's feature detection is hopelessly wrong + + + var browser = jQuery.browser; + var isIe = !!browser.msie; + var isMozilla = !!browser.mozilla; + var isOpera = !!browser.opera; + var version = parseFloat(browser.version); + + s.test("DOM Range support", function(t) { + t.assertEquals(rangy.features.implementsDomRange, !isIe || version >= 9); + }); + + s.test("TextRange support", function(t) { + t.assertEquals(rangy.features.implementsTextRange, isIe && version >= 4); + }); + + s.test("document.selection support", function(t) { + t.assertEquals(rangy.features.implementsTextRange, isIe && version >= 4); + }); + + s.test("window.getSelection() support", function(t) { + t.assertEquals(rangy.features.implementsWinGetSelection, !isIe || version >= 9); + }); + + s.test("selection has rangeCount", function(t) { + t.assertEquals(rangy.features.selectionHasRangeCount, !isIe || version >= 9); + }); + + s.test("selection has anchor and focus support", function(t) { + t.assertEquals(rangy.features.selectionHasAnchorAndFocus, !isIe || version >= 9); + }); + + s.test("selection has extend() method", function(t) { + t.assertEquals(rangy.features.selectionHasExtend, !isIe); + }); + + s.test("HTML parsing", function(t) { + t.assertEquals(rangy.features.htmlParsingConforms, !isIe); + }); + + s.test("Multiple ranges per selection support", function(t) { + t.assertEquals(rangy.features.selectionSupportsMultipleRanges, isMozilla); + }); + + s.test("Collapsed non-editable selections support", function(t) { + t.assertEquals(rangy.features.collapsedNonEditableSelectionsSupported, !isOpera); + }); +}, false); diff --git a/test/index.html b/test/index.html index 205ce55e..6d0fde6c 100644 --- a/test/index.html +++ b/test/index.html @@ -15,6 +15,7 @@
  • Text range tests
  • Build tests
  • TextRange-to-Range performance tests
  • +
  • Feature tests
  • \ No newline at end of file