<!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 <diorahman@rockybars.com> 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 <diorahman@rockybars.com>
+
+ 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 <fpizlo@apple.com>
</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 "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS theTarget.closest("#theTarget") is theTarget
+PASS theTarget.closest("ancestor") is ancestor
+PASS theTarget.closest("tribe ancestor") is ancestor
+PASS theTarget.closest("tribe > ancestor") is ancestor
+PASS theTarget.closest("realm + ancestor") is ancestor
+PASS theTarget.closest("realm ~ ancestor") is ancestor
+PASS theTarget.closest("tribe, ancestor") is ancestor
+PASS theTarget.closest("ancestor, tribe") is ancestor
+PASS theTarget.closest("tribe realm") is null
+PASS theTarget.closest("tribe realm throne") is null
+PASS theTarget.closest("tribe realm ancestor") is null
+PASS theTarget.closest("realm > ancestor") is null
+PASS theTarget.closest("throne + ancestor") is null
+PASS theTarget.closest("throne ~ ancestor") is null
+PASS theTarget.closest(".classic") is ancestor
+FAIL theTarget.closest("john") should be [object HTMLUnknownElement]. Was null.
+PASS theTarget.closest("doe") is null
+PASS theTarget.closest("ancestor[name=old]") is ancestor
+PASS theTarget.closest("ancestor[name=young]") is null
+PASS theTarget.closest(null) is null
+PASS theTarget.closest(undefined) is null
+PASS sour.closest("lemon") is sour
+PASS sour.closest("a, b, c, d, e, f, g, h, i") is i
+PASS sour.closest("a, b, c, d, e, f, g, h") is h
+PASS sour.closest("a, b, c, d, e, f, g") is g
+PASS sour.closest("a, b, c, d, e, f") is f
+PASS sour.closest("a, b, c, d, e") is e
+PASS sour.closest("a, b, c") is c
+PASS sour.closest("a, b") is b
+PASS sour.closest("a") is a
+PASS sour.closest("i, h, g, f, e, d, c, b, a") is i
+PASS sour.closest("h, g, f, e, d, c, b, a") is h
+PASS sour.closest("g, f, e, d, c, b, a") is g
+PASS sour.closest("f, e, d, c, b, a") is f
+PASS sour.closest("e, d, c, b, a") is e
+PASS sour.closest("d, c, b, a") is d
+PASS sour.closest("c, b, a") is c
+PASS sour.closest("b, a") is b
+PASS sour.closest("a") 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("") threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(".123") threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(" ") threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest(")") threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest("(") threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest("()") threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest("^_^") threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest("{") threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest("}") threw exception Error: SyntaxError: DOM Exception 12.
+PASS theTarget.closest("{}") 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>+<!doctype html>
+<html>
+<head>
+<script src="../../resources/js-test-pre.js"></script>
+<script>
+if (window.testRunner)
+ testRunner.dumpAsText();
+</script>
+</head>
+<body>
+        <tribe>
+                <realm>
+                        <throne></throne>
+                        <ancestor id="doe" name="young" class="classic">
+                                <target id="anotherTarget"></target>
+                        </ancestor>
+                </realm>
+                <ancestor id="john" name="old" class="classic">
+                 <sibling></sibling>
+                 <target id="theTarget" webkit="fast"></target>
+         </ancestor>
+ </tribe>
+ <foo>
+         <bar>
+                 <a id="a">
+                         <b id="b">
+                                 <c id="c">
+                                         <d id="d">
+                                                 <e id="e">
+                                                         <f id="f">
+                                                                 <g id="g">
+                                                                         <h id="h">
+                                                                                 <i id="i">
+                                                                                         <lemon id="sour"></lemon>
+                                                                                 </i>
+                                                                         </h>
+                                                                 </g>
+                                                         <f/>
+                                                 <e/>
+                                         </d>
+                                 <c/>
+                         </b>
+                 </a>
+         </bar>
+ </foo>
+</body>
+<script>
+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("#theTarget")', 'theTarget');
+shouldBe('theTarget.closest("ancestor")', 'ancestor');
+shouldBe('theTarget.closest("tribe ancestor")', 'ancestor');
+shouldBe('theTarget.closest("tribe > ancestor")', 'ancestor');
+shouldBe('theTarget.closest("realm + ancestor")', 'ancestor');
+shouldBe('theTarget.closest("realm ~ ancestor")', 'ancestor');
+shouldBe('theTarget.closest("tribe, ancestor")', 'ancestor');
+shouldBe('theTarget.closest("ancestor, tribe")', 'ancestor');
+
+shouldBeNull('theTarget.closest("tribe realm")');
+shouldBeNull('theTarget.closest("tribe realm throne")');
+shouldBeNull('theTarget.closest("tribe realm ancestor")');
+shouldBeNull('theTarget.closest("realm > ancestor")');
+shouldBeNull('theTarget.closest("throne + ancestor")');
+shouldBeNull('theTarget.closest("throne ~ ancestor")');
+
+shouldBe('theTarget.closest(".classic")', 'ancestor');
+shouldBe('theTarget.closest("john")', 'ancestor');
+shouldBeNull('theTarget.closest("doe")');
+shouldBe('theTarget.closest("ancestor[name=old]")', 'ancestor');
+shouldBeNull('theTarget.closest("ancestor[name=young]")', 'ancestor');
+
+shouldBeNull('theTarget.closest(null)');
+shouldBeNull('theTarget.closest(undefined)');
+
+shouldBe('sour.closest("lemon")', 'sour');
+shouldBe('sour.closest("a, b, c, d, e, f, g, h, i")', 'i');
+shouldBe('sour.closest("a, b, c, d, e, f, g, h")', 'h');
+shouldBe('sour.closest("a, b, c, d, e, f, g")', 'g');
+shouldBe('sour.closest("a, b, c, d, e, f")', 'f');
+shouldBe('sour.closest("a, b, c, d, e")', 'e');
+shouldBe('sour.closest("a, b, c")', 'c');
+shouldBe('sour.closest("a, b")', 'b');
+shouldBe('sour.closest("a")', 'a');
+shouldBe('sour.closest("i, h, g, f, e, d, c, b, a")', 'i');
+shouldBe('sour.closest("h, g, f, e, d, c, b, a")', 'h');
+shouldBe('sour.closest("g, f, e, d, c, b, a")', 'g');
+shouldBe('sour.closest("f, e, d, c, b, a")', 'f');
+shouldBe('sour.closest("e, d, c, b, a")', 'e');
+shouldBe('sour.closest("d, c, b, a")', 'd');
+shouldBe('sour.closest("c, b, a")', 'c');
+shouldBe('sour.closest("b, a")', 'b');
+shouldBe('sour.closest("a")', 'a');
+
+shouldBe('document.closest', 'undefined');
+shouldThrow('document.closest()');
+shouldThrow('theTarget.closest()');
+shouldThrow('theTarget.closest("")');
+shouldThrow('theTarget.closest(".123")');
+shouldThrow('theTarget.closest(" ")');
+shouldThrow('theTarget.closest(")")');
+shouldThrow('theTarget.closest("(")');
+shouldThrow('theTarget.closest("()")');
+shouldThrow('theTarget.closest("^_^")');
+shouldThrow('theTarget.closest("{")');
+shouldThrow('theTarget.closest("}")');
+shouldThrow('theTarget.closest("{}")');
+</script>
+<script src="../../resources/js-test-post.js"></script>
+</html>
</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 "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS theTarget.closest(":scope") is theTarget
+PASS theTarget.closest(":not(:scope)") is body
+PASS theTarget.closest("body :scope") is theTarget
+PASS theTarget.closest("body > :scope") is theTarget
+PASS theTarget.closest("body:scope") is null
+PASS theTarget.closest("sibling + :scope") is theTarget
+PASS theTarget.closest("sibling ~ :scope") is theTarget
+PASS theTarget.closest("#theTarget:scope") is theTarget
+PASS theTarget.closest(":scope#theTarget") is theTarget
+PASS theTarget.closest("[webkit]:scope#theTarget") is theTarget
+PASS theTarget.closest(":not([webkit=fast]):scope#theTarget") is null
+PASS theTarget.closest(":scope target") is null
+PASS theTarget.closest(":scope > target") is null
+PASS theTarget.closest(":scope + target") is null
+PASS theTarget.closest(":scope ~ target") is null
+PASS theTarget.closest(":scope *") is null
+PASS theTarget.closest(":scope > *") is null
+PASS theTarget.closest(":scope + *") is null
+PASS theTarget.closest(":scope ~ *") 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>+<!doctype html>
+<html>
+<head>
+<script src="../../resources/js-test-pre.js"></script>
+<script>
+if (window.testRunner)
+        testRunner.dumpAsText();
+</script>
+</head>
+<body>
+        <sibling></sibling>
+        <target id="theTarget" webkit="fast"></target>
+</body>
+<script>
+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(":scope")', 'theTarget');
+shouldBe('theTarget.closest(":not(:scope)")', 'body');
+
+shouldBe('theTarget.closest("body :scope")', 'theTarget');
+shouldBe('theTarget.closest("body > :scope")', 'theTarget');
+shouldBeNull('theTarget.closest("body:scope")');
+
+shouldBe('theTarget.closest("sibling + :scope")', 'theTarget');
+shouldBe('theTarget.closest("sibling ~ :scope")', 'theTarget');
+
+shouldBe('theTarget.closest("#theTarget:scope")', 'theTarget');
+shouldBe('theTarget.closest(":scope#theTarget")', 'theTarget');
+
+shouldBe('theTarget.closest("[webkit]:scope#theTarget")', 'theTarget');
+shouldBeNull('theTarget.closest(":not([webkit=fast]):scope#theTarget")');
+
+shouldBeNull('theTarget.closest(":scope target")');
+shouldBeNull('theTarget.closest(":scope > target")');
+shouldBeNull('theTarget.closest(":scope + target")');
+shouldBeNull('theTarget.closest(":scope ~ target")');
+
+shouldBeNull('theTarget.closest(":scope *")');
+shouldBeNull('theTarget.closest(":scope > *")');
+shouldBeNull('theTarget.closest(":scope + *")');
+shouldBeNull('theTarget.closest(":scope ~ *")');
+</script>
+<script src="../../resources/js-test-post.js"></script>
+</html>
</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 <diorahman@rockybars.com>
+
+ 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 <burg@cs.washington.edu>
</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 && selectorQuery->matches(*this);
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+Element* Element::closest(const String& selector, ExceptionCode& ec)
+{
+ SelectorQuery* selectorQuery = document().selectorQueryForString(selector, ec);
+ if (selectorQuery)
+ return selectorQuery->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& selectors, ExceptionCode&);
</span><ins>+ Element* closest(const String& selectors, ExceptionCode&);
</ins><span class="cx"> virtual bool shouldAppearIndeterminate() const;
</span><span class="cx">
</span><span class="cx"> DOMTokenList& 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, &element, selectorCheckingContext);
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+inline Element* SelectorDataList::selectorClosest(const SelectorData& selectorData, Element& element, const ContainerNode& rootNode) const
+{
+ SelectorChecker selectorChecker(element.document());
+ SelectorChecker::CheckingContext selectorCheckingContext(SelectorChecker::Mode::QueryingRules);
+ selectorCheckingContext.scope = rootNode.isDocumentNode() ? nullptr : &rootNode;
+ Element* currentNode = &element;
+ if (!selectorChecker.match(selectorData.selector, currentNode, selectorCheckingContext))
+ return nullptr;
+ return currentNode;
+}
+
</ins><span class="cx"> bool SelectorDataList::matches(Element& 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& targetElement) const
+{
+ Element* currentNode = &targetElement;
+ do {
+ for (auto& selector : m_selectors) {
+ Element* candidateElement = selectorClosest(selector, *currentNode, targetElement);
+ if (candidateElement)
+ return candidateElement;
+ }
+ currentNode = currentNode->parentElement();
+ } while (currentNode);
+ return nullptr;
+}
+
</ins><span class="cx"> struct AllElementExtractorSelectorQueryTrait {
</span><span class="cx"> typedef Vector<Ref<Element>> 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&);
</span><span class="cx"> bool matches(Element&) const;
</span><ins>+ Element* closest(Element&) const;
</ins><span class="cx"> RefPtr<NodeList> queryAll(ContainerNode& rootNode) const;
</span><span class="cx"> Element* queryFirst(ContainerNode& 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&, Element&, const ContainerNode& rootNode) const;
</span><ins>+ Element* selectorClosest(const SelectorData&, Element&, const ContainerNode& rootNode) const;
</ins><span class="cx">
</span><span class="cx"> template <typename SelectorQueryTrait> void execute(ContainerNode& rootNode, typename SelectorQueryTrait::OutputType&) const;
</span><span class="cx"> template <typename SelectorQueryTrait> void executeFastPathForIdSelector(const ContainerNode& rootNode, const SelectorData&, const CSSSelector* idSelector, typename SelectorQueryTrait::OutputType&) const;
</span><span class="lines">@@ -116,6 +118,7 @@
</span><span class="cx"> public:
</span><span class="cx"> explicit SelectorQuery(CSSSelectorList&&);
</span><span class="cx"> bool matches(Element&) const;
</span><ins>+ Element* closest(Element&) const;
</ins><span class="cx"> RefPtr<NodeList> queryAll(ContainerNode& rootNode) const;
</span><span class="cx"> Element* queryFirst(ContainerNode& 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& element) const
+{
+ return m_selectors.closest(element);
+}
+
</ins><span class="cx"> inline RefPtr<NodeList> SelectorQuery::queryAll(ContainerNode& rootNode) const
</span><span class="cx"> {
</span><span class="cx"> return m_selectors.queryAll(rootNode);
</span></span></pre>
</div>
</div>
</body>
</html>