Title: [259651] trunk
Revision
259651
Author
[email protected]
Date
2020-04-07 11:40:07 -0700 (Tue, 07 Apr 2020)

Log Message

documentFragment.getElementById() should not work for empty-string IDs
https://bugs.webkit.org/show_bug.cgi?id=210111

Reviewed by Geoffrey Garen.

LayoutTests/imported/w3c:

Import test coverage from upstream WPT.

* web-platform-tests/dom/nodes/DocumentFragment-getElementById-expected.txt: Added.
* web-platform-tests/dom/nodes/DocumentFragment-getElementById.html: Added.

Source/WebCore:

Make sure that getElementById() returns null when given an empty string ID:
- https://dom.spec.whatwg.org/#concept-id

Test: imported/w3c/web-platform-tests/dom/nodes/DocumentFragment-getElementById.html

* dom/DocumentFragment.cpp:
(WebCore::DocumentFragment::getElementById const):
* dom/TreeScope.cpp:
(WebCore::TreeScope::getElementById const):

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/imported/w3c/ChangeLog (259650 => 259651)


--- trunk/LayoutTests/imported/w3c/ChangeLog	2020-04-07 18:37:44 UTC (rev 259650)
+++ trunk/LayoutTests/imported/w3c/ChangeLog	2020-04-07 18:40:07 UTC (rev 259651)
@@ -1,3 +1,15 @@
+2020-04-07  Chris Dumez  <[email protected]>
+
+        documentFragment.getElementById() should not work for empty-string IDs
+        https://bugs.webkit.org/show_bug.cgi?id=210111
+
+        Reviewed by Geoffrey Garen.
+
+        Import test coverage from upstream WPT.
+
+        * web-platform-tests/dom/nodes/DocumentFragment-getElementById-expected.txt: Added.
+        * web-platform-tests/dom/nodes/DocumentFragment-getElementById.html: Added.
+
 2020-04-06  Antoine Quint  <[email protected]>
 
         [Web Animations] Move Document.getAnimations() to DocumentOrShadowRoot

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/dom/nodes/DocumentFragment-getElementById-expected.txt (0 => 259651)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/dom/nodes/DocumentFragment-getElementById-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/dom/nodes/DocumentFragment-getElementById-expected.txt	2020-04-07 18:40:07 UTC (rev 259651)
@@ -0,0 +1,7 @@
+
+PASS The method must exist 
+PASS It must return null when there are no matches 
+PASS It must return the first element when there are matches 
+PASS Empty string ID values 
+PASS It must return the first element when there are matches, using a template 
+

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/dom/nodes/DocumentFragment-getElementById.html (0 => 259651)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/dom/nodes/DocumentFragment-getElementById.html	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/dom/nodes/DocumentFragment-getElementById.html	2020-04-07 18:40:07 UTC (rev 259651)
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>DocumentFragment.prototype.getElementById</title>
+<link rel="help" href=""
+<link rel="author" title="Domenic Denicola" href=""
+
+<script src=""
+<script src=""
+
+<template>
+  <div id="bar">
+    <span id="foo" data-yes></span>
+  </div>
+  <div id="foo">
+    <span id="foo"></span>
+    <ul id="bar">
+      <li id="foo"></li>
+    </ul>
+  </div>
+</template>
+
+<script>
+"use strict";
+
+test(() => {
+  assert_equals(typeof DocumentFragment.prototype.getElementById, "function", "It must exist on the prototype");
+  assert_equals(typeof document.createDocumentFragment().getElementById, "function", "It must exist on an instance");
+}, "The method must exist");
+
+test(() => {
+  assert_equals(document.createDocumentFragment().getElementById("foo"), null);
+  assert_equals(document.createDocumentFragment().getElementById(""), null);
+}, "It must return null when there are no matches");
+
+test(() => {
+  const frag = document.createDocumentFragment();
+  frag.appendChild(document.createElement("div"));
+  frag.appendChild(document.createElement("span"));
+  frag.childNodes[0].id = "foo";
+  frag.childNodes[1].id = "foo";
+
+  assert_equals(frag.getElementById("foo"), frag.childNodes[0]);
+}, "It must return the first element when there are matches");
+
+test(() => {
+  const frag = document.createDocumentFragment();
+  frag.appendChild(document.createElement("div"));
+  frag.childNodes[0].setAttribute("id", "");
+
+  assert_equals(
+    frag.getElementById(""),
+    null,
+    "Even if there is an element with an empty-string ID attribute, it must not be returned"
+  );
+}, "Empty string ID values");
+
+test(() => {
+  const frag = document.querySelector("template").content;
+
+  assert_true(frag.getElementById("foo").hasAttribute("data-yes"));
+}, "It must return the first element when there are matches, using a template");
+</script>

Modified: trunk/Source/WebCore/ChangeLog (259650 => 259651)


--- trunk/Source/WebCore/ChangeLog	2020-04-07 18:37:44 UTC (rev 259650)
+++ trunk/Source/WebCore/ChangeLog	2020-04-07 18:40:07 UTC (rev 259651)
@@ -1,3 +1,20 @@
+2020-04-07  Chris Dumez  <[email protected]>
+
+        documentFragment.getElementById() should not work for empty-string IDs
+        https://bugs.webkit.org/show_bug.cgi?id=210111
+
+        Reviewed by Geoffrey Garen.
+
+        Make sure that getElementById() returns null when given an empty string ID:
+        - https://dom.spec.whatwg.org/#concept-id
+
+        Test: imported/w3c/web-platform-tests/dom/nodes/DocumentFragment-getElementById.html
+
+        * dom/DocumentFragment.cpp:
+        (WebCore::DocumentFragment::getElementById const):
+        * dom/TreeScope.cpp:
+        (WebCore::TreeScope::getElementById const):
+
 2020-04-07  Timothy Hatcher  <[email protected]>
 
         WKUserScripts deferred from injection are not injected if -[WKWebView _notifyUserScripts] is called early.

Modified: trunk/Source/WebCore/dom/DocumentFragment.cpp (259650 => 259651)


--- trunk/Source/WebCore/dom/DocumentFragment.cpp	2020-04-07 18:37:44 UTC (rev 259650)
+++ trunk/Source/WebCore/dom/DocumentFragment.cpp	2020-04-07 18:40:07 UTC (rev 259651)
@@ -95,7 +95,7 @@
 
 Element* DocumentFragment::getElementById(const AtomString& id) const
 {
-    if (id.isNull())
+    if (id.isEmpty())
         return nullptr;
 
     // Fast path for ShadowRoot, where we are both a DocumentFragment and a TreeScope.

Modified: trunk/Source/WebCore/dom/TreeScope.cpp (259650 => 259651)


--- trunk/Source/WebCore/dom/TreeScope.cpp	2020-04-07 18:37:44 UTC (rev 259650)
+++ trunk/Source/WebCore/dom/TreeScope.cpp	2020-04-07 18:40:07 UTC (rev 259651)
@@ -100,7 +100,7 @@
 
 Element* TreeScope::getElementById(const AtomString& elementId) const
 {
-    if (elementId.isNull())
+    if (elementId.isEmpty())
         return nullptr;
     if (!m_elementsById)
         return nullptr;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to