2 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "core/html/HTMLSummaryElement.h"
24 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
25 #include "core/HTMLNames.h"
26 #include "core/events/KeyboardEvent.h"
27 #include "core/dom/NodeRenderingTraversal.h"
28 #include "core/dom/shadow/ShadowRoot.h"
29 #include "core/html/HTMLContentElement.h"
30 #include "core/html/HTMLDetailsElement.h"
31 #include "core/html/shadow/DetailsMarkerControl.h"
32 #include "core/rendering/RenderBlockFlow.h"
36 using namespace HTMLNames;
38 PassRefPtrWillBeRawPtr<HTMLSummaryElement> HTMLSummaryElement::create(Document& document)
40 RefPtrWillBeRawPtr<HTMLSummaryElement> summary = adoptRefWillBeNoop(new HTMLSummaryElement(document));
41 summary->ensureUserAgentShadowRoot();
42 return summary.release();
45 HTMLSummaryElement::HTMLSummaryElement(Document& document)
46 : HTMLElement(summaryTag, document)
50 RenderObject* HTMLSummaryElement::createRenderer(RenderStyle*)
52 return new RenderBlockFlow(this);
55 void HTMLSummaryElement::didAddUserAgentShadowRoot(ShadowRoot& root)
57 root.appendChild(DetailsMarkerControl::create(document()));
58 root.appendChild(HTMLContentElement::create(document()));
61 HTMLDetailsElement* HTMLSummaryElement::detailsElement() const
63 Node* parent = NodeRenderingTraversal::parent(this);
64 if (isHTMLDetailsElement(parent))
65 return toHTMLDetailsElement(parent);
69 bool HTMLSummaryElement::isMainSummary() const
71 if (HTMLDetailsElement* details = detailsElement())
72 return details->findMainSummary() == this;
77 static bool isClickableControl(Node* node)
79 if (!node->isElementNode())
81 Element* element = toElement(node);
82 if (element->isFormControlElement())
84 Element* host = element->shadowHost();
85 return host && host->isFormControlElement();
88 bool HTMLSummaryElement::supportsFocus() const
90 return isMainSummary();
93 void HTMLSummaryElement::defaultEventHandler(Event* event)
95 if (isMainSummary() && renderer()) {
96 if (event->type() == EventTypeNames::DOMActivate && !isClickableControl(event->target()->toNode())) {
97 if (HTMLDetailsElement* details = detailsElement())
98 details->toggleOpen();
99 event->setDefaultHandled();
103 if (event->isKeyboardEvent()) {
104 if (event->type() == EventTypeNames::keydown && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
106 // No setDefaultHandled() - IE dispatches a keypress in this case.
109 if (event->type() == EventTypeNames::keypress) {
110 switch (toKeyboardEvent(event)->charCode()) {
112 dispatchSimulatedClick(event);
113 event->setDefaultHandled();
116 // Prevent scrolling down the page.
117 event->setDefaultHandled();
121 if (event->type() == EventTypeNames::keyup && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
123 dispatchSimulatedClick(event);
124 event->setDefaultHandled();
130 HTMLElement::defaultEventHandler(event);
133 bool HTMLSummaryElement::willRespondToMouseClickEvents()
135 if (isMainSummary() && renderer())
138 return HTMLElement::willRespondToMouseClickEvents();