blob: 3a129afd854a7b34397863c08554e75274bb14d4 [file] [log] [blame]
luiz@webkit.org4ea50812011-02-17 21:28:51 +00001/*
2 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3 *
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.
8 *
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.
13 *
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.
18 *
19 */
20
21#include "config.h"
22#include "HTMLSummaryElement.h"
23
morrita@google.com7403e022011-04-15 21:56:48 +000024#include "DetailsMarkerControl.h"
cdumez@apple.com06ee4a52016-06-15 20:59:07 +000025#include "EventNames.h"
luiz@webkit.orgfb0b8be2011-03-14 17:35:17 +000026#include "HTMLDetailsElement.h"
cdumez@apple.come1fa5552014-09-25 03:50:40 +000027#include "HTMLFormControlElement.h"
antti@apple.com86e58952015-10-10 18:16:37 +000028#include "HTMLSlotElement.h"
morrita@google.combeaa6642012-02-24 09:59:07 +000029#include "KeyboardEvent.h"
morrita@google.com7403e022011-04-15 21:56:48 +000030#include "MouseEvent.h"
31#include "PlatformMouseEvent.h"
hyatt@apple.com5388e672013-09-06 20:54:47 +000032#include "RenderBlockFlow.h"
antti@apple.com86e58952015-10-10 18:16:37 +000033#include "ShadowRoot.h"
bfulgham@apple.comb516c152017-02-10 02:19:04 +000034#include "SlotAssignment.h"
fpizlo@apple.com197cd322018-03-17 06:11:00 +000035#include <wtf/IsoMallocInlines.h>
luiz@webkit.org4ea50812011-02-17 21:28:51 +000036
37namespace WebCore {
38
fpizlo@apple.com197cd322018-03-17 06:11:00 +000039WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLSummaryElement);
40
luiz@webkit.org4ea50812011-02-17 21:28:51 +000041using namespace HTMLNames;
42
bfulgham@apple.comb516c152017-02-10 02:19:04 +000043class SummarySlotElement final : public SlotAssignment {
44private:
45 void hostChildElementDidChange(const Element&, ShadowRoot& shadowRoot) override
46 {
47 didChangeSlot(SlotAssignment::defaultSlotName(), shadowRoot);
48 }
49
darin@apple.com0ce67df2019-06-17 01:48:13 +000050 const AtomString& slotNameForHostChild(const Node&) const override { return SlotAssignment::defaultSlotName(); }
bfulgham@apple.comb516c152017-02-10 02:19:04 +000051};
52
weinig@apple.com02f433a2015-01-06 22:32:48 +000053Ref<HTMLSummaryElement> HTMLSummaryElement::create(const QualifiedName& tagName, Document& document)
luiz@webkit.org4ea50812011-02-17 21:28:51 +000054{
weinig@apple.com02f433a2015-01-06 22:32:48 +000055 Ref<HTMLSummaryElement> summary = adoptRef(*new HTMLSummaryElement(tagName, document));
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +000056 summary->addShadowRoot(ShadowRoot::create(document, makeUnique<SummarySlotElement>()));
akling@apple.comdbdca2f2014-11-22 09:12:01 +000057 return summary;
luiz@webkit.org4ea50812011-02-17 21:28:51 +000058}
59
weinig@apple.com49178832013-09-15 00:39:29 +000060HTMLSummaryElement::HTMLSummaryElement(const QualifiedName& tagName, Document& document)
weinig@apple.comdedf67e2013-09-15 05:23:01 +000061 : HTMLElement(tagName, document)
luiz@webkit.org4ea50812011-02-17 21:28:51 +000062{
63 ASSERT(hasTagName(summaryTag));
64}
65
antti@apple.com454418f2016-04-25 19:49:23 +000066RenderPtr<RenderElement> HTMLSummaryElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
luiz@webkit.orgfb0b8be2011-03-14 17:35:17 +000067{
aestes@apple.com13aae082016-01-02 08:03:08 +000068 return createRenderer<RenderBlockFlow>(*this, WTFMove(style));
luiz@webkit.orgfb0b8be2011-03-14 17:35:17 +000069}
70
dbates@webkit.org62df2762017-11-02 04:13:18 +000071void HTMLSummaryElement::didAddUserAgentShadowRoot(ShadowRoot& root)
morrita@google.com7403e022011-04-15 21:56:48 +000072{
dbates@webkit.org62df2762017-11-02 04:13:18 +000073 root.appendChild(DetailsMarkerControl::create(document()));
74 root.appendChild(HTMLSlotElement::create(slotTag, document()));
morrita@google.com7403e022011-04-15 21:56:48 +000075}
76
jiewen_tan@apple.com667a08c2017-11-03 09:32:05 +000077RefPtr<HTMLDetailsElement> HTMLSummaryElement::detailsElement() const
morrita@google.com7403e022011-04-15 21:56:48 +000078{
antti@apple.com86e58952015-10-10 18:16:37 +000079 auto* parent = parentElement();
80 if (parent && is<HTMLDetailsElement>(*parent))
81 return downcast<HTMLDetailsElement>(parent);
82 // Fallback summary element is in the shadow tree.
83 auto* host = shadowHost();
84 if (host && is<HTMLDetailsElement>(*host))
85 return downcast<HTMLDetailsElement>(host);
86 return nullptr;
morrita@google.com7403e022011-04-15 21:56:48 +000087}
88
antti@apple.com86e58952015-10-10 18:16:37 +000089bool HTMLSummaryElement::isActiveSummary() const
morrita@google.com7403e022011-04-15 21:56:48 +000090{
jiewen_tan@apple.com147b9342017-10-19 01:01:21 +000091 RefPtr<HTMLDetailsElement> details = detailsElement();
antti@apple.com86e58952015-10-10 18:16:37 +000092 if (!details)
93 return false;
94 return details->isActiveSummary(*this);
morrita@google.com7403e022011-04-15 21:56:48 +000095}
96
darin@apple.com17cae1e2017-11-13 06:12:09 +000097static bool isClickableControl(EventTarget* target)
commit-queue@webkit.orgac687ea2011-12-14 04:59:33 +000098{
darin@apple.com17cae1e2017-11-13 06:12:09 +000099 if (!is<Element>(target))
commit-queue@webkit.orgac687ea2011-12-14 04:59:33 +0000100 return false;
darin@apple.com17cae1e2017-11-13 06:12:09 +0000101 auto& element = downcast<Element>(*target);
102 return is<HTMLFormControlElement>(element) || is<HTMLFormControlElement>(element.shadowHost());
commit-queue@webkit.orgac687ea2011-12-14 04:59:33 +0000103}
104
rniwa@webkit.orgfa9cad42019-08-29 03:14:24 +0000105int HTMLSummaryElement::defaultTabIndex() const
106{
107 return isActiveSummary() ? 0 : -1;
108}
109
commit-queue@webkit.orgaf24cbe2012-02-13 09:47:40 +0000110bool HTMLSummaryElement::supportsFocus() const
111{
antti@apple.com86e58952015-10-10 18:16:37 +0000112 return isActiveSummary();
commit-queue@webkit.orgaf24cbe2012-02-13 09:47:40 +0000113}
114
akling@apple.come8090dd2016-08-31 16:32:44 +0000115void HTMLSummaryElement::defaultEventHandler(Event& event)
morrita@google.com7403e022011-04-15 21:56:48 +0000116{
antti@apple.com86e58952015-10-10 18:16:37 +0000117 if (isActiveSummary() && renderer()) {
darin@apple.com17cae1e2017-11-13 06:12:09 +0000118 if (event.type() == eventNames().DOMActivateEvent && !isClickableControl(event.target())) {
jiewen_tan@apple.com147b9342017-10-19 01:01:21 +0000119 if (RefPtr<HTMLDetailsElement> details = detailsElement())
commit-queue@webkit.orgaf24cbe2012-02-13 09:47:40 +0000120 details->toggleOpen();
akling@apple.come8090dd2016-08-31 16:32:44 +0000121 event.setDefaultHandled();
commit-queue@webkit.orgaf24cbe2012-02-13 09:47:40 +0000122 return;
123 }
morrita@google.com7403e022011-04-15 21:56:48 +0000124
akling@apple.come8090dd2016-08-31 16:32:44 +0000125 if (is<KeyboardEvent>(event)) {
126 KeyboardEvent& keyboardEvent = downcast<KeyboardEvent>(event);
cdumez@apple.com434faff2014-10-01 22:29:14 +0000127 if (keyboardEvent.type() == eventNames().keydownEvent && keyboardEvent.keyIdentifier() == "U+0020") {
commit-queue@webkit.orgaf24cbe2012-02-13 09:47:40 +0000128 setActive(true, true);
129 // No setDefaultHandled() - IE dispatches a keypress in this case.
130 return;
131 }
cdumez@apple.com434faff2014-10-01 22:29:14 +0000132 if (keyboardEvent.type() == eventNames().keypressEvent) {
133 switch (keyboardEvent.charCode()) {
commit-queue@webkit.orgaf24cbe2012-02-13 09:47:40 +0000134 case '\r':
akling@apple.come8090dd2016-08-31 16:32:44 +0000135 dispatchSimulatedClick(&event);
cdumez@apple.com434faff2014-10-01 22:29:14 +0000136 keyboardEvent.setDefaultHandled();
commit-queue@webkit.orgaf24cbe2012-02-13 09:47:40 +0000137 return;
138 case ' ':
139 // Prevent scrolling down the page.
cdumez@apple.com434faff2014-10-01 22:29:14 +0000140 keyboardEvent.setDefaultHandled();
commit-queue@webkit.orgaf24cbe2012-02-13 09:47:40 +0000141 return;
142 }
143 }
cdumez@apple.com434faff2014-10-01 22:29:14 +0000144 if (keyboardEvent.type() == eventNames().keyupEvent && keyboardEvent.keyIdentifier() == "U+0020") {
commit-queue@webkit.orgaf24cbe2012-02-13 09:47:40 +0000145 if (active())
akling@apple.come8090dd2016-08-31 16:32:44 +0000146 dispatchSimulatedClick(&event);
cdumez@apple.com434faff2014-10-01 22:29:14 +0000147 keyboardEvent.setDefaultHandled();
commit-queue@webkit.orgaf24cbe2012-02-13 09:47:40 +0000148 return;
149 }
150 }
151 }
152
153 HTMLElement::defaultEventHandler(event);
morrita@google.com7403e022011-04-15 21:56:48 +0000154}
155
allan.jensen@nokia.comd4a972d2012-07-30 13:33:14 +0000156bool HTMLSummaryElement::willRespondToMouseClickEvents()
157{
antti@apple.com86e58952015-10-10 18:16:37 +0000158 if (isActiveSummary() && renderer())
allan.jensen@nokia.comd4a972d2012-07-30 13:33:14 +0000159 return true;
160
161 return HTMLElement::willRespondToMouseClickEvents();
162}
163
luiz@webkit.org4ea50812011-02-17 21:28:51 +0000164}