<!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>[174324] 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/174324">174324</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2014-10-04 15:24:21 -0700 (Sat, 04 Oct 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Implement Element.closest() API
https://bugs.webkit.org/show_bug.cgi?id=137418

Patch by Dhi Aurrahman &lt;diorahman@rockybars.com&gt; on 2014-10-04
Reviewed by Benjamin Poulain.

Source/WebCore:

Implement Element.closest() API as specified in [1].

[1]: https://dom.spec.whatwg.org/#dom-element-closest

Tests: fast/selectors/closest-general.html
       fast/selectors/closest-scope.html

* dom/Element.cpp:
(WebCore::Element::closest):
* dom/Element.h:
* dom/Element.idl:
* dom/SelectorQuery.cpp:
(WebCore::SelectorDataList::selectorClosest):
(WebCore::SelectorDataList::closest):
* dom/SelectorQuery.h:
(WebCore::SelectorQuery::closest):

LayoutTests:

Add test cases for Element.closest() API

* fast/selectors/closest-general-expected.txt: Added.
* fast/selectors/closest-general.html: Added.
* fast/selectors/closest-scope-expected.txt: Added.
* fast/selectors/closest-scope.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="#trunkSourceWebCoredomElementcpp">trunk/Source/WebCore/dom/Element.cpp</a></li>
<li><a href="#trunkSourceWebCoredomElementh">trunk/Source/WebCore/dom/Element.h</a></li>
<li><a href="#trunkSourceWebCoredomElementidl">trunk/Source/WebCore/dom/Element.idl</a></li>
<li><a href="#trunkSourceWebCoredomSelectorQuerycpp">trunk/Source/WebCore/dom/SelectorQuery.cpp</a></li>
<li><a href="#trunkSourceWebCoredomSelectorQueryh">trunk/Source/WebCore/dom/SelectorQuery.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsfastselectorsclosestgeneralexpectedtxt">trunk/LayoutTests/fast/selectors/closest-general-expected.txt</a></li>
<li><a href="#trunkLayoutTestsfastselectorsclosestgeneralhtml">trunk/LayoutTests/fast/selectors/closest-general.html</a></li>
<li><a href="#trunkLayoutTestsfastselectorsclosestscopeexpectedtxt">trunk/LayoutTests/fast/selectors/closest-scope-expected.txt</a></li>
<li><a href="#trunkLayoutTestsfastselectorsclosestscopehtml">trunk/LayoutTests/fast/selectors/closest-scope.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (174323 => 174324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2014-10-04 22:11:57 UTC (rev 174323)
+++ trunk/LayoutTests/ChangeLog        2014-10-04 22:24:21 UTC (rev 174324)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2014-10-04  Dhi Aurrahman  &lt;diorahman@rockybars.com&gt;
+
+        Implement Element.closest() API
+        https://bugs.webkit.org/show_bug.cgi?id=137418
+
+        Reviewed by Benjamin Poulain.
+
+        Add test cases for Element.closest() API
+
+        * fast/selectors/closest-general-expected.txt: Added.
+        * fast/selectors/closest-general.html: Added.
+        * fast/selectors/closest-scope-expected.txt: Added.
+        * fast/selectors/closest-scope.html: Added.
+
</ins><span class="cx"> 2014-10-04  Filip Pizlo  &lt;fpizlo@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         FTL should sink PutLocals
</span></span></pre></div>
<a id="trunkLayoutTestsfastselectorsclosestgeneralexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/selectors/closest-general-expected.txt (0 => 174324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/selectors/closest-general-expected.txt                                (rev 0)
+++ trunk/LayoutTests/fast/selectors/closest-general-expected.txt        2014-10-04 22:24:21 UTC (rev 174324)
</span><span class="lines">@@ -0,0 +1,61 @@
</span><ins>+This test makes sure the closest() API works correctly
+
+On success, you will see a series of &quot;PASS&quot; messages, followed by &quot;TEST COMPLETE&quot;.
+
+
+PASS theTarget.closest(&quot;#theTarget&quot;) is theTarget
+PASS theTarget.closest(&quot;ancestor&quot;) is ancestor
+PASS theTarget.closest(&quot;tribe ancestor&quot;) is ancestor
+PASS theTarget.closest(&quot;tribe &gt; ancestor&quot;) is ancestor
+PASS theTarget.closest(&quot;realm + ancestor&quot;) is ancestor
+PASS theTarget.closest(&quot;realm ~ ancestor&quot;) is ancestor
+PASS theTarget.closest(&quot;tribe, ancestor&quot;) is ancestor
+PASS theTarget.closest(&quot;ancestor, tribe&quot;) is ancestor
+PASS theTarget.closest(&quot;tribe realm&quot;) is null
+PASS theTarget.closest(&quot;tribe realm throne&quot;) is null
+PASS theTarget.closest(&quot;tribe realm ancestor&quot;) is null
+PASS theTarget.closest(&quot;realm &gt; ancestor&quot;) is null
+PASS theTarget.closest(&quot;throne + ancestor&quot;) is null
+PASS theTarget.closest(&quot;throne ~ ancestor&quot;) is null
+PASS theTarget.closest(&quot;.classic&quot;) is ancestor
+FAIL theTarget.closest(&quot;john&quot;) should be [object HTMLUnknownElement]. Was null.
+PASS theTarget.closest(&quot;doe&quot;) is null
+PASS theTarget.closest(&quot;ancestor[name=old]&quot;) is ancestor
+PASS theTarget.closest(&quot;ancestor[name=young]&quot;) is null
+PASS theTarget.closest(null) is null
+PASS theTarget.closest(undefined) is null
+PASS sour.closest(&quot;lemon&quot;) is sour
+PASS sour.closest(&quot;a, b, c, d, e, f, g, h, i&quot;) is i
+PASS sour.closest(&quot;a, b, c, d, e, f, g, h&quot;) is h
+PASS sour.closest(&quot;a, b, c, d, e, f, g&quot;) is g
+PASS sour.closest(&quot;a, b, c, d, e, f&quot;) is f
+PASS sour.closest(&quot;a, b, c, d, e&quot;) is e
+PASS sour.closest(&quot;a, b, c&quot;) is c
+PASS sour.closest(&quot;a, b&quot;) is b
+PASS sour.closest(&quot;a&quot;) is a
+PASS sour.closest(&quot;i, h, g, f, e, d, c, b, a&quot;) is i
+PASS sour.closest(&quot;h, g, f, e, d, c, b, a&quot;) is h
+PASS sour.closest(&quot;g, f, e, d, c, b, a&quot;) is g
+PASS sour.closest(&quot;f, e, d, c, b, a&quot;) is f
+PASS sour.closest(&quot;e, d, c, b, a&quot;) is e
+PASS sour.closest(&quot;d, c, b, a&quot;) is d
+PASS sour.closest(&quot;c, b, a&quot;) is c
+PASS sour.closest(&quot;b, a&quot;) is b
+PASS sour.closest(&quot;a&quot;) is a
+PASS document.closest is undefined
+PASS document.closest() threw exception TypeError: undefined is not a function (evaluating 'document.closest()').
+PASS theTarget.closest() threw exception TypeError: Not enough arguments.
+PASS theTarget.closest(&quot;&quot;) threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(&quot;.123&quot;) threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(&quot;    &quot;) threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(&quot;)&quot;) threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(&quot;(&quot;) threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(&quot;()&quot;) threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(&quot;^_^&quot;) threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(&quot;{&quot;) threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(&quot;}&quot;) threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(&quot;{}&quot;) threw exception Error: SyntaxError: DOM Exception 12.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
</ins></span></pre></div>
<a id="trunkLayoutTestsfastselectorsclosestgeneralhtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/selectors/closest-general.html (0 => 174324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/selectors/closest-general.html                                (rev 0)
+++ trunk/LayoutTests/fast/selectors/closest-general.html        2014-10-04 22:24:21 UTC (rev 174324)
</span><span class="lines">@@ -0,0 +1,122 @@
</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;script&gt;
+if (window.testRunner)
+    testRunner.dumpAsText();
+&lt;/script&gt;
+&lt;/head&gt;
+&lt;body&gt;
+        &lt;tribe&gt;
+                &lt;realm&gt;
+                        &lt;throne&gt;&lt;/throne&gt;
+                        &lt;ancestor id=&quot;doe&quot; name=&quot;young&quot; class=&quot;classic&quot;&gt;
+                                &lt;target id=&quot;anotherTarget&quot;&gt;&lt;/target&gt;
+                        &lt;/ancestor&gt;
+                &lt;/realm&gt;
+                &lt;ancestor id=&quot;john&quot; name=&quot;old&quot; class=&quot;classic&quot;&gt;
+                    &lt;sibling&gt;&lt;/sibling&gt;
+                    &lt;target id=&quot;theTarget&quot; webkit=&quot;fast&quot;&gt;&lt;/target&gt;
+            &lt;/ancestor&gt;
+    &lt;/tribe&gt;
+    &lt;foo&gt;
+            &lt;bar&gt;
+                    &lt;a id=&quot;a&quot;&gt;
+                            &lt;b id=&quot;b&quot;&gt;
+                                    &lt;c id=&quot;c&quot;&gt;
+                                            &lt;d id=&quot;d&quot;&gt;
+                                                    &lt;e id=&quot;e&quot;&gt;
+                                                            &lt;f id=&quot;f&quot;&gt;
+                                                                    &lt;g id=&quot;g&quot;&gt;
+                                                                            &lt;h id=&quot;h&quot;&gt;
+                                                                                    &lt;i id=&quot;i&quot;&gt;
+                                                                                            &lt;lemon id=&quot;sour&quot;&gt;&lt;/lemon&gt;
+                                                                                    &lt;/i&gt;
+                                                                            &lt;/h&gt;
+                                                                    &lt;/g&gt;
+                                                            &lt;f/&gt;
+                                                    &lt;e/&gt;
+                                            &lt;/d&gt;
+                                    &lt;c/&gt;
+                            &lt;/b&gt;
+                    &lt;/a&gt;
+            &lt;/bar&gt;
+    &lt;/foo&gt;
+&lt;/body&gt;
+&lt;script&gt;
+description('This test makes sure the closest() API works correctly');
+
+var theTarget = document.getElementById('theTarget');
+var ancestor = document.getElementById('john');
+var sour = document.getElementById('sour');
+var a = document.getElementById('a');
+var b = document.getElementById('b');
+var c = document.getElementById('c');
+var d = document.getElementById('d');
+var e = document.getElementById('e');
+var f = document.getElementById('f');
+var g = document.getElementById('g');
+var h = document.getElementById('h');
+var i = document.getElementById('i');
+
+shouldBe('theTarget.closest(&quot;#theTarget&quot;)', 'theTarget');
+shouldBe('theTarget.closest(&quot;ancestor&quot;)', 'ancestor');
+shouldBe('theTarget.closest(&quot;tribe ancestor&quot;)', 'ancestor');
+shouldBe('theTarget.closest(&quot;tribe &gt; ancestor&quot;)', 'ancestor');
+shouldBe('theTarget.closest(&quot;realm + ancestor&quot;)', 'ancestor');
+shouldBe('theTarget.closest(&quot;realm ~ ancestor&quot;)', 'ancestor');
+shouldBe('theTarget.closest(&quot;tribe, ancestor&quot;)', 'ancestor');
+shouldBe('theTarget.closest(&quot;ancestor, tribe&quot;)', 'ancestor');
+
+shouldBeNull('theTarget.closest(&quot;tribe realm&quot;)');
+shouldBeNull('theTarget.closest(&quot;tribe realm throne&quot;)');
+shouldBeNull('theTarget.closest(&quot;tribe realm ancestor&quot;)');
+shouldBeNull('theTarget.closest(&quot;realm &gt; ancestor&quot;)');
+shouldBeNull('theTarget.closest(&quot;throne + ancestor&quot;)');
+shouldBeNull('theTarget.closest(&quot;throne ~ ancestor&quot;)');
+
+shouldBe('theTarget.closest(&quot;.classic&quot;)', 'ancestor');
+shouldBe('theTarget.closest(&quot;john&quot;)', 'ancestor');
+shouldBeNull('theTarget.closest(&quot;doe&quot;)');
+shouldBe('theTarget.closest(&quot;ancestor[name=old]&quot;)', 'ancestor');
+shouldBeNull('theTarget.closest(&quot;ancestor[name=young]&quot;)', 'ancestor');
+
+shouldBeNull('theTarget.closest(null)');
+shouldBeNull('theTarget.closest(undefined)');
+
+shouldBe('sour.closest(&quot;lemon&quot;)', 'sour');
+shouldBe('sour.closest(&quot;a, b, c, d, e, f, g, h, i&quot;)', 'i');
+shouldBe('sour.closest(&quot;a, b, c, d, e, f, g, h&quot;)', 'h');
+shouldBe('sour.closest(&quot;a, b, c, d, e, f, g&quot;)', 'g');
+shouldBe('sour.closest(&quot;a, b, c, d, e, f&quot;)', 'f');
+shouldBe('sour.closest(&quot;a, b, c, d, e&quot;)', 'e');
+shouldBe('sour.closest(&quot;a, b, c&quot;)', 'c');
+shouldBe('sour.closest(&quot;a, b&quot;)', 'b');
+shouldBe('sour.closest(&quot;a&quot;)', 'a');
+shouldBe('sour.closest(&quot;i, h, g, f, e, d, c, b, a&quot;)', 'i');
+shouldBe('sour.closest(&quot;h, g, f, e, d, c, b, a&quot;)', 'h');
+shouldBe('sour.closest(&quot;g, f, e, d, c, b, a&quot;)', 'g');
+shouldBe('sour.closest(&quot;f, e, d, c, b, a&quot;)', 'f');
+shouldBe('sour.closest(&quot;e, d, c, b, a&quot;)', 'e');
+shouldBe('sour.closest(&quot;d, c, b, a&quot;)', 'd');
+shouldBe('sour.closest(&quot;c, b, a&quot;)', 'c');
+shouldBe('sour.closest(&quot;b, a&quot;)', 'b');
+shouldBe('sour.closest(&quot;a&quot;)', 'a');
+
+shouldBe('document.closest', 'undefined');
+shouldThrow('document.closest()');
+shouldThrow('theTarget.closest()');
+shouldThrow('theTarget.closest(&quot;&quot;)');
+shouldThrow('theTarget.closest(&quot;.123&quot;)');
+shouldThrow('theTarget.closest(&quot;    &quot;)');
+shouldThrow('theTarget.closest(&quot;)&quot;)');
+shouldThrow('theTarget.closest(&quot;(&quot;)');
+shouldThrow('theTarget.closest(&quot;()&quot;)');
+shouldThrow('theTarget.closest(&quot;^_^&quot;)');
+shouldThrow('theTarget.closest(&quot;{&quot;)');
+shouldThrow('theTarget.closest(&quot;}&quot;)');
+shouldThrow('theTarget.closest(&quot;{}&quot;)');
+&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="trunkLayoutTestsfastselectorsclosestscopeexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/selectors/closest-scope-expected.txt (0 => 174324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/selectors/closest-scope-expected.txt                                (rev 0)
+++ trunk/LayoutTests/fast/selectors/closest-scope-expected.txt        2014-10-04 22:24:21 UTC (rev 174324)
</span><span class="lines">@@ -0,0 +1,28 @@
</span><ins>+This test makes sure that :scope works correctly with the closest() API.
+
+On success, you will see a series of &quot;PASS&quot; messages, followed by &quot;TEST COMPLETE&quot;.
+
+
+PASS theTarget.closest(&quot;:scope&quot;) is theTarget
+PASS theTarget.closest(&quot;:not(:scope)&quot;) is body
+PASS theTarget.closest(&quot;body :scope&quot;) is theTarget
+PASS theTarget.closest(&quot;body &gt; :scope&quot;) is theTarget
+PASS theTarget.closest(&quot;body:scope&quot;) is null
+PASS theTarget.closest(&quot;sibling + :scope&quot;) is theTarget
+PASS theTarget.closest(&quot;sibling ~ :scope&quot;) is theTarget
+PASS theTarget.closest(&quot;#theTarget:scope&quot;) is theTarget
+PASS theTarget.closest(&quot;:scope#theTarget&quot;) is theTarget
+PASS theTarget.closest(&quot;[webkit]:scope#theTarget&quot;) is theTarget
+PASS theTarget.closest(&quot;:not([webkit=fast]):scope#theTarget&quot;) is null
+PASS theTarget.closest(&quot;:scope target&quot;) is null
+PASS theTarget.closest(&quot;:scope &gt; target&quot;) is null
+PASS theTarget.closest(&quot;:scope + target&quot;) is null
+PASS theTarget.closest(&quot;:scope ~ target&quot;) is null
+PASS theTarget.closest(&quot;:scope *&quot;) is null
+PASS theTarget.closest(&quot;:scope &gt; *&quot;) is null
+PASS theTarget.closest(&quot;:scope + *&quot;) is null
+PASS theTarget.closest(&quot;:scope ~ *&quot;) is null
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
</ins></span></pre></div>
<a id="trunkLayoutTestsfastselectorsclosestscopehtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/selectors/closest-scope.html (0 => 174324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/selectors/closest-scope.html                                (rev 0)
+++ trunk/LayoutTests/fast/selectors/closest-scope.html        2014-10-04 22:24:21 UTC (rev 174324)
</span><span class="lines">@@ -0,0 +1,47 @@
</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;script&gt;
+if (window.testRunner)
+        testRunner.dumpAsText();
+&lt;/script&gt;
+&lt;/head&gt;
+&lt;body&gt;
+        &lt;sibling&gt;&lt;/sibling&gt;
+        &lt;target id=&quot;theTarget&quot; webkit=&quot;fast&quot;&gt;&lt;/target&gt;
+&lt;/body&gt;
+&lt;script&gt;
+description('This test makes sure that :scope works correctly with the closest() API.');
+
+var theTarget = document.getElementById('theTarget');
+var body = document.body;
+
+shouldBe('theTarget.closest(&quot;:scope&quot;)', 'theTarget');
+shouldBe('theTarget.closest(&quot;:not(:scope)&quot;)', 'body');
+
+shouldBe('theTarget.closest(&quot;body :scope&quot;)', 'theTarget');
+shouldBe('theTarget.closest(&quot;body &gt; :scope&quot;)', 'theTarget');
+shouldBeNull('theTarget.closest(&quot;body:scope&quot;)');
+
+shouldBe('theTarget.closest(&quot;sibling + :scope&quot;)', 'theTarget');
+shouldBe('theTarget.closest(&quot;sibling ~ :scope&quot;)', 'theTarget');
+
+shouldBe('theTarget.closest(&quot;#theTarget:scope&quot;)', 'theTarget');
+shouldBe('theTarget.closest(&quot;:scope#theTarget&quot;)', 'theTarget');
+
+shouldBe('theTarget.closest(&quot;[webkit]:scope#theTarget&quot;)', 'theTarget');
+shouldBeNull('theTarget.closest(&quot;:not([webkit=fast]):scope#theTarget&quot;)');
+
+shouldBeNull('theTarget.closest(&quot;:scope target&quot;)');
+shouldBeNull('theTarget.closest(&quot;:scope &gt; target&quot;)');
+shouldBeNull('theTarget.closest(&quot;:scope + target&quot;)');
+shouldBeNull('theTarget.closest(&quot;:scope ~ target&quot;)');
+
+shouldBeNull('theTarget.closest(&quot;:scope *&quot;)');
+shouldBeNull('theTarget.closest(&quot;:scope &gt; *&quot;)');
+shouldBeNull('theTarget.closest(&quot;:scope + *&quot;)');
+shouldBeNull('theTarget.closest(&quot;:scope ~ *&quot;)');
+&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 (174323 => 174324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2014-10-04 22:11:57 UTC (rev 174323)
+++ trunk/Source/WebCore/ChangeLog        2014-10-04 22:24:21 UTC (rev 174324)
</span><span class="lines">@@ -1,3 +1,27 @@
</span><ins>+2014-10-04  Dhi Aurrahman  &lt;diorahman@rockybars.com&gt;
+
+        Implement Element.closest() API
+        https://bugs.webkit.org/show_bug.cgi?id=137418
+
+        Reviewed by Benjamin Poulain.
+
+        Implement Element.closest() API as specified in [1].
+
+        [1]: https://dom.spec.whatwg.org/#dom-element-closest
+
+        Tests: fast/selectors/closest-general.html
+               fast/selectors/closest-scope.html
+
+        * dom/Element.cpp:
+        (WebCore::Element::closest):
+        * dom/Element.h:
+        * dom/Element.idl:
+        * dom/SelectorQuery.cpp:
+        (WebCore::SelectorDataList::selectorClosest):
+        (WebCore::SelectorDataList::closest):
+        * dom/SelectorQuery.h:
+        (WebCore::SelectorQuery::closest):
+
</ins><span class="cx"> 2014-10-04  Brian J. Burg  &lt;burg@cs.washington.edu&gt;
</span><span class="cx"> 
</span><span class="cx">         Unreviewed, rolling out r174319.
</span></span></pre></div>
<a id="trunkSourceWebCoredomElementcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Element.cpp (174323 => 174324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Element.cpp        2014-10-04 22:11:57 UTC (rev 174323)
+++ trunk/Source/WebCore/dom/Element.cpp        2014-10-04 22:24:21 UTC (rev 174324)
</span><span class="lines">@@ -2321,6 +2321,14 @@
</span><span class="cx">     return selectorQuery &amp;&amp; selectorQuery-&gt;matches(*this);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+Element* Element::closest(const String&amp; selector, ExceptionCode&amp; ec)
+{
+    SelectorQuery* selectorQuery = document().selectorQueryForString(selector, ec);
+    if (selectorQuery)
+        return selectorQuery-&gt;closest(*this);
+    return nullptr;
+}
+
</ins><span class="cx"> bool Element::shouldAppearIndeterminate() const
</span><span class="cx"> {
</span><span class="cx">     return false;
</span></span></pre></div>
<a id="trunkSourceWebCoredomElementh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Element.h (174323 => 174324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Element.h        2014-10-04 22:11:57 UTC (rev 174323)
+++ trunk/Source/WebCore/dom/Element.h        2014-10-04 22:24:21 UTC (rev 174324)
</span><span class="lines">@@ -452,6 +452,7 @@
</span><span class="cx"> 
</span><span class="cx">     virtual bool matchesReadWritePseudoClass() const;
</span><span class="cx">     bool matches(const String&amp; selectors, ExceptionCode&amp;);
</span><ins>+    Element* closest(const String&amp; selectors, ExceptionCode&amp;);
</ins><span class="cx">     virtual bool shouldAppearIndeterminate() const;
</span><span class="cx"> 
</span><span class="cx">     DOMTokenList&amp; classList();
</span></span></pre></div>
<a id="trunkSourceWebCoredomElementidl"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Element.idl (174323 => 174324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Element.idl        2014-10-04 22:11:57 UTC (rev 174323)
+++ trunk/Source/WebCore/dom/Element.idl        2014-10-04 22:24:21 UTC (rev 174324)
</span><span class="lines">@@ -121,6 +121,7 @@
</span><span class="cx">     [RaisesException] NodeList querySelectorAll(DOMString selectors);
</span><span class="cx"> 
</span><span class="cx">     [RaisesException] boolean matches([Default=Undefined] optional DOMString selectors);
</span><ins>+    [RaisesException] Element closest(DOMString selectors);    
</ins><span class="cx">     [ImplementedAs=matches, RaisesException] boolean webkitMatchesSelector([Default=Undefined] optional DOMString selectors);
</span><span class="cx"> 
</span><span class="cx">     // ElementTraversal API
</span></span></pre></div>
<a id="trunkSourceWebCoredomSelectorQuerycpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/SelectorQuery.cpp (174323 => 174324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/SelectorQuery.cpp        2014-10-04 22:11:57 UTC (rev 174323)
+++ trunk/Source/WebCore/dom/SelectorQuery.cpp        2014-10-04 22:24:21 UTC (rev 174324)
</span><span class="lines">@@ -120,6 +120,17 @@
</span><span class="cx">     return selectorChecker.match(selectorData.selector, &amp;element, selectorCheckingContext);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+inline Element* SelectorDataList::selectorClosest(const SelectorData&amp; selectorData, Element&amp; element, const ContainerNode&amp; rootNode) const
+{
+    SelectorChecker selectorChecker(element.document());
+    SelectorChecker::CheckingContext selectorCheckingContext(SelectorChecker::Mode::QueryingRules);
+    selectorCheckingContext.scope = rootNode.isDocumentNode() ? nullptr : &amp;rootNode;
+    Element* currentNode = &amp;element;
+    if (!selectorChecker.match(selectorData.selector, currentNode, selectorCheckingContext))
+        return nullptr;
+    return currentNode;
+}
+
</ins><span class="cx"> bool SelectorDataList::matches(Element&amp; targetElement) const
</span><span class="cx"> {
</span><span class="cx">     unsigned selectorCount = m_selectors.size();
</span><span class="lines">@@ -130,6 +141,20 @@
</span><span class="cx">     return false;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+Element* SelectorDataList::closest(Element&amp; targetElement) const
+{
+    Element* currentNode = &amp;targetElement;
+    do {
+        for (auto&amp; selector : m_selectors) {
+            Element* candidateElement = selectorClosest(selector, *currentNode, targetElement);
+            if (candidateElement)
+                return candidateElement;
+        }
+        currentNode = currentNode-&gt;parentElement();
+    } while (currentNode);
+    return nullptr;
+}
+
</ins><span class="cx"> struct AllElementExtractorSelectorQueryTrait {
</span><span class="cx">     typedef Vector&lt;Ref&lt;Element&gt;&gt; OutputType;
</span><span class="cx">     static const bool shouldOnlyMatchFirstElement = false;
</span></span></pre></div>
<a id="trunkSourceWebCoredomSelectorQueryh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/SelectorQuery.h (174323 => 174324)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/SelectorQuery.h        2014-10-04 22:11:57 UTC (rev 174323)
+++ trunk/Source/WebCore/dom/SelectorQuery.h        2014-10-04 22:24:21 UTC (rev 174324)
</span><span class="lines">@@ -49,6 +49,7 @@
</span><span class="cx"> public:
</span><span class="cx">     explicit SelectorDataList(const CSSSelectorList&amp;);
</span><span class="cx">     bool matches(Element&amp;) const;
</span><ins>+    Element* closest(Element&amp;) const;
</ins><span class="cx">     RefPtr&lt;NodeList&gt; queryAll(ContainerNode&amp; rootNode) const;
</span><span class="cx">     Element* queryFirst(ContainerNode&amp; rootNode) const;
</span><span class="cx"> 
</span><span class="lines">@@ -79,6 +80,7 @@
</span><span class="cx">     };
</span><span class="cx"> 
</span><span class="cx">     bool selectorMatches(const SelectorData&amp;, Element&amp;, const ContainerNode&amp; rootNode) const;
</span><ins>+    Element* selectorClosest(const SelectorData&amp;, Element&amp;, const ContainerNode&amp; rootNode) const;
</ins><span class="cx"> 
</span><span class="cx">     template &lt;typename SelectorQueryTrait&gt; void execute(ContainerNode&amp; rootNode, typename SelectorQueryTrait::OutputType&amp;) const;
</span><span class="cx">     template &lt;typename SelectorQueryTrait&gt; void executeFastPathForIdSelector(const ContainerNode&amp; rootNode, const SelectorData&amp;, const CSSSelector* idSelector, typename SelectorQueryTrait::OutputType&amp;) const;
</span><span class="lines">@@ -116,6 +118,7 @@
</span><span class="cx"> public:
</span><span class="cx">     explicit SelectorQuery(CSSSelectorList&amp;&amp;);
</span><span class="cx">     bool matches(Element&amp;) const;
</span><ins>+    Element* closest(Element&amp;) const;
</ins><span class="cx">     RefPtr&lt;NodeList&gt; queryAll(ContainerNode&amp; rootNode) const;
</span><span class="cx">     Element* queryFirst(ContainerNode&amp; rootNode) const;
</span><span class="cx"> 
</span><span class="lines">@@ -139,6 +142,11 @@
</span><span class="cx">     return m_selectors.matches(element);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+inline Element* SelectorQuery::closest(Element&amp; element) const
+{
+    return m_selectors.closest(element);
+}
+
</ins><span class="cx"> inline RefPtr&lt;NodeList&gt; SelectorQuery::queryAll(ContainerNode&amp; rootNode) const
</span><span class="cx"> {
</span><span class="cx">     return m_selectors.queryAll(rootNode);
</span></span></pre>
</div>
</div>

</body>
</html>