1 | /* |
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 | |
24 | #include "DetailsMarkerControl.h" |
25 | #include "EventNames.h" |
26 | #include "HTMLDetailsElement.h" |
27 | #include "HTMLFormControlElement.h" |
28 | #include "HTMLSlotElement.h" |
29 | #include "KeyboardEvent.h" |
30 | #include "MouseEvent.h" |
31 | #include "PlatformMouseEvent.h" |
32 | #include "RenderBlockFlow.h" |
33 | #include "ShadowRoot.h" |
34 | #include "SlotAssignment.h" |
35 | #include <wtf/IsoMallocInlines.h> |
36 | |
37 | namespace WebCore { |
38 | |
39 | WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLSummaryElement); |
40 | |
41 | using namespace HTMLNames; |
42 | |
43 | class SummarySlotElement final : public SlotAssignment { |
44 | private: |
45 | void hostChildElementDidChange(const Element&, ShadowRoot& shadowRoot) override |
46 | { |
47 | didChangeSlot(SlotAssignment::defaultSlotName(), shadowRoot); |
48 | } |
49 | |
50 | const AtomicString& slotNameForHostChild(const Node&) const override { return SlotAssignment::defaultSlotName(); } |
51 | }; |
52 | |
53 | Ref<HTMLSummaryElement> HTMLSummaryElement::create(const QualifiedName& tagName, Document& document) |
54 | { |
55 | Ref<HTMLSummaryElement> summary = adoptRef(*new HTMLSummaryElement(tagName, document)); |
56 | summary->addShadowRoot(ShadowRoot::create(document, std::make_unique<SummarySlotElement>())); |
57 | return summary; |
58 | } |
59 | |
60 | HTMLSummaryElement::HTMLSummaryElement(const QualifiedName& tagName, Document& document) |
61 | : HTMLElement(tagName, document) |
62 | { |
63 | ASSERT(hasTagName(summaryTag)); |
64 | } |
65 | |
66 | RenderPtr<RenderElement> HTMLSummaryElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&) |
67 | { |
68 | return createRenderer<RenderBlockFlow>(*this, WTFMove(style)); |
69 | } |
70 | |
71 | void HTMLSummaryElement::didAddUserAgentShadowRoot(ShadowRoot& root) |
72 | { |
73 | root.appendChild(DetailsMarkerControl::create(document())); |
74 | root.appendChild(HTMLSlotElement::create(slotTag, document())); |
75 | } |
76 | |
77 | RefPtr<HTMLDetailsElement> HTMLSummaryElement::detailsElement() const |
78 | { |
79 | 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; |
87 | } |
88 | |
89 | bool HTMLSummaryElement::isActiveSummary() const |
90 | { |
91 | RefPtr<HTMLDetailsElement> details = detailsElement(); |
92 | if (!details) |
93 | return false; |
94 | return details->isActiveSummary(*this); |
95 | } |
96 | |
97 | static bool isClickableControl(EventTarget* target) |
98 | { |
99 | if (!is<Element>(target)) |
100 | return false; |
101 | auto& element = downcast<Element>(*target); |
102 | return is<HTMLFormControlElement>(element) || is<HTMLFormControlElement>(element.shadowHost()); |
103 | } |
104 | |
105 | bool HTMLSummaryElement::supportsFocus() const |
106 | { |
107 | return isActiveSummary(); |
108 | } |
109 | |
110 | void HTMLSummaryElement::defaultEventHandler(Event& event) |
111 | { |
112 | if (isActiveSummary() && renderer()) { |
113 | if (event.type() == eventNames().DOMActivateEvent && !isClickableControl(event.target())) { |
114 | if (RefPtr<HTMLDetailsElement> details = detailsElement()) |
115 | details->toggleOpen(); |
116 | event.setDefaultHandled(); |
117 | return; |
118 | } |
119 | |
120 | if (is<KeyboardEvent>(event)) { |
121 | KeyboardEvent& keyboardEvent = downcast<KeyboardEvent>(event); |
122 | if (keyboardEvent.type() == eventNames().keydownEvent && keyboardEvent.keyIdentifier() == "U+0020" ) { |
123 | setActive(true, true); |
124 | // No setDefaultHandled() - IE dispatches a keypress in this case. |
125 | return; |
126 | } |
127 | if (keyboardEvent.type() == eventNames().keypressEvent) { |
128 | switch (keyboardEvent.charCode()) { |
129 | case '\r': |
130 | dispatchSimulatedClick(&event); |
131 | keyboardEvent.setDefaultHandled(); |
132 | return; |
133 | case ' ': |
134 | // Prevent scrolling down the page. |
135 | keyboardEvent.setDefaultHandled(); |
136 | return; |
137 | } |
138 | } |
139 | if (keyboardEvent.type() == eventNames().keyupEvent && keyboardEvent.keyIdentifier() == "U+0020" ) { |
140 | if (active()) |
141 | dispatchSimulatedClick(&event); |
142 | keyboardEvent.setDefaultHandled(); |
143 | return; |
144 | } |
145 | } |
146 | } |
147 | |
148 | HTMLElement::defaultEventHandler(event); |
149 | } |
150 | |
151 | bool HTMLSummaryElement::willRespondToMouseClickEvents() |
152 | { |
153 | if (isActiveSummary() && renderer()) |
154 | return true; |
155 | |
156 | return HTMLElement::willRespondToMouseClickEvents(); |
157 | } |
158 | |
159 | } |
160 | |