This source file includes following definitions.
- supportsLabels
- create
- rendererIsFocusable
- control
- formOwner
- setActive
- setHovered
- isInteractiveContent
- isInInteractiveContent
- defaultEventHandler
- willRespondToMouseClickEvents
- focus
- accessKeyAction
#include "config.h"
#include "core/html/HTMLLabelElement.h"
#include "HTMLNames.h"
#include "core/dom/ElementTraversal.h"
#include "core/events/Event.h"
#include "core/html/FormAssociatedElement.h"
namespace WebCore {
using namespace HTMLNames;
static bool supportsLabels(const Element& element)
{
if (!element.isHTMLElement())
return false;
if (!toHTMLElement(element).isLabelable())
return false;
return toLabelableElement(element).supportLabels();
}
inline HTMLLabelElement::HTMLLabelElement(Document& document)
: HTMLElement(labelTag, document)
{
ScriptWrappable::init(this);
}
PassRefPtr<HTMLLabelElement> HTMLLabelElement::create(Document& document)
{
return adoptRef(new HTMLLabelElement(document));
}
bool HTMLLabelElement::rendererIsFocusable() const
{
HTMLLabelElement* that = const_cast<HTMLLabelElement*>(this);
return that->isContentEditable();
}
LabelableElement* HTMLLabelElement::control() const
{
const AtomicString& controlId = getAttribute(forAttr);
if (controlId.isNull()) {
for (Element* element = ElementTraversal::next(*this, this); element; element = ElementTraversal::next(*element, this)) {
if (!supportsLabels(*element))
continue;
return toLabelableElement(element);
}
return 0;
}
if (Element* element = treeScope().getElementById(controlId)) {
if (supportsLabels(*element))
return toLabelableElement(element);
}
return 0;
}
HTMLFormElement* HTMLLabelElement::formOwner() const
{
return FormAssociatedElement::findAssociatedForm(this);
}
void HTMLLabelElement::setActive(bool down)
{
if (down == active())
return;
HTMLElement::setActive(down);
if (HTMLElement* element = control())
element->setActive(down);
}
void HTMLLabelElement::setHovered(bool over)
{
if (over == hovered())
return;
HTMLElement::setHovered(over);
if (HTMLElement* element = control())
element->setHovered(over);
}
bool HTMLLabelElement::isInteractiveContent() const
{
return true;
}
bool HTMLLabelElement::isInInteractiveContent(Node* node) const
{
if (!containsIncludingShadowDOM(node))
return false;
while (node && this != node) {
if (node->isHTMLElement() && toHTMLElement(node)->isInteractiveContent())
return true;
node = node->parentOrShadowHostNode();
}
return false;
}
void HTMLLabelElement::defaultEventHandler(Event* evt)
{
static bool processingClick = false;
if (evt->type() == EventTypeNames::click && !processingClick) {
RefPtr<HTMLElement> element = control();
if (!element || (evt->target() && element->containsIncludingShadowDOM(evt->target()->toNode())))
return;
if (evt->target() && isInInteractiveContent(evt->target()->toNode()))
return;
processingClick = true;
document().updateLayoutIgnorePendingStylesheets();
if (element->isMouseFocusable())
element->focus(true, FocusTypeMouse);
element->dispatchSimulatedClick(evt);
processingClick = false;
evt->setDefaultHandled();
}
HTMLElement::defaultEventHandler(evt);
}
bool HTMLLabelElement::willRespondToMouseClickEvents()
{
if (control() && control()->willRespondToMouseClickEvents())
return true;
return HTMLElement::willRespondToMouseClickEvents();
}
void HTMLLabelElement::focus(bool, FocusType type)
{
if (HTMLElement* element = control())
element->focus(true, type);
if (isFocusable())
HTMLElement::focus(true, type);
}
void HTMLLabelElement::accessKeyAction(bool sendMouseEvents)
{
if (HTMLElement* element = control())
element->accessKeyAction(sendMouseEvents);
else
HTMLElement::accessKeyAction(sendMouseEvents);
}
}