Title: [143239] trunk
Revision
143239
Author
mk...@chromium.org
Date
2013-02-18 10:21:59 -0800 (Mon, 18 Feb 2013)

Log Message

compareDocumentPosition reports disconnected nodes as following each other
https://bugs.webkit.org/show_bug.cgi?id=108274

Reviewed by Dimitri Glazkov.

Source/WebCore:

jQuery has had to implement their own sorting mechanism in Sizzle[1] due
to Node::compareDocumentPosition always reporting disconnected nodes
as following each other. According to spec[2], we should instead be
indicating that the result is (a) disconnected, (b) implementation
specific, and (c) deterministically ordered.

[1]: https://github.com/jquery/sizzle/commit/1c8aec91284af8d8c14447976235d5dd72b0d75e
[2]: http://dom.spec.whatwg.org/#dom-node-comparedocumentposition

Test: fast/dom/compare-document-position-disconnected-nodes.html

* dom/Node.cpp:
(WebCore::Node::compareDocumentPosition):
    After walking the parentNode chain of both Nodes, compare the root.
    If the Nodes don't share a root, they're in distinct trees, and
    should return as described above. We determine which element
    "preceeds" the other in an arbitrary fashion via pointer comparison.

LayoutTests:

* fast/dom/compare-document-position-disconnected-nodes-expected.txt: Added.
* fast/dom/compare-document-position-disconnected-nodes.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (143238 => 143239)


--- trunk/LayoutTests/ChangeLog	2013-02-18 18:06:06 UTC (rev 143238)
+++ trunk/LayoutTests/ChangeLog	2013-02-18 18:21:59 UTC (rev 143239)
@@ -1,3 +1,13 @@
+2013-02-18  Mike West  <mk...@chromium.org>
+
+        compareDocumentPosition reports disconnected nodes as following each other
+        https://bugs.webkit.org/show_bug.cgi?id=108274
+
+        Reviewed by Dimitri Glazkov.
+
+        * fast/dom/compare-document-position-disconnected-nodes-expected.txt: Added.
+        * fast/dom/compare-document-position-disconnected-nodes.html: Added.
+
 2013-02-18  Stephen White  <senorbla...@chromium.org>
 
         [chromium] Unreviewed gardening.

Added: trunk/LayoutTests/fast/dom/compare-document-position-disconnected-nodes-expected.txt (0 => 143239)


--- trunk/LayoutTests/fast/dom/compare-document-position-disconnected-nodes-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/dom/compare-document-position-disconnected-nodes-expected.txt	2013-02-18 18:21:59 UTC (rev 143239)
@@ -0,0 +1,10 @@
+PASS a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_DISCONNECTED is Node.DOCUMENT_POSITION_DISCONNECTED
+PASS b.compareDocumentPosition(a) & Node.DOCUMENT_POSITION_DISCONNECTED is Node.DOCUMENT_POSITION_DISCONNECTED
+PASS a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC is Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
+PASS b.compareDocumentPosition(a) & Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC is Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
+PASS a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_PRECEDING is not b.compareDocumentPosition(a) & Node.DOCUMENT_POSITION_PRECEDING
+PASS a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING is not b.compareDocumentPosition(a) & Node.DOCUMENT_POSITION_FOLLOWING
+PASS successfullyParsed is true
+
+TEST COMPLETE
+Comparing the positions of two disconnected nodes should result in DOCUMENT_POSITION_DISCONNECTED.

Added: trunk/LayoutTests/fast/dom/compare-document-position-disconnected-nodes.html (0 => 143239)


--- trunk/LayoutTests/fast/dom/compare-document-position-disconnected-nodes.html	                        (rev 0)
+++ trunk/LayoutTests/fast/dom/compare-document-position-disconnected-nodes.html	2013-02-18 18:21:59 UTC (rev 143239)
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <script src=""
+    <script>
+        window.a = document.createElement('a');
+        window.b = document.createElement('b');
+
+        shouldBe('a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_DISCONNECTED', 'Node.DOCUMENT_POSITION_DISCONNECTED');
+        shouldBe('b.compareDocumentPosition(a) & Node.DOCUMENT_POSITION_DISCONNECTED', 'Node.DOCUMENT_POSITION_DISCONNECTED');
+        shouldBe('a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC', 'Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC');
+        shouldBe('b.compareDocumentPosition(a) & Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC', 'Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC');
+        shouldNotBe('a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_PRECEDING', 'b.compareDocumentPosition(a) & Node.DOCUMENT_POSITION_PRECEDING');
+        shouldNotBe('a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING', 'b.compareDocumentPosition(a) & Node.DOCUMENT_POSITION_FOLLOWING');
+    </script>
+    <script src=""
+</head>
+<body>
+    <p>Comparing the positions of two disconnected nodes should result in DOCUMENT_POSITION_DISCONNECTED.</p>
+</body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (143238 => 143239)


--- trunk/Source/WebCore/ChangeLog	2013-02-18 18:06:06 UTC (rev 143238)
+++ trunk/Source/WebCore/ChangeLog	2013-02-18 18:21:59 UTC (rev 143239)
@@ -1,3 +1,28 @@
+2013-02-18  Mike West  <mk...@chromium.org>
+
+        compareDocumentPosition reports disconnected nodes as following each other
+        https://bugs.webkit.org/show_bug.cgi?id=108274
+
+        Reviewed by Dimitri Glazkov.
+
+        jQuery has had to implement their own sorting mechanism in Sizzle[1] due
+        to Node::compareDocumentPosition always reporting disconnected nodes
+        as following each other. According to spec[2], we should instead be
+        indicating that the result is (a) disconnected, (b) implementation
+        specific, and (c) deterministically ordered.
+
+        [1]: https://github.com/jquery/sizzle/commit/1c8aec91284af8d8c14447976235d5dd72b0d75e
+        [2]: http://dom.spec.whatwg.org/#dom-node-comparedocumentposition
+
+        Test: fast/dom/compare-document-position-disconnected-nodes.html
+
+        * dom/Node.cpp:
+        (WebCore::Node::compareDocumentPosition):
+            After walking the parentNode chain of both Nodes, compare the root.
+            If the Nodes don't share a root, they're in distinct trees, and
+            should return as described above. We determine which element
+            "preceeds" the other in an arbitrary fashion via pointer comparison.
+
 2013-02-18  Andrey Adaikin  <aand...@chromium.org>
 
         Web Inspector: [Canvas] fix replay log grouping by frames

Modified: trunk/Source/WebCore/dom/Node.cpp (143238 => 143239)


--- trunk/Source/WebCore/dom/Node.cpp	2013-02-18 18:06:06 UTC (rev 143238)
+++ trunk/Source/WebCore/dom/Node.cpp	2013-02-18 18:21:59 UTC (rev 143239)
@@ -1785,10 +1785,17 @@
         chain1.append(current);
     for (current = start2; current; current = current->parentNode())
         chain2.append(current);
-   
-    // Walk the two chains backwards and look for the first difference.
+
     unsigned index1 = chain1.size();
     unsigned index2 = chain2.size();
+
+    // If the two elements don't have a common root, they're not in the same tree.
+    if (chain1[index1 - 1] != chain2[index2 - 1]) {
+        unsigned short direction = (start1 > start2) ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING;
+        return DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | direction;
+    }
+
+    // Walk the two chains backwards and look for the first difference.
     for (unsigned i = min(index1, index2); i; --i) {
         Node* child1 = chain1[--index1];
         Node* child2 = chain2[--index2];
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to