luiz@webkit.org | 4ea5081 | 2011-02-17 21:28:51 +0000 | [diff] [blame] | 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 | |
morrita@google.com | 7403e02 | 2011-04-15 21:56:48 +0000 | [diff] [blame] | 24 | #include "DetailsMarkerControl.h" |
cdumez@apple.com | 06ee4a5 | 2016-06-15 20:59:07 +0000 | [diff] [blame] | 25 | #include "EventNames.h" |
luiz@webkit.org | fb0b8be | 2011-03-14 17:35:17 +0000 | [diff] [blame] | 26 | #include "HTMLDetailsElement.h" |
cdumez@apple.com | e1fa555 | 2014-09-25 03:50:40 +0000 | [diff] [blame] | 27 | #include "HTMLFormControlElement.h" |
antti@apple.com | 86e5895 | 2015-10-10 18:16:37 +0000 | [diff] [blame] | 28 | #include "HTMLSlotElement.h" |
morrita@google.com | beaa664 | 2012-02-24 09:59:07 +0000 | [diff] [blame] | 29 | #include "KeyboardEvent.h" |
morrita@google.com | 7403e02 | 2011-04-15 21:56:48 +0000 | [diff] [blame] | 30 | #include "MouseEvent.h" |
| 31 | #include "PlatformMouseEvent.h" |
hyatt@apple.com | 5388e67 | 2013-09-06 20:54:47 +0000 | [diff] [blame] | 32 | #include "RenderBlockFlow.h" |
antti@apple.com | 86e5895 | 2015-10-10 18:16:37 +0000 | [diff] [blame] | 33 | #include "ShadowRoot.h" |
bfulgham@apple.com | b516c15 | 2017-02-10 02:19:04 +0000 | [diff] [blame] | 34 | #include "SlotAssignment.h" |
fpizlo@apple.com | 197cd32 | 2018-03-17 06:11:00 +0000 | [diff] [blame] | 35 | #include <wtf/IsoMallocInlines.h> |
luiz@webkit.org | 4ea5081 | 2011-02-17 21:28:51 +0000 | [diff] [blame] | 36 | |
| 37 | namespace WebCore { |
| 38 | |
fpizlo@apple.com | 197cd32 | 2018-03-17 06:11:00 +0000 | [diff] [blame] | 39 | WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLSummaryElement); |
| 40 | |
luiz@webkit.org | 4ea5081 | 2011-02-17 21:28:51 +0000 | [diff] [blame] | 41 | using namespace HTMLNames; |
| 42 | |
bfulgham@apple.com | b516c15 | 2017-02-10 02:19:04 +0000 | [diff] [blame] | 43 | class SummarySlotElement final : public SlotAssignment { |
| 44 | private: |
| 45 | void hostChildElementDidChange(const Element&, ShadowRoot& shadowRoot) override |
| 46 | { |
| 47 | didChangeSlot(SlotAssignment::defaultSlotName(), shadowRoot); |
| 48 | } |
| 49 | |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 50 | const AtomString& slotNameForHostChild(const Node&) const override { return SlotAssignment::defaultSlotName(); } |
bfulgham@apple.com | b516c15 | 2017-02-10 02:19:04 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
weinig@apple.com | 02f433a | 2015-01-06 22:32:48 +0000 | [diff] [blame] | 53 | Ref<HTMLSummaryElement> HTMLSummaryElement::create(const QualifiedName& tagName, Document& document) |
luiz@webkit.org | 4ea5081 | 2011-02-17 21:28:51 +0000 | [diff] [blame] | 54 | { |
weinig@apple.com | 02f433a | 2015-01-06 22:32:48 +0000 | [diff] [blame] | 55 | Ref<HTMLSummaryElement> summary = adoptRef(*new HTMLSummaryElement(tagName, document)); |
ysuzuki@apple.com | 1d8e24d | 2019-08-19 06:59:40 +0000 | [diff] [blame] | 56 | summary->addShadowRoot(ShadowRoot::create(document, makeUnique<SummarySlotElement>())); |
akling@apple.com | dbdca2f | 2014-11-22 09:12:01 +0000 | [diff] [blame] | 57 | return summary; |
luiz@webkit.org | 4ea5081 | 2011-02-17 21:28:51 +0000 | [diff] [blame] | 58 | } |
| 59 | |
weinig@apple.com | 4917883 | 2013-09-15 00:39:29 +0000 | [diff] [blame] | 60 | HTMLSummaryElement::HTMLSummaryElement(const QualifiedName& tagName, Document& document) |
weinig@apple.com | dedf67e | 2013-09-15 05:23:01 +0000 | [diff] [blame] | 61 | : HTMLElement(tagName, document) |
luiz@webkit.org | 4ea5081 | 2011-02-17 21:28:51 +0000 | [diff] [blame] | 62 | { |
| 63 | ASSERT(hasTagName(summaryTag)); |
| 64 | } |
| 65 | |
antti@apple.com | 454418f | 2016-04-25 19:49:23 +0000 | [diff] [blame] | 66 | RenderPtr<RenderElement> HTMLSummaryElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&) |
luiz@webkit.org | fb0b8be | 2011-03-14 17:35:17 +0000 | [diff] [blame] | 67 | { |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 68 | return createRenderer<RenderBlockFlow>(*this, WTFMove(style)); |
luiz@webkit.org | fb0b8be | 2011-03-14 17:35:17 +0000 | [diff] [blame] | 69 | } |
| 70 | |
dbates@webkit.org | 62df276 | 2017-11-02 04:13:18 +0000 | [diff] [blame] | 71 | void HTMLSummaryElement::didAddUserAgentShadowRoot(ShadowRoot& root) |
morrita@google.com | 7403e02 | 2011-04-15 21:56:48 +0000 | [diff] [blame] | 72 | { |
dbates@webkit.org | 62df276 | 2017-11-02 04:13:18 +0000 | [diff] [blame] | 73 | root.appendChild(DetailsMarkerControl::create(document())); |
| 74 | root.appendChild(HTMLSlotElement::create(slotTag, document())); |
morrita@google.com | 7403e02 | 2011-04-15 21:56:48 +0000 | [diff] [blame] | 75 | } |
| 76 | |
jiewen_tan@apple.com | 667a08c | 2017-11-03 09:32:05 +0000 | [diff] [blame] | 77 | RefPtr<HTMLDetailsElement> HTMLSummaryElement::detailsElement() const |
morrita@google.com | 7403e02 | 2011-04-15 21:56:48 +0000 | [diff] [blame] | 78 | { |
antti@apple.com | 86e5895 | 2015-10-10 18:16:37 +0000 | [diff] [blame] | 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; |
morrita@google.com | 7403e02 | 2011-04-15 21:56:48 +0000 | [diff] [blame] | 87 | } |
| 88 | |
antti@apple.com | 86e5895 | 2015-10-10 18:16:37 +0000 | [diff] [blame] | 89 | bool HTMLSummaryElement::isActiveSummary() const |
morrita@google.com | 7403e02 | 2011-04-15 21:56:48 +0000 | [diff] [blame] | 90 | { |
jiewen_tan@apple.com | 147b934 | 2017-10-19 01:01:21 +0000 | [diff] [blame] | 91 | RefPtr<HTMLDetailsElement> details = detailsElement(); |
antti@apple.com | 86e5895 | 2015-10-10 18:16:37 +0000 | [diff] [blame] | 92 | if (!details) |
| 93 | return false; |
| 94 | return details->isActiveSummary(*this); |
morrita@google.com | 7403e02 | 2011-04-15 21:56:48 +0000 | [diff] [blame] | 95 | } |
| 96 | |
darin@apple.com | 17cae1e | 2017-11-13 06:12:09 +0000 | [diff] [blame] | 97 | static bool isClickableControl(EventTarget* target) |
commit-queue@webkit.org | ac687ea | 2011-12-14 04:59:33 +0000 | [diff] [blame] | 98 | { |
darin@apple.com | 17cae1e | 2017-11-13 06:12:09 +0000 | [diff] [blame] | 99 | if (!is<Element>(target)) |
commit-queue@webkit.org | ac687ea | 2011-12-14 04:59:33 +0000 | [diff] [blame] | 100 | return false; |
darin@apple.com | 17cae1e | 2017-11-13 06:12:09 +0000 | [diff] [blame] | 101 | auto& element = downcast<Element>(*target); |
| 102 | return is<HTMLFormControlElement>(element) || is<HTMLFormControlElement>(element.shadowHost()); |
commit-queue@webkit.org | ac687ea | 2011-12-14 04:59:33 +0000 | [diff] [blame] | 103 | } |
| 104 | |
rniwa@webkit.org | fa9cad4 | 2019-08-29 03:14:24 +0000 | [diff] [blame] | 105 | int HTMLSummaryElement::defaultTabIndex() const |
| 106 | { |
| 107 | return isActiveSummary() ? 0 : -1; |
| 108 | } |
| 109 | |
commit-queue@webkit.org | af24cbe | 2012-02-13 09:47:40 +0000 | [diff] [blame] | 110 | bool HTMLSummaryElement::supportsFocus() const |
| 111 | { |
antti@apple.com | 86e5895 | 2015-10-10 18:16:37 +0000 | [diff] [blame] | 112 | return isActiveSummary(); |
commit-queue@webkit.org | af24cbe | 2012-02-13 09:47:40 +0000 | [diff] [blame] | 113 | } |
| 114 | |
akling@apple.com | e8090dd | 2016-08-31 16:32:44 +0000 | [diff] [blame] | 115 | void HTMLSummaryElement::defaultEventHandler(Event& event) |
morrita@google.com | 7403e02 | 2011-04-15 21:56:48 +0000 | [diff] [blame] | 116 | { |
antti@apple.com | 86e5895 | 2015-10-10 18:16:37 +0000 | [diff] [blame] | 117 | if (isActiveSummary() && renderer()) { |
darin@apple.com | 17cae1e | 2017-11-13 06:12:09 +0000 | [diff] [blame] | 118 | if (event.type() == eventNames().DOMActivateEvent && !isClickableControl(event.target())) { |
jiewen_tan@apple.com | 147b934 | 2017-10-19 01:01:21 +0000 | [diff] [blame] | 119 | if (RefPtr<HTMLDetailsElement> details = detailsElement()) |
commit-queue@webkit.org | af24cbe | 2012-02-13 09:47:40 +0000 | [diff] [blame] | 120 | details->toggleOpen(); |
akling@apple.com | e8090dd | 2016-08-31 16:32:44 +0000 | [diff] [blame] | 121 | event.setDefaultHandled(); |
commit-queue@webkit.org | af24cbe | 2012-02-13 09:47:40 +0000 | [diff] [blame] | 122 | return; |
| 123 | } |
morrita@google.com | 7403e02 | 2011-04-15 21:56:48 +0000 | [diff] [blame] | 124 | |
akling@apple.com | e8090dd | 2016-08-31 16:32:44 +0000 | [diff] [blame] | 125 | if (is<KeyboardEvent>(event)) { |
| 126 | KeyboardEvent& keyboardEvent = downcast<KeyboardEvent>(event); |
cdumez@apple.com | 434faff | 2014-10-01 22:29:14 +0000 | [diff] [blame] | 127 | if (keyboardEvent.type() == eventNames().keydownEvent && keyboardEvent.keyIdentifier() == "U+0020") { |
commit-queue@webkit.org | af24cbe | 2012-02-13 09:47:40 +0000 | [diff] [blame] | 128 | setActive(true, true); |
| 129 | // No setDefaultHandled() - IE dispatches a keypress in this case. |
| 130 | return; |
| 131 | } |
cdumez@apple.com | 434faff | 2014-10-01 22:29:14 +0000 | [diff] [blame] | 132 | if (keyboardEvent.type() == eventNames().keypressEvent) { |
| 133 | switch (keyboardEvent.charCode()) { |
commit-queue@webkit.org | af24cbe | 2012-02-13 09:47:40 +0000 | [diff] [blame] | 134 | case '\r': |
akling@apple.com | e8090dd | 2016-08-31 16:32:44 +0000 | [diff] [blame] | 135 | dispatchSimulatedClick(&event); |
cdumez@apple.com | 434faff | 2014-10-01 22:29:14 +0000 | [diff] [blame] | 136 | keyboardEvent.setDefaultHandled(); |
commit-queue@webkit.org | af24cbe | 2012-02-13 09:47:40 +0000 | [diff] [blame] | 137 | return; |
| 138 | case ' ': |
| 139 | // Prevent scrolling down the page. |
cdumez@apple.com | 434faff | 2014-10-01 22:29:14 +0000 | [diff] [blame] | 140 | keyboardEvent.setDefaultHandled(); |
commit-queue@webkit.org | af24cbe | 2012-02-13 09:47:40 +0000 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | } |
cdumez@apple.com | 434faff | 2014-10-01 22:29:14 +0000 | [diff] [blame] | 144 | if (keyboardEvent.type() == eventNames().keyupEvent && keyboardEvent.keyIdentifier() == "U+0020") { |
commit-queue@webkit.org | af24cbe | 2012-02-13 09:47:40 +0000 | [diff] [blame] | 145 | if (active()) |
akling@apple.com | e8090dd | 2016-08-31 16:32:44 +0000 | [diff] [blame] | 146 | dispatchSimulatedClick(&event); |
cdumez@apple.com | 434faff | 2014-10-01 22:29:14 +0000 | [diff] [blame] | 147 | keyboardEvent.setDefaultHandled(); |
commit-queue@webkit.org | af24cbe | 2012-02-13 09:47:40 +0000 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | HTMLElement::defaultEventHandler(event); |
morrita@google.com | 7403e02 | 2011-04-15 21:56:48 +0000 | [diff] [blame] | 154 | } |
| 155 | |
allan.jensen@nokia.com | d4a972d | 2012-07-30 13:33:14 +0000 | [diff] [blame] | 156 | bool HTMLSummaryElement::willRespondToMouseClickEvents() |
| 157 | { |
antti@apple.com | 86e5895 | 2015-10-10 18:16:37 +0000 | [diff] [blame] | 158 | if (isActiveSummary() && renderer()) |
allan.jensen@nokia.com | d4a972d | 2012-07-30 13:33:14 +0000 | [diff] [blame] | 159 | return true; |
| 160 | |
| 161 | return HTMLElement::willRespondToMouseClickEvents(); |
| 162 | } |
| 163 | |
luiz@webkit.org | 4ea5081 | 2011-02-17 21:28:51 +0000 | [diff] [blame] | 164 | } |