1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004-2022 Apple Inc. All rights reserved.
* (C) 2006 Alexey Proskuryakov (ap@nypop.com)
* Copyright (C) 2014 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "HTMLLabelElement.h"
#include "Document.h"
#include "Event.h"
#include "EventNames.h"
#include "FormListedElement.h"
#include "HTMLFormControlElement.h"
#include "HTMLNames.h"
#include "MouseEvent.h"
#include "SelectionRestorationMode.h"
#include "TypedElementDescendantIteratorInlines.h"
#include <wtf/SetForScope.h>
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(HTMLLabelElement);
using namespace HTMLNames;
static HTMLElement* elementForAttributeIfLabelable(const HTMLLabelElement& context, const QualifiedName& attributeName)
{
if (RefPtr element = context.elementForAttributeInternal(attributeName)) {
if (auto* labelableElement = dynamicDowncast<HTMLElement>(*element)) {
if (labelableElement->isLabelable())
return labelableElement;
}
}
return nullptr;
}
inline HTMLLabelElement::HTMLLabelElement(const QualifiedName& tagName, Document& document)
: HTMLElement(tagName, document)
{
ASSERT(hasTagName(labelTag));
}
Ref<HTMLLabelElement> HTMLLabelElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new HTMLLabelElement(tagName, document));
}
Ref<HTMLLabelElement> HTMLLabelElement::create(Document& document)
{
return adoptRef(*new HTMLLabelElement(labelTag, document));
}
RefPtr<HTMLElement> HTMLLabelElement::control() const
{
if (!hasAttributeWithoutSynchronization(forAttr)) {
// https://html.spec.whatwg.org/multipage/forms.html#labeled-control
for (auto& descendant : descendantsOfType<HTMLElement>(*this)) {
if (document().settings().shadowRootReferenceTargetEnabled()) {
RefPtr referenceTarget = dynamicDowncast<HTMLElement>(descendant.resolveReferenceTarget());
if (referenceTarget && referenceTarget->isLabelable())
return referenceTarget.get();
continue;
}
if (descendant.isLabelable())
return const_cast<HTMLElement*>(&descendant);
}
return nullptr;
}
return isConnected() ? elementForAttributeIfLabelable(*this, forAttr) : nullptr;
}
RefPtr<HTMLElement> HTMLLabelElement::controlForBindings() const
{
return dynamicDowncast<HTMLElement>(retargetReferenceTargetForBindings(control()));
}
HTMLFormElement* HTMLLabelElement::form() const
{
if (auto element = control()) {
if (auto* listedElement = element->asValidatedFormListedElement())
return listedElement->form();
}
return nullptr;
}
HTMLFormElement* HTMLLabelElement::formForBindings() const
{
// FIXME: The downcast should be unnecessary, but the WPT was written before https://github.com/WICG/webcomponents/issues/1072 was resolved. Update once the WPT has been updated.
return dynamicDowncast<HTMLFormElement>(retargetReferenceTargetForBindings(form())).get();
}
void HTMLLabelElement::setActive(bool down, Style::InvalidationScope invalidationScope)
{
if (down == active())
return;
// Update our status first.
HTMLElement::setActive(down, invalidationScope);
// Also update our corresponding control.
if (auto element = control())
element->setActive(down);
}
void HTMLLabelElement::setHovered(bool over, Style::InvalidationScope invalidationScope, HitTestRequest request)
{
if (over == hovered())
return;
// Update our status first.
HTMLElement::setHovered(over, invalidationScope, request);
// Also update our corresponding control.
if (auto element = control())
element->setHovered(over);
}
bool HTMLLabelElement::isEventTargetedAtInteractiveDescendants(Event& event) const
{
RefPtr node = dynamicDowncast<Node>(*event.target());
if (!node)
return false;
if (!containsIncludingShadowDOM(node.get()))
return false;
for (const auto* it = node.get(); it && it != this; it = it->parentElementInComposedTree()) {
auto* element = dynamicDowncast<HTMLElement>(*it);
if (element && element->isInteractiveContent())
return true;
}
return false;
}
void HTMLLabelElement::defaultEventHandler(Event& event)
{
if (isAnyClick(event) && !m_processingClick) {
auto control = this->control();
// If we can't find a control or if the control received the click
// event, then there's no need for us to do anything.
auto* eventTarget = dynamicDowncast<Node>(event.target());
if (!control || (eventTarget && control->containsIncludingShadowDOM(eventTarget))) {
HTMLElement::defaultEventHandler(event);
return;
}
// The activation behavior of a label element for events targeted at interactive
// content descendants of a label element, and any descendants of those interactive
// content descendants, must be to do nothing.
// https://html.spec.whatwg.org/#the-label-element
if (isEventTargetedAtInteractiveDescendants(event)) {
HTMLElement::defaultEventHandler(event);
return;
}
SetForScope processingClick(m_processingClick, true);
control->dispatchSimulatedClick(&event);
protectedDocument()->updateLayoutIgnorePendingStylesheets();
if (control->isMouseFocusable())
control->focus({ { }, { }, { }, FocusTrigger::Click, { } });
event.setDefaultHandled();
}
HTMLElement::defaultEventHandler(event);
}
bool HTMLLabelElement::willRespondToMouseClickEventsWithEditability(Editability editability) const
{
auto element = control();
return (element && element->willRespondToMouseClickEventsWithEditability(editability)) || HTMLElement::willRespondToMouseClickEventsWithEditability(editability);
}
void HTMLLabelElement::focus(const FocusOptions& options)
{
Ref<HTMLLabelElement> protectedThis(*this);
auto document = protectedDocument();
if (document->haveStylesheetsLoaded()) {
document->updateLayout();
if (isFocusable()) {
// The value of restorationMode is not used for label elements as it doesn't override updateFocusAppearance.
Element::focus(options);
return;
}
}
// To match other browsers, always restore previous selection.
if (auto element = control())
element->focus({ SelectionRestorationMode::RestoreOrSelectAll, options.direction });
}
bool HTMLLabelElement::accessKeyAction(bool sendMouseEvents)
{
if (auto element = control())
return element->accessKeyAction(sendMouseEvents);
return HTMLElement::accessKeyAction(sendMouseEvents);
}
auto HTMLLabelElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree) -> InsertedIntoAncestorResult
{
auto result = HTMLElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);
if (parentOfInsertedTree.isInTreeScope() && insertionType.treeScopeChanged) {
auto& newScope = parentOfInsertedTree.treeScope();
if (newScope.shouldCacheLabelsByForAttribute())
updateLabel(newScope, nullAtom(), attributeWithoutSynchronization(forAttr));
}
return result;
}
void HTMLLabelElement::updateLabel(TreeScope& scope, const AtomString& oldForAttributeValue, const AtomString& newForAttributeValue)
{
if (!isConnected())
return;
if (oldForAttributeValue == newForAttributeValue)
return;
if (!oldForAttributeValue.isEmpty())
scope.removeLabel(oldForAttributeValue, *this);
if (!newForAttributeValue.isEmpty())
scope.addLabel(newForAttributeValue, *this);
}
void HTMLLabelElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree)
{
if (oldParentOfRemovedTree.isInTreeScope() && removalType.treeScopeChanged) {
auto& oldScope = oldParentOfRemovedTree.treeScope();
if (oldScope.shouldCacheLabelsByForAttribute())
updateLabel(oldScope, attributeWithoutSynchronization(forAttr), nullAtom());
}
HTMLElement::removedFromAncestor(removalType, oldParentOfRemovedTree);
}
} // namespace
|