<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[190187] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/190187">190187</a></dd>
<dt>Author</dt> <dd>cdumez@apple.com</dd>
<dt>Date</dt> <dd>2015-09-23 14:40:50 -0700 (Wed, 23 Sep 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>TreeWalker.previousSibling()  / nextSibling() does not behave according to the specification
https://bugs.webkit.org/show_bug.cgi?id=149493

Reviewed by Darin Adler.

LayoutTests/imported/w3c:

Rebaseline existing W3C DOM test now that more checks are passing.

* web-platform-tests/dom/traversal/TreeWalker-expected.txt:

Source/WebCore:

TreeWalker.previousSibling()  / nextSibling() does not behave according
to the specification:
- https://dom.spec.whatwg.org/#dom-treewalker-nextsibling
- https://dom.spec.whatwg.org/#dom-treewalker-previoussibling
- https://dom.spec.whatwg.org/#concept-traverse-siblings

In particular, the previous code would fail to update 'node' variable
in the case acceptNode() returned FILTER_REJECT. This patch fixes this
and refactors the function to match more closely the text of the spec
and avoid code duplication.

No new tests, already covered by existing test.

* dom/TreeWalker.cpp:
(WebCore::TreeWalker::traverseSiblings):
(WebCore::TreeWalker::previousSibling):
(WebCore::TreeWalker::nextSibling):
(WebCore::TreeWalker::previousNode): Deleted.
* dom/TreeWalker.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsimportedw3cChangeLog">trunk/LayoutTests/imported/w3c/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsimportedw3cwebplatformtestsdomtraversalTreeWalkerexpectedtxt">trunk/LayoutTests/imported/w3c/web-platform-tests/dom/traversal/TreeWalker-expected.txt</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoredomTreeWalkercpp">trunk/Source/WebCore/dom/TreeWalker.cpp</a></li>
<li><a href="#trunkSourceWebCoredomTreeWalkerh">trunk/Source/WebCore/dom/TreeWalker.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsimportedw3cChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/ChangeLog (190186 => 190187)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/ChangeLog        2015-09-23 21:03:30 UTC (rev 190186)
+++ trunk/LayoutTests/imported/w3c/ChangeLog        2015-09-23 21:40:50 UTC (rev 190187)
</span><span class="lines">@@ -1,5 +1,16 @@
</span><span class="cx"> 2015-09-23  Chris Dumez  &lt;cdumez@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        TreeWalker.previousSibling()  / nextSibling() does not behave according to the specification
+        https://bugs.webkit.org/show_bug.cgi?id=149493
+
+        Reviewed by Darin Adler.
+
+        Rebaseline existing W3C DOM test now that more checks are passing.
+
+        * web-platform-tests/dom/traversal/TreeWalker-expected.txt:
+
+2015-09-23  Chris Dumez  &lt;cdumez@apple.com&gt;
+
</ins><span class="cx">         Range's setStartBefore() / setStartAfter() / setEndBefore() / setEndAfter() do not match the specification
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=149490
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsimportedw3cwebplatformtestsdomtraversalTreeWalkerexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/dom/traversal/TreeWalker-expected.txt (190186 => 190187)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/web-platform-tests/dom/traversal/TreeWalker-expected.txt        2015-09-23 21:03:30 UTC (rev 190186)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/dom/traversal/TreeWalker-expected.txt        2015-09-23 21:40:50 UTC (rev 190187)
</span><span class="lines">@@ -146,7 +146,7 @@
</span><span class="cx"> PASS document.createTreeWalker(document, 0xFFFFFFFF, null) 
</span><span class="cx"> PASS document.createTreeWalker(document, 0xFFFFFFFF, (function(node) { return true })) 
</span><span class="cx"> PASS document.createTreeWalker(document, 0xFFFFFFFF, (function(node) { return false })) 
</span><del>-FAIL document.createTreeWalker(document, 0xFFFFFFFF, (function(node) { return node.nodeName[0] == '#' })) assert_equals: .nextSibling() expected Text node &quot;TreeWalker tests&quot; but got null
</del><ins>+PASS document.createTreeWalker(document, 0xFFFFFFFF, (function(node) { return node.nodeName[0] == '#' })) 
</ins><span class="cx"> PASS document.createTreeWalker(document, NodeFilter.SHOW_ELEMENT, null) 
</span><span class="cx"> PASS document.createTreeWalker(document, NodeFilter.SHOW_ELEMENT, (function(node) { return true })) 
</span><span class="cx"> PASS document.createTreeWalker(document, NodeFilter.SHOW_ELEMENT, (function(node) { return false })) 
</span><span class="lines">@@ -166,7 +166,7 @@
</span><span class="cx"> PASS document.createTreeWalker(detachedDiv, 0xFFFFFFFF, null) 
</span><span class="cx"> PASS document.createTreeWalker(detachedDiv, 0xFFFFFFFF, (function(node) { return true })) 
</span><span class="cx"> PASS document.createTreeWalker(detachedDiv, 0xFFFFFFFF, (function(node) { return false })) 
</span><del>-FAIL document.createTreeWalker(detachedDiv, 0xFFFFFFFF, (function(node) { return node.nodeName[0] == '#' })) assert_equals: .nextSibling() expected Text node &quot;Wxyzabcd&quot; but got null
</del><ins>+PASS document.createTreeWalker(detachedDiv, 0xFFFFFFFF, (function(node) { return node.nodeName[0] == '#' })) 
</ins><span class="cx"> PASS document.createTreeWalker(detachedDiv, NodeFilter.SHOW_ELEMENT, null) 
</span><span class="cx"> PASS document.createTreeWalker(detachedDiv, NodeFilter.SHOW_ELEMENT, (function(node) { return true })) 
</span><span class="cx"> PASS document.createTreeWalker(detachedDiv, NodeFilter.SHOW_ELEMENT, (function(node) { return false })) 
</span><span class="lines">@@ -186,7 +186,7 @@
</span><span class="cx"> PASS document.createTreeWalker(foreignDoc, 0xFFFFFFFF, null) 
</span><span class="cx"> PASS document.createTreeWalker(foreignDoc, 0xFFFFFFFF, (function(node) { return true })) 
</span><span class="cx"> PASS document.createTreeWalker(foreignDoc, 0xFFFFFFFF, (function(node) { return false })) 
</span><del>-FAIL document.createTreeWalker(foreignDoc, 0xFFFFFFFF, (function(node) { return node.nodeName[0] == '#' })) assert_equals: .nextSibling() expected Text node &quot;Efghijkl&quot; but got Comment node &lt;!--&quot;Commenter&quot; and &quot;commentator&quot; mean different things.  I'v...--&gt;
</del><ins>+PASS document.createTreeWalker(foreignDoc, 0xFFFFFFFF, (function(node) { return node.nodeName[0] == '#' })) 
</ins><span class="cx"> PASS document.createTreeWalker(foreignDoc, NodeFilter.SHOW_ELEMENT, null) 
</span><span class="cx"> PASS document.createTreeWalker(foreignDoc, NodeFilter.SHOW_ELEMENT, (function(node) { return true })) 
</span><span class="cx"> PASS document.createTreeWalker(foreignDoc, NodeFilter.SHOW_ELEMENT, (function(node) { return false })) 
</span><span class="lines">@@ -226,7 +226,7 @@
</span><span class="cx"> PASS document.createTreeWalker(xmlDoc, 0xFFFFFFFF, null) 
</span><span class="cx"> PASS document.createTreeWalker(xmlDoc, 0xFFFFFFFF, (function(node) { return true })) 
</span><span class="cx"> PASS document.createTreeWalker(xmlDoc, 0xFFFFFFFF, (function(node) { return false })) 
</span><del>-FAIL document.createTreeWalker(xmlDoc, 0xFFFFFFFF, (function(node) { return node.nodeName[0] == '#' })) assert_equals: .nextSibling() expected Text node &quot;do re mi fa so la ti&quot; but got Comment node &lt;!--I maliciously created a comment that will break incautiou...--&gt;
</del><ins>+PASS document.createTreeWalker(xmlDoc, 0xFFFFFFFF, (function(node) { return node.nodeName[0] == '#' })) 
</ins><span class="cx"> PASS document.createTreeWalker(xmlDoc, NodeFilter.SHOW_ELEMENT, null) 
</span><span class="cx"> PASS document.createTreeWalker(xmlDoc, NodeFilter.SHOW_ELEMENT, (function(node) { return true })) 
</span><span class="cx"> PASS document.createTreeWalker(xmlDoc, NodeFilter.SHOW_ELEMENT, (function(node) { return false })) 
</span><span class="lines">@@ -506,8 +506,7 @@
</span><span class="cx"> PASS document.createTreeWalker(testDiv, 0xFFFFFFFF, null) 
</span><span class="cx"> PASS document.createTreeWalker(testDiv, 0xFFFFFFFF, (function(node) { return true })) 
</span><span class="cx"> PASS document.createTreeWalker(testDiv, 0xFFFFFFFF, (function(node) { return false })) 
</span><del>-FAIL document.createTreeWalker(testDiv, 0xFFFFFFFF, (function(node) { return node.nodeName[0] == '#' })) assert_equals: .nextSibling() expected Text node &quot;Ijklmnop
-&quot; but got Comment node &lt;!--Alphabet soup?--&gt;
</del><ins>+PASS document.createTreeWalker(testDiv, 0xFFFFFFFF, (function(node) { return node.nodeName[0] == '#' })) 
</ins><span class="cx"> PASS document.createTreeWalker(testDiv, NodeFilter.SHOW_ELEMENT, null) 
</span><span class="cx"> PASS document.createTreeWalker(testDiv, NodeFilter.SHOW_ELEMENT, (function(node) { return true })) 
</span><span class="cx"> PASS document.createTreeWalker(testDiv, NodeFilter.SHOW_ELEMENT, (function(node) { return false })) 
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (190186 => 190187)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-09-23 21:03:30 UTC (rev 190186)
+++ trunk/Source/WebCore/ChangeLog        2015-09-23 21:40:50 UTC (rev 190187)
</span><span class="lines">@@ -1,3 +1,30 @@
</span><ins>+2015-09-23  Chris Dumez  &lt;cdumez@apple.com&gt;
+
+        TreeWalker.previousSibling()  / nextSibling() does not behave according to the specification
+        https://bugs.webkit.org/show_bug.cgi?id=149493
+
+        Reviewed by Darin Adler.
+
+        TreeWalker.previousSibling()  / nextSibling() does not behave according
+        to the specification:
+        - https://dom.spec.whatwg.org/#dom-treewalker-nextsibling
+        - https://dom.spec.whatwg.org/#dom-treewalker-previoussibling
+        - https://dom.spec.whatwg.org/#concept-traverse-siblings
+
+        In particular, the previous code would fail to update 'node' variable
+        in the case acceptNode() returned FILTER_REJECT. This patch fixes this
+        and refactors the function to match more closely the text of the spec
+        and avoid code duplication.
+
+        No new tests, already covered by existing test.
+
+        * dom/TreeWalker.cpp:
+        (WebCore::TreeWalker::traverseSiblings):
+        (WebCore::TreeWalker::previousSibling):
+        (WebCore::TreeWalker::nextSibling):
+        (WebCore::TreeWalker::previousNode): Deleted.
+        * dom/TreeWalker.h:
+
</ins><span class="cx"> 2015-09-23  Alex Christensen  &lt;achristensen@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Fix CMake build.
</span></span></pre></div>
<a id="trunkSourceWebCoredomTreeWalkercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/TreeWalker.cpp (190186 => 190187)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/TreeWalker.cpp        2015-09-23 21:03:30 UTC (rev 190186)
+++ trunk/Source/WebCore/dom/TreeWalker.cpp        2015-09-23 21:40:50 UTC (rev 190187)
</span><span class="lines">@@ -130,29 +130,24 @@
</span><span class="cx">     return nullptr;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-Node* TreeWalker::previousSibling()
</del><ins>+template&lt;TreeWalker::SiblingTraversalType type&gt; Node* TreeWalker::traverseSiblings()
</ins><span class="cx"> {
</span><span class="cx">     RefPtr&lt;Node&gt; node = m_current;
</span><span class="cx">     if (node == root())
</span><span class="cx">         return nullptr;
</span><del>-    while (1) {
-        for (RefPtr&lt;Node&gt; sibling = node-&gt;previousSibling(); sibling; ) {
</del><ins>+
+    auto isNext = type == SiblingTraversalType::Next;
+    while (true) {
+        for (RefPtr&lt;Node&gt; sibling = isNext ? node-&gt;nextSibling() : node-&gt;previousSibling(); sibling; ) {
</ins><span class="cx">             short acceptNodeResult = acceptNode(sibling.get());
</span><del>-            switch (acceptNodeResult) {
-                case NodeFilter::FILTER_ACCEPT:
-                    m_current = sibling.release();
-                    return m_current.get();
-                case NodeFilter::FILTER_SKIP:
-                    if (sibling-&gt;lastChild()) {
-                        sibling = sibling-&gt;lastChild();
-                        node = sibling;
-                        continue;
-                    }
-                    break;
-                case NodeFilter::FILTER_REJECT:
-                    break;
</del><ins>+            if (acceptNodeResult == NodeFilter::FILTER_ACCEPT) {
+                m_current = WTF::move(sibling);
+                return m_current.get();
</ins><span class="cx">             }
</span><del>-            sibling = sibling-&gt;previousSibling();
</del><ins>+            node = sibling;
+            sibling = isNext ? sibling-&gt;firstChild() : sibling-&gt;lastChild();
+            if (acceptNodeResult == NodeFilter::FILTER_REJECT || !sibling)
+                sibling = isNext ? node-&gt;nextSibling() : node-&gt;previousSibling();
</ins><span class="cx">         }
</span><span class="cx">         node = node-&gt;parentNode();
</span><span class="cx">         if (!node || node == root())
</span><span class="lines">@@ -163,37 +158,14 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+Node* TreeWalker::previousSibling()
+{
+    return traverseSiblings&lt;SiblingTraversalType::Previous&gt;();
+}
+
</ins><span class="cx"> Node* TreeWalker::nextSibling()
</span><span class="cx"> {
</span><del>-    RefPtr&lt;Node&gt; node = m_current;
-    if (node == root())
-        return nullptr;
-    while (1) {
-        for (RefPtr&lt;Node&gt; sibling = node-&gt;nextSibling(); sibling; ) {
-            short acceptNodeResult = acceptNode(sibling.get());
-            switch (acceptNodeResult) {
-                case NodeFilter::FILTER_ACCEPT:
-                    m_current = sibling.release();
-                    return m_current.get();
-                case NodeFilter::FILTER_SKIP:
-                    if (sibling-&gt;firstChild()) {
-                        sibling = sibling-&gt;firstChild();
-                        node = sibling;
-                        continue;
-                    }
-                    break;
-                case NodeFilter::FILTER_REJECT:
-                    break;
-            }
-            sibling = sibling-&gt;nextSibling();
-        }
-        node = node-&gt;parentNode();
-        if (!node || node == root())
-            return nullptr;
-        short acceptNodeResult = acceptNode(node.get());
-        if (acceptNodeResult == NodeFilter::FILTER_ACCEPT)
-            return nullptr;
-    }
</del><ins>+    return traverseSiblings&lt;SiblingTraversalType::Next&gt;();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> Node* TreeWalker::previousNode()
</span></span></pre></div>
<a id="trunkSourceWebCoredomTreeWalkerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/TreeWalker.h (190186 => 190187)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/TreeWalker.h        2015-09-23 21:03:30 UTC (rev 190186)
+++ trunk/Source/WebCore/dom/TreeWalker.h        2015-09-23 21:40:50 UTC (rev 190187)
</span><span class="lines">@@ -55,6 +55,8 @@
</span><span class="cx"> 
</span><span class="cx">     private:
</span><span class="cx">         TreeWalker(PassRefPtr&lt;Node&gt;, unsigned long whatToShow, RefPtr&lt;NodeFilter&gt;&amp;&amp;);
</span><ins>+        enum class SiblingTraversalType { Previous, Next };
+        template&lt;SiblingTraversalType&gt; Node* traverseSiblings();
</ins><span class="cx">         
</span><span class="cx">         Node* setCurrent(PassRefPtr&lt;Node&gt;);
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>