Test getSelection() and the Selection interface
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Wed, 16 Nov 2011 12:38:09 -0700
changeset 677 eccd9a0d408e
parent 676 0c4c345c2c63
child 678 91a2c1c3f552
Test getSelection() and the Selection interface
editing.html
selecttest/getSelection.html
selecttest/interfaces.html
source.html
--- a/editing.html	Tue Nov 15 12:14:14 2011 -0700
+++ b/editing.html	Wed Nov 16 12:38:09 2011 -0700
@@ -67,7 +67,7 @@
 <body class=draft>
 <div class=head id=head>
 <h1>HTML Editing APIs</h1>
-<h2 class="no-num no-toc" id=work-in-progress-&mdash;-last-update-15-november-2011>Work in Progress &mdash; Last Update 15 November 2011</h2>
+<h2 class="no-num no-toc" id=work-in-progress-&mdash;-last-update-16-november-2011>Work in Progress &mdash; Last Update 16 November 2011</h2>
 <dl>
  <dt>Editor
  <dd>Aryeh Gregor &lt;<a href=mailto:ayg@aryeh.name>ayg@aryeh.name</a>&gt;
@@ -1226,9 +1226,19 @@
   <a href=#selection>Selection</a> <a href=#dom-document-getselection title=dom-Document-getSelection>getSelection</a>();
 };</pre>
 
+<p class=comments>Originally Gecko returned the stringification here (<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=636512">bug 636512</a> fixed
+that).  Now, what happens if you create a Document object with no defaultView
+(say via <code title="">document.implementation.createHTMLDocument("")</code>) and call
+<code title="">getSelection()</code> on it?  IE9 seems to return a different Selection
+object.  Firefox 9.0a2 and Opera 12.00 return the same object as for the
+current window.  Chrome 16 dev returns null.  We go with Firefox/Opera, since
+it's likely the oldest behavior and makes the most sense.  TODO: Is the wording
+about prototypes the best way to identify the correct global object?
+
 <p>The <dfn id=dom-document-getselection title=dom-Document-getSelection><code>getSelection()</code></dfn>
 method on the <code class=external data-anolis-spec=dom><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#document>Document</a></code> interface must return the same
-<code><a href=#selection>Selection</a></code> object as its <code class=external data-anolis-spec=html title=dom-Document-defaultView><a href=http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#dom-document-defaultview>defaultView</a></code>'s <code title=dom-Window-getSelection><a href=#dom-window-getselection>getSelection()</a></code> method.
+<code><a href=#selection>Selection</a></code> object as calling <code title=dom-Window-getSelection><a href=#dom-window-getselection>getSelection()</a></code> on the <code class=external data-anolis-spec=html><a href=http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#window>Window</a></code> object on which the <code class=external data-anolis-spec=dom><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#document>Document</a></code>'s
+prototype resides.
 
 <pre class=idl>partial interface <a class=external data-anolis-spec=html href=http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#window>Window</a> {
   <a href=#selection>Selection</a> <a href=#dom-window-getselection title=dom-Window-getSelection>getSelection</a>();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/selecttest/getSelection.html	Wed Nov 16 12:38:09 2011 -0700
@@ -0,0 +1,74 @@
+<!doctype html>
+<title>getSelection() tests</title>
+<div id=log></div>
+<script src=/resources/testharnessreport.js></script>
+<script src=/resources/testharness.js></script>
+<script>
+"use strict";
+
+test(function() {
+	// Sanity checks like this are to flag known browser bugs with clearer
+	// error messages, instead of throwing inscrutable exceptions.
+	assert_true("Selection" in window, "window must have Selection property");
+
+	assert_true(window.getSelection() instanceof Selection);
+}, "window.getSelection() instanceof Selection");
+
+test(function() {
+	assert_true("Selection" in window, "window must have Selection property");
+
+	assert_true(document.getSelection() instanceof Selection);
+}, "document.getSelection() instanceof Selection");
+
+test(function() {
+	assert_equals(window.getSelection(), document.getSelection());
+}, "window.getSelection() === document.getSelection()");
+
+test(function() {
+	assert_true("Selection" in window, "window must have Selection property");
+
+	var doc = document.implementation.createHTMLDocument("");
+	assert_true(doc.getSelection() instanceof Selection,
+		"getSelection() on HTML document with no browsing context must be instanceof Selection");
+	assert_equals(doc.getSelection(), document.getSelection(),
+		"getSelection() on HTML document with no browsing context must be same as the window");
+}, "HTML document with no browsing context");
+
+test(function() {
+	var xmlDoc = document.implementation.createDocument(null, "", null);
+
+	assert_true("getSelection" in xmlDoc, "XML document must have getSelection()");
+	assert_true("Selection" in window, "window must have Selection property");
+
+	assert_true(xmlDoc.getSelection() instanceof Selection,
+		"getSelection() on XML document with no browsing context must be instanceof Selection");
+	assert_equals(xmlDoc.getSelection(), document.getSelection(),
+		"getSelection() on XML document with no browsing context must be same as the window");
+}, "XML document with no browsing context");
+
+var iframe = document.createElement("iframe");
+document.body.appendChild(iframe);
+test(function() {
+	assert_true("Selection" in iframe.contentWindow, "window must have Selection property");
+
+	assert_true(iframe.contentWindow.getSelection() instanceof iframe.contentWindow.Selection,
+		"window.getSelection() instanceof Selection");
+	assert_true(iframe.contentDocument.getSelection() instanceof iframe.contentWindow.Selection,
+		"document.getSelection() instanceof Selection");
+	assert_equals(iframe.contentWindow.getSelection(), iframe.contentDocument.getSelection(),
+		"window.getSelection() === document.getSelection()");
+	assert_not_equals(iframe.contentWindow.getSelection(), getSelection(),
+		"getSelection() inside and outside iframe must return different objects");
+}, "In an iframe");
+
+test(function() {
+	assert_true("Selection" in iframe.contentWindow, "window must have Selection property");
+
+	var doc = iframe.contentDocument.implementation.createHTMLDocument("");
+	assert_true(doc.getSelection() instanceof iframe.contentWindow.Selection,
+		"getSelection() on HTML document with no browsing context must be instanceof Selection");
+	assert_equals(doc.getSelection(), iframe.contentDocument.getSelection(),
+		"getSelection() on HTML document with no browsing context must be same as the window");
+}, "HTML document with no browsing context inside an iframe");
+document.body.removeChild(iframe);
+</script>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/selecttest/interfaces.html	Wed Nov 16 12:38:09 2011 -0700
@@ -0,0 +1,36 @@
+<!doctype html>
+<title>Selection interface tests</title>
+<div id=log></div>
+<script src=/resources/testharnessreport.js></script>
+<script src=/resources/testharness.js></script>
+<script src=/resources/WebIDLParser.js></script>
+<script src=/resources/idlharness.js></script>
+<script>
+"use strict";
+
+new IdlInterface('interface Selection {\n\
+  readonly attribute Node? anchorNode;\n\
+  readonly attribute unsigned long anchorOffset;\n\
+  readonly attribute Node? focusNode;\n\
+  readonly attribute unsigned long focusOffset;\n\
+\n\
+  readonly attribute boolean isCollapsed;\n\
+  void               collapse(Node parentNode, unsigned long offset);\n\
+  void               collapseToStart();\n\
+  void               collapseToEnd();\n\
+\n\
+  void               extend(Node parentNode, unsigned long offset);\n\
+  void               modify(DOMString alter, DOMString direction, DOMString granularity);\n\
+\n\
+  void               selectAllChildren(Node parentNode);\n\
+  void               deleteFromDocument();\n\
+\n\
+  readonly attribute unsigned long rangeCount;\n\
+  Range              getRangeAt(unsigned long index);\n\
+  void               addRange(Range range);\n\
+  void               removeRange(Range range);\n\
+  void               removeAllRanges();\n\
+\n\
+  stringifier;\n\
+};').test();
+</script>
--- a/source.html	Tue Nov 15 12:14:14 2011 -0700
+++ b/source.html	Wed Nov 16 12:38:09 2011 -0700
@@ -1176,11 +1176,22 @@
   <span>Selection</span> <span title=dom-Document-getSelection>getSelection</span>();
 };</pre>
 
+<p class=comments>Originally Gecko returned the stringification here (<a
+href=https://bugzilla.mozilla.org/show_bug.cgi?id=636512>bug 636512</a> fixed
+that).  Now, what happens if you create a Document object with no defaultView
+(say via {{code|document.implementation.createHTMLDocument("")}}) and call
+{{code|getSelection()}} on it?  IE9 seems to return a different Selection
+object.  Firefox 9.0a2 and Opera 12.00 return the same object as for the
+current window.  Chrome 16 dev returns null.  We go with Firefox/Opera, since
+it's likely the oldest behavior and makes the most sense.  TODO: Is the wording
+about prototypes the best way to identify the correct global object?
+
 <p>The <dfn title=dom-Document-getSelection><code>getSelection()</code></dfn>
 method on the [[document]] interface must return the same
-<code>Selection</code> object as its <code data-anolis-spec=html
-title=dom-Document-defaultView>defaultView</code>'s <code
-title=dom-Window-getSelection>getSelection()</code> method.
+<code>Selection</code> object as calling <code
+title=dom-Window-getSelection>getSelection()</code> on the <code
+data-anolis-spec=html>Window</code> object on which the [[document]]'s
+prototype resides.
 
 <pre class=idl>partial interface <span data-anolis-spec=html>Window</span> {
   <span>Selection</span> <span title=dom-Window-getSelection>getSelection</span>();