Add getElementById to DocumentFragment
authorweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 16 May 2015 18:16:26 +0000 (18:16 +0000)
committerweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 16 May 2015 18:16:26 +0000 (18:16 +0000)
https://bugs.webkit.org/show_bug.cgi?id=145094

Reviewed by Alexey Proskuryakov.

Source/WebCore:

Match the latest DOM standard and other browsers by adding getElementById
to DocumentFragment in addition to Document and SVGSVGElement. Add NonElementParentNode
interface that the DOM standard defines.

Test: fast/dom/DocumentFragment/getElementById.html

* DerivedSources.make:
* WebCore.xcodeproj/project.pbxproj:
Add NonElementParentNode.idl

* dom/Document.idl:
Mark Document as implementing NonElementParentNode and remove now redundant getElementById
declaration.

* dom/DocumentFragment.cpp:
(WebCore::DocumentFragment::getElementById):
* dom/DocumentFragment.h:
Add implementation of getElementById for DocumentFragments. Add a fast path for ShadowRoots
which can take advantage of the fact that they are TreeScopes to use the elements by id cache
in TreeScrope.

* dom/DocumentFragment.idl:
Mark DocumentFragment as implementing NonElementParentNode.

* dom/NonElementParentNode.idl: Copied from Source/WebCore/dom/NonDocumentTypeChildNode.idl.
Added.

* svg/SVGSVGElement.cpp:
(WebCore::SVGSVGElement::getElementById):
* svg/SVGSVGElement.h:
* svg/SVGSVGElement.idl:
Convert to taking an AtomicString to match other getElementByIds.

LayoutTests:

* fast/dom/DocumentFragment/getElementById-expected.txt: Added.
* fast/dom/DocumentFragment/getElementById.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@184435 268f45cc-cd09-0410-ab3c-d52691b4dbfc

15 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/dom/DocumentFragment/getElementById-expected.txt [new file with mode: 0644]
LayoutTests/fast/dom/DocumentFragment/getElementById.html [new file with mode: 0644]
Source/WebCore/CMakeLists.txt
Source/WebCore/ChangeLog
Source/WebCore/DerivedSources.make
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/dom/Document.idl
Source/WebCore/dom/DocumentFragment.cpp
Source/WebCore/dom/DocumentFragment.h
Source/WebCore/dom/DocumentFragment.idl
Source/WebCore/dom/NonElementParentNode.idl [new file with mode: 0644]
Source/WebCore/svg/SVGSVGElement.cpp
Source/WebCore/svg/SVGSVGElement.h
Source/WebCore/svg/SVGSVGElement.idl

index 4dab23b..b4453a9 100644 (file)
@@ -1,3 +1,13 @@
+2015-05-15  Sam Weinig  <sam@webkit.org>
+
+        Add getElementById to DocumentFragment
+        https://bugs.webkit.org/show_bug.cgi?id=145094
+
+        Reviewed by Alexey Proskuryakov.
+
+        * fast/dom/DocumentFragment/getElementById-expected.txt: Added.
+        * fast/dom/DocumentFragment/getElementById.html: Added.
+
 2015-05-15  Antti Koivisto  <antti@apple.com>
 
         When redirecting to data URL use HTTP response for same origin policy checks
diff --git a/LayoutTests/fast/dom/DocumentFragment/getElementById-expected.txt b/LayoutTests/fast/dom/DocumentFragment/getElementById-expected.txt
new file mode 100644 (file)
index 0000000..ff7b1a1
--- /dev/null
@@ -0,0 +1,15 @@
+Tests that getElementById() API is exposed on DocumentFragment nodes.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS fragment.getElementById('divID') is div
+PASS fragment.getElementById('aID') is a
+PASS fragment.getElementById('notInFragment') is null
+PASS fragment.getElementById('doesNotExist') is null
+PASS fragment.getElementById('duplicateId1') is span
+PASS fragment.getElementById('duplicateId2') is h1
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/dom/DocumentFragment/getElementById.html b/LayoutTests/fast/dom/DocumentFragment/getElementById.html
new file mode 100644 (file)
index 0000000..e526115
--- /dev/null
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8">
+<script src="../../../resources/js-test-pre.js"></script>
+</head>
+<body>
+<div id="notInFragment"></div>
+<div id="duplicateId1"></div>
+<script>
+
+description("Tests that getElementById() API is exposed on DocumentFragment nodes.");
+
+var fragment = new DocumentFragment();
+var div = document.createElement("div");
+div.id = "divID";
+fragment.appendChild(div);
+var a = document.createElement("a");
+a.id = "aID";
+div.appendChild(a);
+var span = document.createElement("span");
+span.id = "duplicateId1";
+div.appendChild(span);
+var h1 = document.createElement("h1");
+h1.id = "duplicateId2";
+div.appendChild(h1);
+var h2 = document.createElement("h2");
+h2.id = "duplicateId2";
+div.appendChild(h2);
+
+shouldBe("fragment.getElementById('divID')", "div");
+shouldBe("fragment.getElementById('aID')", "a");
+shouldBeNull("fragment.getElementById('notInFragment')");
+shouldBeNull("fragment.getElementById('doesNotExist')");
+
+// Duplicate ID cases.
+shouldBe("fragment.getElementById('duplicateId1')", "span"); // Should return the Element *inside* the DocumentFragment.
+shouldBe("fragment.getElementById('duplicateId2')", "h1"); // Should return the first matching Element in case of duplicate.
+
+</script>
+<script src="../../../resources/js-test-post.js"></script>
+</body>
+</html>
index 654512f..b120baf 100644 (file)
@@ -411,6 +411,7 @@ set(WebCore_NON_SVG_IDL_FILES
     dom/NodeFilter.idl
     dom/NodeIterator.idl
     dom/NodeList.idl
+    dom/NonElementParentNode.idl
     dom/NonDocumentTypeChildNode.idl
     dom/OverflowEvent.idl
     dom/PageTransitionEvent.idl
index 7826231..d8b05ef 100644 (file)
@@ -1,3 +1,43 @@
+2015-05-15  Sam Weinig  <sam@webkit.org>
+
+        Add getElementById to DocumentFragment
+        https://bugs.webkit.org/show_bug.cgi?id=145094
+
+        Reviewed by Alexey Proskuryakov.
+
+        Match the latest DOM standard and other browsers by adding getElementById
+        to DocumentFragment in addition to Document and SVGSVGElement. Add NonElementParentNode
+        interface that the DOM standard defines.
+
+        Test: fast/dom/DocumentFragment/getElementById.html
+
+        * DerivedSources.make:
+        * WebCore.xcodeproj/project.pbxproj:
+        Add NonElementParentNode.idl
+
+        * dom/Document.idl:
+        Mark Document as implementing NonElementParentNode and remove now redundant getElementById
+        declaration.
+
+        * dom/DocumentFragment.cpp:
+        (WebCore::DocumentFragment::getElementById):
+        * dom/DocumentFragment.h:
+        Add implementation of getElementById for DocumentFragments. Add a fast path for ShadowRoots
+        which can take advantage of the fact that they are TreeScopes to use the elements by id cache
+        in TreeScrope.
+
+        * dom/DocumentFragment.idl:
+        Mark DocumentFragment as implementing NonElementParentNode.
+
+        * dom/NonElementParentNode.idl: Copied from Source/WebCore/dom/NonDocumentTypeChildNode.idl.
+        Added.
+
+        * svg/SVGSVGElement.cpp:
+        (WebCore::SVGSVGElement::getElementById):
+        * svg/SVGSVGElement.h:
+        * svg/SVGSVGElement.idl:
+        Convert to taking an AtomicString to match other getElementByIds.
+
 2015-05-15  Antti Koivisto  <antti@apple.com>
 
         When redirecting to data URL use HTTP response for same origin policy checks
index 0f4ba2d..1a32355 100644 (file)
@@ -299,6 +299,7 @@ NON_SVG_BINDING_IDLS = \
     $(WebCore)/dom/NodeFilter.idl \
     $(WebCore)/dom/NodeIterator.idl \
     $(WebCore)/dom/NodeList.idl \
+    $(WebCore)/dom/NonElementParentNode.idl \
     $(WebCore)/dom/NonDocumentTypeChildNode.idl \
     $(WebCore)/dom/OverflowEvent.idl \
     $(WebCore)/dom/PageTransitionEvent.idl \
index f498bb1..b085a07 100644 (file)
                7C6136F71710C35200FF4A57 /* InFilesCompiler.pm */ = {isa = PBXFileReference; lastKnownFileType = text.script.perl; name = InFilesCompiler.pm; path = scripts/InFilesCompiler.pm; sourceTree = "<group>"; };
                7C6136F81710C35200FF4A57 /* InFilesParser.pm */ = {isa = PBXFileReference; lastKnownFileType = text.script.perl; name = InFilesParser.pm; path = scripts/InFilesParser.pm; sourceTree = "<group>"; };
                7C6136F91710C35200FF4A57 /* StaticString.pm */ = {isa = PBXFileReference; lastKnownFileType = text.script.perl; name = StaticString.pm; path = scripts/StaticString.pm; sourceTree = "<group>"; };
+               7C6752BA1B06E82000C279CB /* NonElementParentNode.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = NonElementParentNode.idl; sourceTree = "<group>"; };
                7C73FB05191EF416007DE061 /* UserMessageHandlersNamespace.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UserMessageHandlersNamespace.cpp; sourceTree = "<group>"; };
                7C73FB06191EF417007DE061 /* UserMessageHandlersNamespace.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UserMessageHandlersNamespace.h; sourceTree = "<group>"; };
                7C73FB09191EF49F007DE061 /* UserMessageHandlersNamespace.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = UserMessageHandlersNamespace.idl; sourceTree = "<group>"; };
                                E43105BA16750F1600DB2FB8 /* NodeTraversal.h */,
                                9382AAB10D8C386100F357A6 /* NodeWithIndex.h */,
                                8369E58F1AFDD0300087DF68 /* NonDocumentTypeChildNode.idl */,
+                               7C6752BA1B06E82000C279CB /* NonElementParentNode.idl */,
                                1A0D57340A5C77FE007EDD4C /* OverflowEvent.cpp */,
                                1A0D57350A5C77FE007EDD4C /* OverflowEvent.h */,
                                1A0D57380A5C7812007EDD4C /* OverflowEvent.idl */,
index a5ad310..034b587 100644 (file)
@@ -52,7 +52,6 @@
                                                                           [TreatNullAs=NullString,Default=Undefined] optional DOMString qualifiedName);
     [ObjCLegacyUnnamedParameters] NodeList getElementsByTagNameNS([TreatNullAs=NullString,Default=Undefined] optional DOMString namespaceURI,
                                                    [Default=Undefined] optional DOMString localName);
-    Element getElementById([Default=Undefined,ObjCExplicitAtomicString,RequiresExistingAtomicString] optional DOMString elementId);
 
     // DOM Level 3 Core
 
 };
 
 Document implements ParentNode;
+Document implements NonElementParentNode;
index 2f71bfc..4fab58e 100644 (file)
@@ -24,6 +24,7 @@
 #include "DocumentFragment.h"
 
 #include "Document.h"
+#include "ElementDescendantIterator.h"
 #include "HTMLDocumentParser.h"
 #include "Page.h"
 #include "Settings.h"
@@ -91,4 +92,19 @@ bool DocumentFragment::parseXML(const String& source, Element* contextElement, P
     return XMLDocumentParser::parseDocumentFragment(source, *this, contextElement, parserContentPolicy);
 }
 
+Element* DocumentFragment::getElementById(const AtomicString& id) const
+{
+    // Fast path for ShadowRoot, where we are both a DocumentFragment and a TreeScope.
+    if (isTreeScope())
+        return treeScope().getElementById(id);
+
+    // Otherwise, fall back to iterating all of the element descendants.
+    for (auto& element : elementDescendants(*this)) {
+        if (element.getIdAttribute() == id)
+            return const_cast<Element*>(&element);
+    }
+
+    return nullptr;
+}
+
 }
index a7c2791..d07e034 100644 (file)
@@ -39,6 +39,9 @@ public:
     virtual bool canContainRangeEndPoint() const override final { return true; }
     virtual bool isTemplateContent() const { return false; }
 
+    // From the NonElementParentNode interface - https://dom.spec.whatwg.org/#interface-nonelementparentnode
+    Element* getElementById(const AtomicString&) const;
+
 protected:
     DocumentFragment(Document&, ConstructionType = CreateContainer);
     virtual String nodeName() const override final;
index 2c94fbd..78552e9 100644 (file)
  * Boston, MA 02110-1301, USA.
  */
 
- [
+[
     Constructor,
     ConstructorCallWith=Document
- ] interface DocumentFragment : Node {
+] interface DocumentFragment : Node {
 };
 
 DocumentFragment implements ParentNode;
+DocumentFragment implements NonElementParentNode;
diff --git a/Source/WebCore/dom/NonElementParentNode.idl b/Source/WebCore/dom/NonElementParentNode.idl
new file mode 100644 (file)
index 0000000..0623a45
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// https://dom.spec.whatwg.org/#interface-nonelementparentnode
+[
+    NoInterfaceObject,
+] interface NonElementParentNode {
+    Element getElementById([Default=Undefined,ObjCExplicitAtomicString,RequiresExistingAtomicString] optional DOMString elementId);
+};
index c6516c4..954239f 100644 (file)
@@ -670,7 +670,7 @@ void SVGSVGElement::documentDidResumeFromPageCache()
 
 // getElementById on SVGSVGElement is restricted to only the child subtree defined by the <svg> element.
 // See http://www.w3.org/TR/SVG11/struct.html#InterfaceSVGSVGElement
-Element* SVGSVGElement::getElementById(const String& id)
+Element* SVGSVGElement::getElementById(const AtomicString& id)
 {
     Element* element = treeScope().getElementById(id);
     if (element && element->isDescendantOf(this))
index d72b534..f325dbc 100644 (file)
@@ -97,7 +97,7 @@ public: // DOM
     static SVGTransform createSVGTransform();
     static SVGTransform createSVGTransformFromMatrix(const SVGMatrix&);
 
-    Element* getElementById(const String&);
+    Element* getElementById(const AtomicString&);
 
     SVGZoomAndPanType zoomAndPan() const;
     void setZoomAndPan(unsigned short);
index 8fc710d..49715f8 100644 (file)
@@ -67,7 +67,8 @@ interface SVGSVGElement : SVGGraphicsElement {
     SVGRect createSVGRect();
     SVGTransform createSVGTransform();
     SVGTransform createSVGTransformFromMatrix([Default=Undefined] optional SVGMatrix matrix);
-    Element getElementById([Default=Undefined] optional DOMString elementId);
+
+    Element getElementById([Default=Undefined, RequiresExistingAtomicString] optional DOMString elementId);
 };
 
 SVGSVGElement implements SVGExternalResourcesRequired;