HTMLLabelElement.cpp [plain text]
#include "config.h"
#include "HTMLLabelElement.h"
#include "Document.h"
#include "Event.h"
#include "EventNames.h"
#include "HTMLFormControlElement.h"
#include "HTMLFormElement.h"
#include "HTMLNames.h"
namespace WebCore {
using namespace HTMLNames;
static HTMLFormControlElement* nodeAsLabelableFormControl(Node* node)
{
if (!node || !node->isElementNode() || !static_cast<Element*>(node)->isFormControlElement())
return 0;
HTMLFormControlElement* formControlElement = static_cast<HTMLFormControlElement*>(node);
if (!formControlElement->isLabelable())
return 0;
return formControlElement;
}
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;
}
HTMLFormControlElement* HTMLLabelElement::control()
{
const AtomicString& controlId = getAttribute(forAttr);
if (controlId.isNull()) {
Node* node = this;
while ((node = node->traverseNextNode(this))) {
if (HTMLFormControlElement* formControlElement = nodeAsLabelableFormControl(node))
return formControlElement;
}
return 0;
}
return nodeAsLabelableFormControl(treeScope()->getElementById(controlId));
}
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);
}
void HTMLLabelElement::focus(bool)
{
if (HTMLElement* element = control())
element->focus();
}
void HTMLLabelElement::accessKeyAction(bool sendToAnyElement)
{
if (HTMLElement* element = control())
element->accessKeyAction(sendToAnyElement);
else
HTMLElement::accessKeyAction(sendToAnyElement);
}
void HTMLLabelElement::parseMappedAttribute(Attribute* attribute)
{
if (attribute->name() == forAttr) {
if (document())
document()->notifyLocalNodeListsLabelChanged();
} else
HTMLElement::parseMappedAttribute(attribute);
}
}