Replace WTF::move with WTFMove
[WebKit-https.git] / Source / WebCore / html / HTMLSummaryElement.cpp
index daf126c..13d3cf4 100644 (file)
 
 #if ENABLE(DETAILS_ELEMENT)
 #include "DetailsMarkerControl.h"
-#include "ElementShadow.h"
-#include "HTMLContentElement.h"
 #include "HTMLDetailsElement.h"
-#include "HTMLNames.h"
+#include "HTMLFormControlElement.h"
+#include "HTMLSlotElement.h"
 #include "KeyboardEvent.h"
 #include "MouseEvent.h"
-#include "NodeRenderingContext.h"
 #include "PlatformMouseEvent.h"
-#include "RenderBlock.h"
+#include "RenderBlockFlow.h"
 #include "ShadowRoot.h"
 
 namespace WebCore {
 
 using namespace HTMLNames;
 
-class SummaryContentElement : public HTMLContentElement {
-public:
-    static PassRefPtr<SummaryContentElement> create(Document*);
-
-private:
-    SummaryContentElement(Document* document)
-        : HTMLContentElement(HTMLNames::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> result = adoptRef(new HTMLSummaryElement(tagName, document));
-    result->createShadowSubtree();
-    return result;
+    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)
+HTMLSummaryElement::HTMLSummaryElement(const QualifiedName& tagName, Document& document)
     : HTMLElement(tagName, document)
 {
     ASSERT(hasTagName(summaryTag));
 }
 
-RenderObject* HTMLSummaryElement::createRenderer(RenderArena* arena, RenderStyle*)
-{
-    return new (arena) RenderBlock(this);
-}
-
-bool HTMLSummaryElement::childShouldCreateRenderer(const NodeRenderingContext& childContext) const
+RenderPtr<RenderElement> HTMLSummaryElement::createElementRenderer(Ref<RenderStyle>&& style, const RenderTreePosition&)
 {
-    return childContext.isOnEncapsulationBoundary() && HTMLElement::childShouldCreateRenderer(childContext);
+    return createRenderer<RenderBlockFlow>(*this, WTFMove(style));
 }
 
-void HTMLSummaryElement::createShadowSubtree()
+void HTMLSummaryElement::didAddUserAgentShadowRoot(ShadowRoot* root)
 {
-    ASSERT(!shadow());
-    RefPtr<ShadowRoot> root = ShadowRoot::create(this, ShadowRoot::UserAgentShadowRoot);
-    root->appendChild(DetailsMarkerControl::create(document()), ASSERT_NO_EXCEPTION, true);
-    root->appendChild(SummaryContentElement::create(document()), ASSERT_NO_EXCEPTION, true);
+    root->appendChild(DetailsMarkerControl::create(document()), ASSERT_NO_EXCEPTION);
+    root->appendChild(HTMLSlotElement::create(slotTag, document()));
 }
 
 HTMLDetailsElement* HTMLSummaryElement::detailsElement() const
 {
-    Node* mayDetails = const_cast<HTMLSummaryElement*>(this)->parentNodeForRenderingAndStyle();
-    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 = toElement(element->shadowAncestorNode());
-    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();
@@ -127,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,6 +138,14 @@ void HTMLSummaryElement::defaultEventHandler(Event* event)
     HTMLElement::defaultEventHandler(event);
 }
 
+bool HTMLSummaryElement::willRespondToMouseClickEvents()
+{
+    if (isActiveSummary() && renderer())
+        return true;
+
+    return HTMLElement::willRespondToMouseClickEvents();
+}
+
 }
 
 #endif