Title: [174324] trunk
Revision
174324
Author
[email protected]
Date
2014-10-04 15:24:21 -0700 (Sat, 04 Oct 2014)

Log Message

Implement Element.closest() API
https://bugs.webkit.org/show_bug.cgi?id=137418

Patch by Dhi Aurrahman <[email protected]> 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.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (174323 => 174324)


--- trunk/LayoutTests/ChangeLog	2014-10-04 22:11:57 UTC (rev 174323)
+++ trunk/LayoutTests/ChangeLog	2014-10-04 22:24:21 UTC (rev 174324)
@@ -1,3 +1,17 @@
+2014-10-04  Dhi Aurrahman  <[email protected]>
+
+        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.
+
 2014-10-04  Filip Pizlo  <[email protected]>
 
         FTL should sink PutLocals

Added: trunk/LayoutTests/fast/selectors/closest-general-expected.txt (0 => 174324)


--- 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)
@@ -0,0 +1,61 @@
+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
+

Added: trunk/LayoutTests/fast/selectors/closest-general.html (0 => 174324)


--- 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)
@@ -0,0 +1,122 @@
+<!doctype html>
+<html>
+<head>
+<script src=""
+<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=""
+</html>

Added: trunk/LayoutTests/fast/selectors/closest-scope-expected.txt (0 => 174324)


--- 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)
@@ -0,0 +1,28 @@
+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
+

Added: trunk/LayoutTests/fast/selectors/closest-scope.html (0 => 174324)


--- 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)
@@ -0,0 +1,47 @@
+<!doctype html>
+<html>
+<head>
+<script src=""
+<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=""
+</html>

Modified: trunk/Source/WebCore/ChangeLog (174323 => 174324)


--- 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)
@@ -1,3 +1,27 @@
+2014-10-04  Dhi Aurrahman  <[email protected]>
+
+        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):
+
 2014-10-04  Brian J. Burg  <[email protected]>
 
         Unreviewed, rolling out r174319.

Modified: trunk/Source/WebCore/dom/Element.cpp (174323 => 174324)


--- 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)
@@ -2321,6 +2321,14 @@
     return selectorQuery && selectorQuery->matches(*this);
 }
 
+Element* Element::closest(const String& selector, ExceptionCode& ec)
+{
+    SelectorQuery* selectorQuery = document().selectorQueryForString(selector, ec);
+    if (selectorQuery)
+        return selectorQuery->closest(*this);
+    return nullptr;
+}
+
 bool Element::shouldAppearIndeterminate() const
 {
     return false;

Modified: trunk/Source/WebCore/dom/Element.h (174323 => 174324)


--- 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)
@@ -452,6 +452,7 @@
 
     virtual bool matchesReadWritePseudoClass() const;
     bool matches(const String& selectors, ExceptionCode&);
+    Element* closest(const String& selectors, ExceptionCode&);
     virtual bool shouldAppearIndeterminate() const;
 
     DOMTokenList& classList();

Modified: trunk/Source/WebCore/dom/Element.idl (174323 => 174324)


--- 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)
@@ -121,6 +121,7 @@
     [RaisesException] NodeList querySelectorAll(DOMString selectors);
 
     [RaisesException] boolean matches([Default=Undefined] optional DOMString selectors);
+    [RaisesException] Element closest(DOMString selectors);    
     [ImplementedAs=matches, RaisesException] boolean webkitMatchesSelector([Default=Undefined] optional DOMString selectors);
 
     // ElementTraversal API

Modified: trunk/Source/WebCore/dom/SelectorQuery.cpp (174323 => 174324)


--- 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)
@@ -120,6 +120,17 @@
     return selectorChecker.match(selectorData.selector, &element, selectorCheckingContext);
 }
 
+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;
+}
+
 bool SelectorDataList::matches(Element& targetElement) const
 {
     unsigned selectorCount = m_selectors.size();
@@ -130,6 +141,20 @@
     return false;
 }
 
+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;
+}
+
 struct AllElementExtractorSelectorQueryTrait {
     typedef Vector<Ref<Element>> OutputType;
     static const bool shouldOnlyMatchFirstElement = false;

Modified: trunk/Source/WebCore/dom/SelectorQuery.h (174323 => 174324)


--- 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)
@@ -49,6 +49,7 @@
 public:
     explicit SelectorDataList(const CSSSelectorList&);
     bool matches(Element&) const;
+    Element* closest(Element&) const;
     RefPtr<NodeList> queryAll(ContainerNode& rootNode) const;
     Element* queryFirst(ContainerNode& rootNode) const;
 
@@ -79,6 +80,7 @@
     };
 
     bool selectorMatches(const SelectorData&, Element&, const ContainerNode& rootNode) const;
+    Element* selectorClosest(const SelectorData&, Element&, const ContainerNode& rootNode) const;
 
     template <typename SelectorQueryTrait> void execute(ContainerNode& rootNode, typename SelectorQueryTrait::OutputType&) const;
     template <typename SelectorQueryTrait> void executeFastPathForIdSelector(const ContainerNode& rootNode, const SelectorData&, const CSSSelector* idSelector, typename SelectorQueryTrait::OutputType&) const;
@@ -116,6 +118,7 @@
 public:
     explicit SelectorQuery(CSSSelectorList&&);
     bool matches(Element&) const;
+    Element* closest(Element&) const;
     RefPtr<NodeList> queryAll(ContainerNode& rootNode) const;
     Element* queryFirst(ContainerNode& rootNode) const;
 
@@ -139,6 +142,11 @@
     return m_selectors.matches(element);
 }
 
+inline Element* SelectorQuery::closest(Element& element) const
+{
+    return m_selectors.closest(element);
+}
+
 inline RefPtr<NodeList> SelectorQuery::queryAll(ContainerNode& rootNode) const
 {
     return m_selectors.queryAll(rootNode);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to