ComposedTreeIterator traverses normal children for elements with empty shadow root
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 19 Feb 2016 21:49:34 +0000 (21:49 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 19 Feb 2016 21:49:34 +0000 (21:49 +0000)
https://bugs.webkit.org/show_bug.cgi?id=154464

Reviewed by Ryosuke Niwa.

Source/WebCore:

Test: fast/shadow-dom/composed-tree-basic.html

* dom/ComposedTreeIterator.cpp:
(WebCore::ComposedTreeIterator::initializeContextStack):
(WebCore::ComposedTreeIterator::traverseShadowRoot):

    If the shadow root is empty continue by skipping the real children.

(WebCore::ComposedTreeIterator::traverseNextInShadowTree):
(WebCore::composedTreeAsText):
(WebCore::ComposedTreeIterator::pushContext): Deleted.
* dom/ComposedTreeIterator.h:
(WebCore::ComposedTreeIterator::context):
(WebCore::ComposedTreeIterator::current):
(WebCore::ComposedTreeIterator::traverseNext):
(WebCore::composedTreeChildren):
* testing/Internals.cpp:
(WebCore::Internals::composedTreeAsText):

    Testing support.

* testing/Internals.h:
* testing/Internals.idl:

LayoutTests:

* fast/shadow-dom/composed-tree-basic-expected.txt: Added.
* fast/shadow-dom/composed-tree-basic.html: Added.

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

LayoutTests/ChangeLog
LayoutTests/fast/shadow-dom/composed-tree-basic-expected.txt [new file with mode: 0644]
LayoutTests/fast/shadow-dom/composed-tree-basic.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/dom/ComposedTreeIterator.cpp
Source/WebCore/dom/ComposedTreeIterator.h
Source/WebCore/testing/Internals.cpp
Source/WebCore/testing/Internals.h
Source/WebCore/testing/Internals.idl

index 567c55c..ce5f34f 100644 (file)
@@ -1,3 +1,13 @@
+2016-02-19  Antti Koivisto  <antti@apple.com>
+
+        ComposedTreeIterator traverses normal children for elements with empty shadow root
+        https://bugs.webkit.org/show_bug.cgi?id=154464
+
+        Reviewed by Ryosuke Niwa.
+
+        * fast/shadow-dom/composed-tree-basic-expected.txt: Added.
+        * fast/shadow-dom/composed-tree-basic.html: Added.
+
 2016-02-19  Nan Wang  <n_wang@apple.com>
 
         AX: Inconsistency between CharacterOffset and VisiblePostition
diff --git a/LayoutTests/fast/shadow-dom/composed-tree-basic-expected.txt b/LayoutTests/fast/shadow-dom/composed-tree-basic-expected.txt
new file mode 100644 (file)
index 0000000..48bd15d
--- /dev/null
@@ -0,0 +1,71 @@
+
+Test 1
+
+Test 2
+  #text
+
+Test 3
+  div
+
+Test 4
+  div
+    #text
+    div
+      #text
+
+Test 5
+  div (shadow root)
+  #text
+
+Test 6
+  div (shadow root)
+  #text
+
+Test 7
+  div (shadow root)
+    #text
+    div
+      div
+    #text
+  div
+    #text
+
+Test 8
+  div (shadow root)
+    #text
+    div
+      div
+    #text
+  div
+    #text
+
+Test 9
+  div (shadow root)
+    div
+      #text
+      div (shadow root)
+        #text
+        div
+          div
+        #text
+    div (shadow root)
+      #text
+      div
+        div
+      #text
+
+Test 10
+  div (shadow root)
+    div
+      #text
+      div (shadow root)
+        #text
+        div
+          div
+        #text
+    div (shadow root)
+      #text
+      div
+        div
+      #text
+
diff --git a/LayoutTests/fast/shadow-dom/composed-tree-basic.html b/LayoutTests/fast/shadow-dom/composed-tree-basic.html
new file mode 100644 (file)
index 0000000..3a49ab5
--- /dev/null
@@ -0,0 +1,58 @@
+<script>
+if (window.testRunner)
+    testRunner.dumpAsText();
+</script>
+
+<template id=shadow1></template>
+<template id=shadow2>text<div><div></div></div>text</template>
+<template id=shadow3><div>text<div shadow=shadow2></div></div><div shadow=shadow2></div></template>
+
+<template test=1></template>
+
+<template test=2>text</template>
+
+<template test=3><div></div></template>
+
+<template test=4><div>text<div>text</div></div></template>
+
+<template test=5><div shadow=shadow1></div>text</template>
+
+<template test=6><div shadow=shadow1>text<div></div></div>text</template>
+
+<template test=7><div shadow=shadow2></div><div>text</div></template>
+
+<template test=8><div shadow=shadow2>text<div></div></div><div>text</div></template>
+
+<template test=9><div shadow=shadow3></div></template>
+
+<template test=10><div shadow=shadow3>text<div></div></div></template>
+
+<body>
+<pre id=console></pre>
+<script>
+function installShadows(tree)
+{
+    var shadowHosts = tree.querySelectorAll("[shadow]");
+    for (var i = 0; i < shadowHosts.length; ++i) {
+        var shadowId = shadowHosts[i].getAttribute("shadow");
+        var shadowContents = document.querySelector("#"+shadowId).content.cloneNode(true);
+
+        installShadows(shadowContents);
+
+        var shadowRoot = shadowHosts[i].attachShadow({ mode: "open" });
+        shadowRoot.appendChild(shadowContents);
+    }
+}
+
+var console = document.querySelector("#console");
+
+var tests = document.querySelectorAll("[test]");
+for (var i = 0; i < tests.length; ++i) {
+    var test = tests[i].content.cloneNode(true);
+    installShadows(test);
+    console.innerText += "\nTest " + tests[i].getAttribute("test") + "\n";
+    console.innerText += internals.composedTreeAsText(test);
+}
+
+</script>
+</body>
index f4bbb97..e4eab52 100644 (file)
@@ -1,3 +1,34 @@
+2016-02-19  Antti Koivisto  <antti@apple.com>
+
+        ComposedTreeIterator traverses normal children for elements with empty shadow root
+        https://bugs.webkit.org/show_bug.cgi?id=154464
+
+        Reviewed by Ryosuke Niwa.
+
+        Test: fast/shadow-dom/composed-tree-basic.html
+
+        * dom/ComposedTreeIterator.cpp:
+        (WebCore::ComposedTreeIterator::initializeContextStack):
+        (WebCore::ComposedTreeIterator::traverseShadowRoot):
+
+            If the shadow root is empty continue by skipping the real children.
+
+        (WebCore::ComposedTreeIterator::traverseNextInShadowTree):
+        (WebCore::composedTreeAsText):
+        (WebCore::ComposedTreeIterator::pushContext): Deleted.
+        * dom/ComposedTreeIterator.h:
+        (WebCore::ComposedTreeIterator::context):
+        (WebCore::ComposedTreeIterator::current):
+        (WebCore::ComposedTreeIterator::traverseNext):
+        (WebCore::composedTreeChildren):
+        * testing/Internals.cpp:
+        (WebCore::Internals::composedTreeAsText):
+
+            Testing support.
+
+        * testing/Internals.h:
+        * testing/Internals.idl:
+
 2016-02-19  Jer Noble  <jer.noble@apple.com>
 
         Adopt CachedRawResourceClient::shouldCacheResponse() in MediaResourceLoader and WebCoreNSURLSession
index 6201427..e7721d6 100644 (file)
@@ -27,6 +27,7 @@
 #include "ComposedTreeIterator.h"
 
 #include "HTMLSlotElement.h"
+#include "TextStream.h"
 
 namespace WebCore {
 
@@ -105,13 +106,16 @@ void ComposedTreeIterator::initializeContextStack(ContainerNode& root, Node& cur
     m_contextStack.reverse();
 }
 
-bool ComposedTreeIterator::pushContext(ShadowRoot& shadowRoot)
+void ComposedTreeIterator::traverseShadowRoot(ShadowRoot& shadowRoot)
 {
     Context shadowContext(shadowRoot);
-    if (!shadowContext.iterator)
-        return false;
+    if (!shadowContext.iterator) {
+        // Empty shadow root.
+        traverseNextSkippingChildren();
+        return;
+    }
+
     m_contextStack.append(WTFMove(shadowContext));
-    return true;
 }
 
 void ComposedTreeIterator::traverseNextInShadowTree()
@@ -180,4 +184,24 @@ void ComposedTreeIterator::traverseSiblingInSlot(int direction)
 }
 #endif
 
+String composedTreeAsText(ContainerNode& root)
+{
+    TextStream stream;
+    auto descendants = composedTreeDescendants(root);
+    for (auto it = descendants.begin(), end = descendants.end(); it != end; ++it) {
+        writeIndent(stream, it.depth());
+
+        if (is<Text>(*it)) {
+            stream << "#text\n";
+            continue;
+        }
+        auto& element = downcast<Element>(*it);
+        stream << element.localName();
+        if (element.shadowRoot())
+            stream << " (shadow root)";
+        stream << "\n";
+    }
+    return stream.release();
+}
+
 }
index ebf4555..06a9b33 100644 (file)
@@ -58,7 +58,7 @@ private:
     void initializeContextStack(ContainerNode& root, Node& current);
     void traverseNextInShadowTree();
     void traverseNextLeavingContext();
-    bool pushContext(ShadowRoot&);
+    void traverseShadowRoot(ShadowRoot&);
 #if ENABLE(SHADOW_DOM) || ENABLE(DETAILS_ELEMENT)
     bool advanceInSlot(int direction);
     void traverseSiblingInSlot(int direction);
@@ -92,8 +92,8 @@ inline ComposedTreeIterator::ComposedTreeIterator()
 inline ComposedTreeIterator& ComposedTreeIterator::traverseNext()
 {
     if (auto* shadowRoot = context().iterator->shadowRoot()) {
-        if (pushContext(*shadowRoot))
-            return *this;
+        traverseShadowRoot(*shadowRoot);
+        return *this;
     }
 
     if (m_contextStack.size() > 1) {
@@ -200,6 +200,8 @@ inline ComposedTreeChildAdapter composedTreeChildren(ContainerNode& parent)
     return ComposedTreeChildAdapter(parent);
 }
 
+WEBCORE_EXPORT String composedTreeAsText(ContainerNode& root);
+
 }
 
 #endif
index 4eff2c8..140f2e9 100644 (file)
@@ -39,6 +39,7 @@
 #include "ChromeClient.h"
 #include "ClientRect.h"
 #include "ClientRectList.h"
+#include "ComposedTreeIterator.h"
 #include "Cursor.h"
 #include "DOMPath.h"
 #include "DOMStringList.h"
@@ -3487,4 +3488,11 @@ void Internals::setResourceLoadStatisticsEnabled(bool enable)
     Settings::setResourceLoadStatisticsEnabled(enable);
 }
 
+String Internals::composedTreeAsText(Node* node)
+{
+    if (!is<ContainerNode>(node))
+        return "";
+    return WebCore::composedTreeAsText(downcast<ContainerNode>(*node));
+}
+
 }
index d3a4297..67a0193 100644 (file)
@@ -471,6 +471,8 @@ public:
     bool isReadableStreamDisturbed(ScriptState&, JSC::JSValue);
 #endif
 
+    String composedTreeAsText(Node*);
+
 private:
     explicit Internals(Document*);
     Document* contextDocument() const;
index f8535fc..8043d67 100644 (file)
@@ -439,4 +439,6 @@ enum AutoFillButtonType {
     void setResourceLoadStatisticsEnabled(boolean enable);
 
     [RaisesException] void setCanShowModalDialogOverride(boolean allow);
+
+    DOMString composedTreeAsText(Node parent);
 };