<!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>[167585] 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/167585">167585</a></dd>
<dt>Author</dt> <dd>benjamin@webkit.org</dd>
<dt>Date</dt> <dd>2014-04-20 23:26:20 -0700 (Sun, 20 Apr 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Compile the :root pseudo class and fix a related issue with :nth-child()
https://bugs.webkit.org/show_bug.cgi?id=131926

Reviewed by Andreas Kling.


Source/WebCore: 
Add the :root pseudo class. This is another trivial selector, we just need to compare
the element pointer with the documentElement.

I discovered some issues with :nth-child(n) through the layout tests for &quot;:root&quot;.
When the pseudo class nth-child could match anything, no code was generated. That decision
was taken when generating the fragments.

The specification of :nth-child() has two tests: the parent test and the counter test.
Since some fragments would not generate any code for :nth-child(n), they would succeed on the root,
which is incorrect since the root should fail the parent test.

This was fixed by moving the filtering of non-counting :nth-child() after we generate the parent
check.
We still don't generate any counter test unless required.

Test: fast/selectors/nth-child-on-root.html

* cssjit/SelectorCompiler.cpp:
(WebCore::SelectorCompiler::addPseudoClassType):
(WebCore::SelectorCompiler::SelectorCodeGenerator::generateElementMatching):
(WebCore::SelectorCompiler::SelectorCodeGenerator::generateElementIsNthChild):
(WebCore::SelectorCompiler::SelectorCodeGenerator::generateElementIsRoot):
* dom/Document.h:
(WebCore::Document::documentElementMemoryOffset):

LayoutTests: 
Add more test coverage that would have caught the bug with :nth-child(n).

* fast/selectors/nth-child-on-root-expected.txt: Added.
* fast/selectors/nth-child-on-root.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorecssjitSelectorCompilercpp">trunk/Source/WebCore/cssjit/SelectorCompiler.cpp</a></li>
<li><a href="#trunkSourceWebCoredomDocumenth">trunk/Source/WebCore/dom/Document.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsfastselectorsnthchildonrootexpectedtxt">trunk/LayoutTests/fast/selectors/nth-child-on-root-expected.txt</a></li>
<li><a href="#trunkLayoutTestsfastselectorsnthchildonroothtml">trunk/LayoutTests/fast/selectors/nth-child-on-root.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (167584 => 167585)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2014-04-21 06:08:05 UTC (rev 167584)
+++ trunk/LayoutTests/ChangeLog        2014-04-21 06:26:20 UTC (rev 167585)
</span><span class="lines">@@ -1,5 +1,17 @@
</span><span class="cx"> 2014-04-20  Benjamin Poulain  &lt;benjamin@webkit.org&gt;
</span><span class="cx"> 
</span><ins>+        Compile the :root pseudo class and fix a related issue with :nth-child()
+        https://bugs.webkit.org/show_bug.cgi?id=131926
+
+        Reviewed by Andreas Kling.
+
+        Add more test coverage that would have caught the bug with :nth-child(n).
+
+        * fast/selectors/nth-child-on-root-expected.txt: Added.
+        * fast/selectors/nth-child-on-root.html: Added.
+
+2014-04-20  Benjamin Poulain  &lt;benjamin@webkit.org&gt;
+
</ins><span class="cx">         Add Element.matches, the standard name for webkitMatchesSelector
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=131922
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsfastselectorsnthchildonrootexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/selectors/nth-child-on-root-expected.txt (0 => 167585)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/selectors/nth-child-on-root-expected.txt                                (rev 0)
+++ trunk/LayoutTests/fast/selectors/nth-child-on-root-expected.txt        2014-04-21 06:26:20 UTC (rev 167585)
</span><span class="lines">@@ -0,0 +1,20 @@
</span><ins>+Verify the nth-child() pseudo class matcher always test for the parent element. Some :nth-child pseudo selectors can skip counting the siblings, but they should never skip the parent check.
+
+On success, you will see a series of &quot;PASS&quot; messages, followed by &quot;TEST COMPLETE&quot;.
+
+
+PASS document.querySelectorAll(&quot;html:nth-child(1)&quot;).length is 0
+PASS document.querySelectorAll(&quot;html:nth-child(n)&quot;).length is 0
+PASS document.querySelectorAll(&quot;html:nth-child(n+1)&quot;).length is 0
+PASS document.querySelectorAll(&quot;:root:nth-child(1)&quot;).length is 0
+PASS document.querySelectorAll(&quot;:root:nth-child(n)&quot;).length is 0
+PASS document.querySelectorAll(&quot;:root:nth-child(n+1)&quot;).length is 0
+PASS getComputedStyle(document.documentElement).backgroundColor is &quot;rgb(255, 255, 255)&quot;
+PASS document.querySelectorAll(&quot;svg:root&quot;).length is 0
+PASS document.querySelectorAll(&quot;svg:nth-child(1)&quot;).length is 1
+PASS document.querySelectorAll(&quot;svg:nth-child(n)&quot;).length is 1
+PASS getComputedStyle(document.querySelector(&quot;svg&quot;)).backgroundColor is &quot;rgb(1, 2, 3)&quot;
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
</ins></span></pre></div>
<a id="trunkLayoutTestsfastselectorsnthchildonroothtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/selectors/nth-child-on-root.html (0 => 167585)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/selectors/nth-child-on-root.html                                (rev 0)
+++ trunk/LayoutTests/fast/selectors/nth-child-on-root.html        2014-04-21 06:26:20 UTC (rev 167585)
</span><span class="lines">@@ -0,0 +1,60 @@
</span><ins>+&lt;!doctype html&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;script src=&quot;../../resources/js-test-pre.js&quot;&gt;&lt;/script&gt;
+&lt;style&gt;
+html {
+    background-color:white;
+}
+html:nth-child(1) {
+    background-color:rgb(1, 2, 3);
+}
+html:nth-child(n) {
+    background-color:rgb(1, 2, 3);
+}
+html:nth-child(n+1) {
+    background-color:rgb(1, 2, 3);
+}
+
+:root:nth-child(1) {
+    background-color:rgb(1, 2, 3);
+}
+:root:nth-child(n) {
+    background-color:rgb(1, 2, 3);
+}
+:root:nth-child(n+1) {
+    background-color:rgb(1, 2, 3);
+}
+
+svg:nth-child(n) {
+    background-color:rgb(1, 2, 3);
+}
+svg:root {
+    background-color:rgb(4, 5, 6);
+}
+&lt;/style&gt;
+&lt;/head&gt;
+&lt;body&gt;
+    &lt;div style=&quot;display:none&quot;&gt;
+        &lt;svg&gt;&lt;g&gt;&lt;/g&gt;&lt;/svg&gt;
+    &lt;/div&gt;
+&lt;/body&gt;
+&lt;script&gt;
+description('Verify the nth-child() pseudo class matcher always test for the parent element. Some :nth-child pseudo selectors can skip counting the siblings, but they should never skip the parent check.');
+
+shouldBe('document.querySelectorAll(&quot;html:nth-child(1)&quot;).length', '0');
+shouldBe('document.querySelectorAll(&quot;html:nth-child(n)&quot;).length', '0');
+shouldBe('document.querySelectorAll(&quot;html:nth-child(n+1)&quot;).length', '0');
+shouldBe('document.querySelectorAll(&quot;:root:nth-child(1)&quot;).length', '0');
+shouldBe('document.querySelectorAll(&quot;:root:nth-child(n)&quot;).length', '0');
+shouldBe('document.querySelectorAll(&quot;:root:nth-child(n+1)&quot;).length', '0');
+shouldBeEqualToString('getComputedStyle(document.documentElement).backgroundColor', 'rgb(255, 255, 255)');
+
+// This svg document is not the root, &quot;:root&quot; should not match anything, nth-child should work.
+shouldBe('document.querySelectorAll(&quot;svg:root&quot;).length', '0');
+shouldBe('document.querySelectorAll(&quot;svg:nth-child(1)&quot;).length', '1');
+shouldBe('document.querySelectorAll(&quot;svg:nth-child(n)&quot;).length', '1');
+shouldBeEqualToString('getComputedStyle(document.querySelector(&quot;svg&quot;)).backgroundColor', 'rgb(1, 2, 3)');
+&lt;/script&gt;
+&lt;script src=&quot;../../resources/js-test-post.js&quot;&gt;&lt;/script&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (167584 => 167585)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2014-04-21 06:08:05 UTC (rev 167584)
+++ trunk/Source/WebCore/ChangeLog        2014-04-21 06:26:20 UTC (rev 167585)
</span><span class="lines">@@ -1,5 +1,37 @@
</span><span class="cx"> 2014-04-20  Benjamin Poulain  &lt;benjamin@webkit.org&gt;
</span><span class="cx"> 
</span><ins>+        Compile the :root pseudo class and fix a related issue with :nth-child()
+        https://bugs.webkit.org/show_bug.cgi?id=131926
+
+        Reviewed by Andreas Kling.
+
+        Add the :root pseudo class. This is another trivial selector, we just need to compare
+        the element pointer with the documentElement.
+
+        I discovered some issues with :nth-child(n) through the layout tests for &quot;:root&quot;.
+        When the pseudo class nth-child could match anything, no code was generated. That decision
+        was taken when generating the fragments.
+
+        The specification of :nth-child() has two tests: the parent test and the counter test.
+        Since some fragments would not generate any code for :nth-child(n), they would succeed on the root,
+        which is incorrect since the root should fail the parent test.
+
+        This was fixed by moving the filtering of non-counting :nth-child() after we generate the parent
+        check.
+        We still don't generate any counter test unless required.
+
+        Test: fast/selectors/nth-child-on-root.html
+
+        * cssjit/SelectorCompiler.cpp:
+        (WebCore::SelectorCompiler::addPseudoClassType):
+        (WebCore::SelectorCompiler::SelectorCodeGenerator::generateElementMatching):
+        (WebCore::SelectorCompiler::SelectorCodeGenerator::generateElementIsNthChild):
+        (WebCore::SelectorCompiler::SelectorCodeGenerator::generateElementIsRoot):
+        * dom/Document.h:
+        (WebCore::Document::documentElementMemoryOffset):
+
+2014-04-20  Benjamin Poulain  &lt;benjamin@webkit.org&gt;
+
</ins><span class="cx">         Add Element.matches, the standard name for webkitMatchesSelector
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=131922
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorecssjitSelectorCompilercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/cssjit/SelectorCompiler.cpp (167584 => 167585)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/cssjit/SelectorCompiler.cpp        2014-04-21 06:08:05 UTC (rev 167584)
+++ trunk/Source/WebCore/cssjit/SelectorCompiler.cpp        2014-04-21 06:26:20 UTC (rev 167585)
</span><span class="lines">@@ -190,6 +190,7 @@
</span><span class="cx">     void generateElementHasClasses(Assembler::JumpList&amp; failureCases, const LocalRegister&amp; elementDataAddress, const Vector&lt;const AtomicStringImpl*&gt;&amp; classNames);
</span><span class="cx">     void generateElementIsLink(Assembler::JumpList&amp; failureCases);
</span><span class="cx">     void generateElementIsNthChild(Assembler::JumpList&amp; failureCases, const SelectorFragment&amp;);
</span><ins>+    void generateElementIsRoot(Assembler::JumpList&amp; failureCases);
</ins><span class="cx">     void generateElementIsTarget(Assembler::JumpList&amp; failureCases);
</span><span class="cx"> 
</span><span class="cx">     // Helpers.
</span><span class="lines">@@ -319,6 +320,7 @@
</span><span class="cx">         return FunctionType::SimpleSelectorChecker;
</span><span class="cx"> 
</span><span class="cx">     case CSSSelector::PseudoClassLink:
</span><ins>+    case CSSSelector::PseudoClassRoot:
</ins><span class="cx">     case CSSSelector::PseudoClassTarget:
</span><span class="cx">         fragment.pseudoClasses.add(type);
</span><span class="cx">         return FunctionType::SimpleSelectorChecker;
</span><span class="lines">@@ -343,10 +345,6 @@
</span><span class="cx">             if (a &lt;= 0 &amp;&amp; b &lt; 1)
</span><span class="cx">                 return FunctionType::CannotMatchAnything;
</span><span class="cx"> 
</span><del>-            // Anything modulo 1 is zero. Unless b restrict the range, this does not filter anything out.
-            if (a == 1 &amp;&amp; (!b || (b == 1)))
-                return FunctionType::SimpleSelectorChecker;
-
</del><span class="cx">             fragment.nthChildfilters.append(std::pair&lt;int, int&gt;(a, b));
</span><span class="cx">             if (selectorContext == SelectorContext::QuerySelector)
</span><span class="cx">                 return FunctionType::SimpleSelectorChecker;
</span><span class="lines">@@ -1168,6 +1166,9 @@
</span><span class="cx">     if (fragment.pseudoClasses.contains(CSSSelector::PseudoClassLink))
</span><span class="cx">         generateElementIsLink(failureCases);
</span><span class="cx"> 
</span><ins>+    if (fragment.pseudoClasses.contains(CSSSelector::PseudoClassRoot))
+        generateElementIsRoot(failureCases);
+
</ins><span class="cx">     if (fragment.pseudoClasses.contains(CSSSelector::PseudoClassTarget))
</span><span class="cx">         generateElementIsTarget(failureCases);
</span><span class="cx"> 
</span><span class="lines">@@ -1942,6 +1943,20 @@
</span><span class="cx">     Assembler::RegisterID parentElement = m_registerAllocator.allocateRegister();
</span><span class="cx">     generateWalkToParentElement(failureCases, parentElement);
</span><span class="cx"> 
</span><ins>+    Vector&lt;std::pair&lt;int, int&gt;&gt; validSubsetFilters;
+    validSubsetFilters.reserveInitialCapacity(fragment.nthChildfilters.size());
+    for (const auto&amp; slot : fragment.nthChildfilters) {
+        int a = slot.first;
+        int b = slot.second;
+
+        // Anything modulo 1 is zero. Unless b restricts the range, this does not filter anything out.
+        if (a == 1 &amp;&amp; (!b || (b == 1)))
+            continue;
+        validSubsetFilters.uncheckedAppend(slot);
+    }
+    if (validSubsetFilters.isEmpty())
+        return;
+
</ins><span class="cx">     // Setup the counter at 1.
</span><span class="cx">     LocalRegister elementCounter(m_registerAllocator);
</span><span class="cx">     m_assembler.move(Assembler::TrustedImm32(1), elementCounter);
</span><span class="lines">@@ -2018,7 +2033,7 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     // Test every the nth-child filter.
</span><del>-    for (const auto&amp; slot : fragment.nthChildfilters) {
</del><ins>+    for (const auto&amp; slot : validSubsetFilters) {
</ins><span class="cx">         int a = slot.first;
</span><span class="cx">         int b = slot.second;
</span><span class="cx"> 
</span><span class="lines">@@ -2043,6 +2058,13 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void SelectorCodeGenerator::generateElementIsRoot(Assembler::JumpList&amp; failureCases)
+{
+    LocalRegister document(m_registerAllocator);
+    getDocument(m_assembler, elementAddressRegister, document);
+    failureCases.append(m_assembler.branchPtr(Assembler::NotEqual, Assembler::Address(document, Document::documentElementMemoryOffset()), elementAddressRegister));
+}
+
</ins><span class="cx"> void SelectorCodeGenerator::generateElementIsTarget(Assembler::JumpList&amp; failureCases)
</span><span class="cx"> {
</span><span class="cx">     LocalRegister document(m_registerAllocator);
</span></span></pre></div>
<a id="trunkSourceWebCoredomDocumenth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Document.h (167584 => 167585)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Document.h        2014-04-21 06:08:05 UTC (rev 167584)
+++ trunk/Source/WebCore/dom/Document.h        2014-04-21 06:26:20 UTC (rev 167585)
</span><span class="lines">@@ -406,6 +406,7 @@
</span><span class="cx">     {
</span><span class="cx">         return m_documentElement.get();
</span><span class="cx">     }
</span><ins>+    static ptrdiff_t documentElementMemoryOffset() { return OBJECT_OFFSETOF(Document, m_documentElement); }
</ins><span class="cx"> 
</span><span class="cx">     Element* activeElement();
</span><span class="cx">     bool hasFocus() const;
</span></span></pre>
</div>
</div>

</body>
</html>