HTMLLabelElement.cpp [plain text]
#include "config.h"
#include "HTMLLabelElement.h"
#include "Document.h"
#include "ElementIterator.h"
#include "Event.h"
#include "EventNames.h"
#include "FormAssociatedElement.h"
#include "HTMLNames.h"
namespace WebCore {
using namespace HTMLNames;
static LabelableElement* nodeAsSupportedLabelableElement(Node* node)
{
if (!node || !isLabelableElement(*node))
return nullptr;
LabelableElement& element = toLabelableElement(*node);
return element.supportLabels() ? &element : nullptr;
}
inline HTMLLabelElement::HTMLLabelElement(const QualifiedName& tagName, Document& document)
: HTMLElement(tagName, document)
{
ASSERT(hasTagName(labelTag));
}
PassRefPtr<HTMLLabelElement> HTMLLabelElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(new HTMLLabelElement(tagName, document));
}
bool HTMLLabelElement::isFocusable() const
{
return false;
}
LabelableElement* HTMLLabelElement::control()
{
const AtomicString& controlId = getAttribute(forAttr);
if (controlId.isNull()) {
for (auto& labelableElement : descendantsOfType<LabelableElement>(*this)) {
if (labelableElement.supportLabels())
return &labelableElement;
}
return nullptr;
}
return nodeAsSupportedLabelableElement(treeScope().getElementById(controlId));
}
HTMLFormElement* HTMLLabelElement::form() const
{
return FormAssociatedElement::findAssociatedForm(this, 0);
}
void HTMLLabelElement::setActive(bool down, bool pause)
{
if (down == active())
return;
HTMLElement::setActive(down, pause);
if (HTMLElement* element = control())
element->setActive(down, pause);
}
void HTMLLabelElement::setHovered(bool over)
{
if (over == hovered())
return;
HTMLElement::setHovered(over);
if (HTMLElement* element = control())
element->setHovered(over);
}
void HTMLLabelElement::defaultEventHandler(Event* evt)
{
static bool processingClick = false;
if (evt->type() == eventNames().clickEvent && !processingClick) {
RefPtr<HTMLElement> element = control();
if (!element || (evt->target() && element->containsIncludingShadowDOM(evt->target()->toNode())))
return;
processingClick = true;
element->dispatchSimulatedClick(evt);
document().updateLayoutIgnorePendingStylesheets();
if (element->isMouseFocusable())
element->focus();
processingClick = false;
evt->setDefaultHandled();
}
HTMLElement::defaultEventHandler(evt);
}
bool HTMLLabelElement::willRespondToMouseClickEvents()
{
return (control() && control()->willRespondToMouseClickEvents()) || HTMLElement::willRespondToMouseClickEvents();
}
void HTMLLabelElement::focus(bool, FocusDirection direction)
{
if (HTMLElement* element = control())
element->focus(true, direction);
}
void HTMLLabelElement::accessKeyAction(bool sendMouseEvents)
{
if (HTMLElement* element = control())
element->accessKeyAction(sendMouseEvents);
else
HTMLElement::accessKeyAction(sendMouseEvents);
}
}