Replace WTF::move with WTFMove
[WebKit-https.git] / Source / WebCore / html / HTMLSummaryElement.cpp
index de8598e..13d3cf4 100644 (file)
 #if ENABLE(DETAILS_ELEMENT)
 #include "DetailsMarkerControl.h"
 #include "HTMLDetailsElement.h"
-#include "InsertionPoint.h"
+#include "HTMLFormControlElement.h"
+#include "HTMLSlotElement.h"
 #include "KeyboardEvent.h"
 #include "MouseEvent.h"
-#include "NodeRenderingTraversal.h"
 #include "PlatformMouseEvent.h"
 #include "RenderBlockFlow.h"
+#include "ShadowRoot.h"
 
 namespace WebCore {
 
 using namespace HTMLNames;
 
-class SummaryContentElement : public InsertionPoint {
-public:
-    static PassRefPtr<SummaryContentElement> create(Document&);
-
-private:
-    SummaryContentElement(Document& document)
-        : InsertionPoint(webkitShadowContentTag, document)
-    {
-    }
-};
-
-PassRefPtr<SummaryContentElement> SummaryContentElement::create(Document& document)
-{
-    return adoptRef(new SummaryContentElement(document));
-}
-
-PassRefPtr<HTMLSummaryElement> HTMLSummaryElement::create(const QualifiedName& tagName, Document& document)
+Ref<HTMLSummaryElement> HTMLSummaryElement::create(const QualifiedName& tagName, Document& document)
 {
-    RefPtr<HTMLSummaryElement> summary = adoptRef(new HTMLSummaryElement(tagName, document));
-    summary->ensureUserAgentShadowRoot();
-    return summary.release();
+    Ref<HTMLSummaryElement> summary = adoptRef(*new HTMLSummaryElement(tagName, document));
+    summary->addShadowRoot(ShadowRoot::create(document, ShadowRoot::Type::UserAgent));
+    return summary;
 }
 
 HTMLSummaryElement::HTMLSummaryElement(const QualifiedName& tagName, Document& document)
@@ -64,60 +49,57 @@ HTMLSummaryElement::HTMLSummaryElement(const QualifiedName& tagName, Document& d
     ASSERT(hasTagName(summaryTag));
 }
 
-RenderElement* HTMLSummaryElement::createRenderer(PassRef<RenderStyle> style)
+RenderPtr<RenderElement> HTMLSummaryElement::createElementRenderer(Ref<RenderStyle>&& style, const RenderTreePosition&)
 {
-    return new RenderBlockFlow(*this, std::move(style));
-}
-
-bool HTMLSummaryElement::childShouldCreateRenderer(const Node& child) const
-{
-    if (child.isPseudoElement())
-        return HTMLElement::childShouldCreateRenderer(child);
-
-    return hasShadowRootOrActiveInsertionPointParent(child) && HTMLElement::childShouldCreateRenderer(child);
+    return createRenderer<RenderBlockFlow>(*this, WTFMove(style));
 }
 
 void HTMLSummaryElement::didAddUserAgentShadowRoot(ShadowRoot* root)
 {
-    root->appendChild(DetailsMarkerControl::create(document()), ASSERT_NO_EXCEPTION, AttachLazily);
-    root->appendChild(SummaryContentElement::create(document()), ASSERT_NO_EXCEPTION, AttachLazily);
+    root->appendChild(DetailsMarkerControl::create(document()), ASSERT_NO_EXCEPTION);
+    root->appendChild(HTMLSlotElement::create(slotTag, document()));
 }
 
 HTMLDetailsElement* HTMLSummaryElement::detailsElement() const
 {
-    Node* mayDetails = NodeRenderingTraversal::parent(this);
-    if (!mayDetails || !mayDetails->hasTagName(detailsTag))
-        return 0;
-    return static_cast<HTMLDetailsElement*>(mayDetails);
+    auto* parent = parentElement();
+    if (parent && is<HTMLDetailsElement>(*parent))
+        return downcast<HTMLDetailsElement>(parent);
+    // Fallback summary element is in the shadow tree.
+    auto* host = shadowHost();
+    if (host && is<HTMLDetailsElement>(*host))
+        return downcast<HTMLDetailsElement>(host);
+    return nullptr;
 }
 
-bool HTMLSummaryElement::isMainSummary() const
+bool HTMLSummaryElement::isActiveSummary() const
 {
-    if (HTMLDetailsElement* details = detailsElement())
-        return details->findMainSummary() == this;
-
-    return false;
+    HTMLDetailsElement* details = detailsElement();
+    if (!details)
+        return false;
+    return details->isActiveSummary(*this);
 }
 
 static bool isClickableControl(Node* node)
 {
-    if (!node->isElementNode())
+    ASSERT(node);
+    if (!is<Element>(*node))
         return false;
-    Element* element = toElement(node);
-    if (element->isFormControlElement())
+    Element& element = downcast<Element>(*node);
+    if (is<HTMLFormControlElement>(element))
         return true;
-    Element* host = element->shadowHost();
-    return host && host->isFormControlElement();
+    Element* host = element.shadowHost();
+    return host && is<HTMLFormControlElement>(host);
 }
 
 bool HTMLSummaryElement::supportsFocus() const
 {
-    return isMainSummary();
+    return isActiveSummary();
 }
 
 void HTMLSummaryElement::defaultEventHandler(Event* event)
 {
-    if (isMainSummary() && renderer()) {
+    if (isActiveSummary() && renderer()) {
         if (event->type() == eventNames().DOMActivateEvent && !isClickableControl(event->target()->toNode())) {
             if (HTMLDetailsElement* details = detailsElement())
                 details->toggleOpen();
@@ -125,28 +107,29 @@ void HTMLSummaryElement::defaultEventHandler(Event* event)
             return;
         }
 
-        if (event->isKeyboardEvent()) {
-            if (event->type() == eventNames().keydownEvent && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "U+0020") {
+        if (is<KeyboardEvent>(*event)) {
+            KeyboardEvent& keyboardEvent = downcast<KeyboardEvent>(*event);
+            if (keyboardEvent.type() == eventNames().keydownEvent && keyboardEvent.keyIdentifier() == "U+0020") {
                 setActive(true, true);
                 // No setDefaultHandled() - IE dispatches a keypress in this case.
                 return;
             }
-            if (event->type() == eventNames().keypressEvent) {
-                switch (static_cast<KeyboardEvent*>(event)->charCode()) {
+            if (keyboardEvent.type() == eventNames().keypressEvent) {
+                switch (keyboardEvent.charCode()) {
                 case '\r':
                     dispatchSimulatedClick(event);
-                    event->setDefaultHandled();
+                    keyboardEvent.setDefaultHandled();
                     return;
                 case ' ':
                     // Prevent scrolling down the page.
-                    event->setDefaultHandled();
+                    keyboardEvent.setDefaultHandled();
                     return;
                 }
             }
-            if (event->type() == eventNames().keyupEvent && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "U+0020") {
+            if (keyboardEvent.type() == eventNames().keyupEvent && keyboardEvent.keyIdentifier() == "U+0020") {
                 if (active())
                     dispatchSimulatedClick(event);
-                event->setDefaultHandled();
+                keyboardEvent.setDefaultHandled();
                 return;
             }
         }
@@ -157,7 +140,7 @@ void HTMLSummaryElement::defaultEventHandler(Event* event)
 
 bool HTMLSummaryElement::willRespondToMouseClickEvents()
 {
-    if (isMainSummary() && renderer())
+    if (isActiveSummary() && renderer())
         return true;
 
     return HTMLElement::willRespondToMouseClickEvents();