HTMLLabelElement.cpp [plain text]
#include "config.h"
#include "HTMLLabelElement.h"
#include "Document.h"
#include "Event.h"
#include "EventNames.h"
#include "FormAssociatedElement.h"
#include "HTMLNames.h"
#include "NodeTraversal.h"
namespace WebCore {
using namespace HTMLNames;
static LabelableElement* nodeAsLabelableElement(Node* node)
{
if (!node || !node->isHTMLElement())
return 0;
HTMLElement* element = static_cast<HTMLElement*>(node);
if (!element->isLabelable())
return 0;
LabelableElement* labelableElement = static_cast<LabelableElement*>(element);
if (!labelableElement->supportLabels())
return 0;
return labelableElement;
}
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()) {
Element* element = this;
while ((element = ElementTraversal::next(element, this))) {
if (LabelableElement* labelableElement = nodeAsLabelableElement(element))
return labelableElement;
}
return 0;
}
return nodeAsLabelableElement(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);
if (element->isMouseFocusable())
element->focus();
processingClick = false;
evt->setDefaultHandled();
}
HTMLElement::defaultEventHandler(evt);
}
bool HTMLLabelElement::willRespondToMouseClickEvents()
{
if (control() && control()->willRespondToMouseClickEvents())
return true;
return 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);
}
}