HTMLFormControlElement.cpp [plain text]
#include "config.h"
#include "HTMLFormControlElement.h"
#include "Attribute.h"
#include "Event.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "Frame.h"
#include "HTMLFieldSetElement.h"
#include "HTMLFormElement.h"
#include "HTMLInputElement.h"
#include "HTMLLegendElement.h"
#include "RenderBox.h"
#include "RenderTheme.h"
#include "ScriptEventListener.h"
#include "ValidationMessage.h"
#include "ValidityState.h"
#include <wtf/Vector.h>
namespace WebCore {
using namespace HTMLNames;
using namespace std;
HTMLFormControlElement::HTMLFormControlElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
: LabelableElement(tagName, document)
, m_disabled(false)
, m_readOnly(false)
, m_required(false)
, m_valueMatchesRenderer(false)
, m_ancestorDisabledState(AncestorDisabledStateUnknown)
, m_dataListAncestorState(Unknown)
, m_willValidateInitialized(false)
, m_willValidate(true)
, m_isValid(true)
, m_wasChangedSinceLastFormControlChangeEvent(false)
, m_hasAutofocused(false)
{
setForm(form ? form : findFormAncestor());
setHasCustomWillOrDidRecalcStyle();
}
HTMLFormControlElement::~HTMLFormControlElement()
{
}
void HTMLFormControlElement::detach()
{
m_validationMessage = nullptr;
HTMLElement::detach();
}
String HTMLFormControlElement::formEnctype() const
{
return FormSubmission::Attributes::parseEncodingType(fastGetAttribute(formenctypeAttr));
}
void HTMLFormControlElement::setFormEnctype(const String& value)
{
setAttribute(formenctypeAttr, value);
}
String HTMLFormControlElement::formMethod() const
{
return FormSubmission::Attributes::methodString(FormSubmission::Attributes::parseMethodType(fastGetAttribute(formmethodAttr)));
}
void HTMLFormControlElement::setFormMethod(const String& value)
{
setAttribute(formmethodAttr, value);
}
bool HTMLFormControlElement::formNoValidate() const
{
return fastHasAttribute(formnovalidateAttr);
}
void HTMLFormControlElement::updateAncestorDisabledState() const
{
HTMLFieldSetElement* fieldSetAncestor = 0;
ContainerNode* legendAncestor = 0;
for (ContainerNode* ancestor = parentNode(); ancestor; ancestor = ancestor->parentNode()) {
if (!legendAncestor && ancestor->hasTagName(legendTag))
legendAncestor = ancestor;
if (ancestor->hasTagName(fieldsetTag)) {
fieldSetAncestor = static_cast<HTMLFieldSetElement*>(ancestor);
break;
}
}
m_ancestorDisabledState = (fieldSetAncestor && fieldSetAncestor->disabled() && !(legendAncestor && legendAncestor == fieldSetAncestor->legend())) ? AncestorDisabledStateDisabled : AncestorDisabledStateEnabled;
}
void HTMLFormControlElement::ancestorDisabledStateWasChanged()
{
m_ancestorDisabledState = AncestorDisabledStateUnknown;
disabledAttributeChanged();
}
void HTMLFormControlElement::parseAttribute(Attribute* attr)
{
if (attr->name() == formAttr)
formAttributeChanged();
else if (attr->name() == disabledAttr) {
bool oldDisabled = m_disabled;
m_disabled = !attr->isNull();
if (oldDisabled != m_disabled)
disabledAttributeChanged();
} else if (attr->name() == readonlyAttr) {
bool oldReadOnly = m_readOnly;
m_readOnly = !attr->isNull();
if (oldReadOnly != m_readOnly) {
setNeedsWillValidateCheck();
setNeedsStyleRecalc();
if (renderer() && renderer()->style()->hasAppearance())
renderer()->theme()->stateChanged(renderer(), ReadOnlyState);
}
} else if (attr->name() == requiredAttr) {
bool oldRequired = m_required;
m_required = !attr->isNull();
if (oldRequired != m_required)
requiredAttributeChanged();
} else
HTMLElement::parseAttribute(attr);
}
void HTMLFormControlElement::disabledAttributeChanged()
{
setNeedsWillValidateCheck();
setNeedsStyleRecalc();
if (renderer() && renderer()->style()->hasAppearance())
renderer()->theme()->stateChanged(renderer(), EnabledState);
}
void HTMLFormControlElement::requiredAttributeChanged()
{
setNeedsValidityCheck();
setNeedsStyleRecalc();
}
static bool shouldAutofocus(HTMLFormControlElement* element)
{
if (!element->autofocus())
return false;
if (!element->renderer())
return false;
if (element->document()->ignoreAutofocus())
return false;
if (element->document()->isSandboxed(SandboxAutomaticFeatures))
return false;
if (element->hasAutofocused())
return false;
if (element->hasTagName(inputTag))
return !static_cast<HTMLInputElement*>(element)->isInputTypeHidden();
if (element->hasTagName(selectTag))
return true;
if (element->hasTagName(keygenTag))
return true;
if (element->hasTagName(buttonTag))
return true;
if (element->hasTagName(textareaTag))
return true;
return false;
}
static void focusPostAttach(Node* element, unsigned)
{
static_cast<Element*>(element)->focus();
element->deref();
}
void HTMLFormControlElement::attach()
{
ASSERT(!attached());
suspendPostAttachCallbacks();
HTMLElement::attach();
if (renderer())
renderer()->updateFromElement();
if (shouldAutofocus(this)) {
setAutofocused();
ref();
queuePostAttachCallback(focusPostAttach, this);
}
resumePostAttachCallbacks();
}
void HTMLFormControlElement::didMoveToNewDocument(Document* oldDocument)
{
FormAssociatedElement::didMoveToNewDocument(oldDocument);
HTMLElement::didMoveToNewDocument(oldDocument);
}
Node::InsertionNotificationRequest HTMLFormControlElement::insertedInto(Node* insertionPoint)
{
m_ancestorDisabledState = AncestorDisabledStateUnknown;
m_dataListAncestorState = Unknown;
setNeedsWillValidateCheck();
HTMLElement::insertedInto(insertionPoint);
FormAssociatedElement::insertedInto(insertionPoint);
return InsertionDone;
}
void HTMLFormControlElement::removedFrom(Node* insertionPoint)
{
m_ancestorDisabledState = AncestorDisabledStateUnknown;
m_dataListAncestorState = Unknown;
HTMLElement::removedFrom(insertionPoint);
FormAssociatedElement::removedFrom(insertionPoint);
}
const AtomicString& HTMLFormControlElement::formControlName() const
{
const AtomicString& name = getNameAttribute();
return name.isNull() ? emptyAtom : name;
}
void HTMLFormControlElement::setName(const AtomicString& value)
{
setAttribute(nameAttr, value);
}
bool HTMLFormControlElement::wasChangedSinceLastFormControlChangeEvent() const
{
return m_wasChangedSinceLastFormControlChangeEvent;
}
void HTMLFormControlElement::setChangedSinceLastFormControlChangeEvent(bool changed)
{
m_wasChangedSinceLastFormControlChangeEvent = changed;
}
void HTMLFormControlElement::dispatchFormControlChangeEvent()
{
HTMLElement::dispatchChangeEvent();
setChangedSinceLastFormControlChangeEvent(false);
}
void HTMLFormControlElement::dispatchFormControlInputEvent()
{
setChangedSinceLastFormControlChangeEvent(true);
HTMLElement::dispatchInputEvent();
}
bool HTMLFormControlElement::disabled() const
{
if (m_disabled)
return true;
if (m_ancestorDisabledState == AncestorDisabledStateUnknown)
updateAncestorDisabledState();
return m_ancestorDisabledState == AncestorDisabledStateDisabled;
}
void HTMLFormControlElement::setDisabled(bool b)
{
setAttribute(disabledAttr, b ? "" : 0);
}
bool HTMLFormControlElement::autofocus() const
{
return hasAttribute(autofocusAttr);
}
bool HTMLFormControlElement::required() const
{
return m_required;
}
static void updateFromElementCallback(Node* node, unsigned)
{
ASSERT_ARG(node, node->isElementNode());
ASSERT_ARG(node, static_cast<Element*>(node)->isFormControlElement());
ASSERT(node->renderer());
if (RenderObject* renderer = node->renderer())
renderer->updateFromElement();
}
void HTMLFormControlElement::didRecalcStyle(StyleChange)
{
if (renderer())
queuePostAttachCallback(updateFromElementCallback, this);
}
bool HTMLFormControlElement::supportsFocus() const
{
return !m_disabled;
}
bool HTMLFormControlElement::isFocusable() const
{
if (!renderer() || !renderer()->isBox() || toRenderBox(renderer())->size().isEmpty())
return false;
return HTMLElement::isFocusable();
}
bool HTMLFormControlElement::isKeyboardFocusable(KeyboardEvent* event) const
{
if (isFocusable())
if (document()->frame())
return document()->frame()->eventHandler()->tabsToAllFormControls(event);
return false;
}
bool HTMLFormControlElement::isMouseFocusable() const
{
#if PLATFORM(GTK) || PLATFORM(QT)
return HTMLElement::isMouseFocusable();
#else
return false;
#endif
}
short HTMLFormControlElement::tabIndex() const
{
return Element::tabIndex();
}
bool HTMLFormControlElement::recalcWillValidate() const
{
if (m_dataListAncestorState == Unknown) {
for (ContainerNode* ancestor = parentNode(); ancestor; ancestor = ancestor->parentNode()) {
if (ancestor->hasTagName(datalistTag)) {
m_dataListAncestorState = InsideDataList;
break;
}
}
if (m_dataListAncestorState == Unknown)
m_dataListAncestorState = NotInsideDataList;
}
return m_dataListAncestorState == NotInsideDataList && !disabled() && !m_readOnly;
}
bool HTMLFormControlElement::willValidate() const
{
if (!m_willValidateInitialized || m_dataListAncestorState == Unknown) {
m_willValidateInitialized = true;
m_willValidate = recalcWillValidate();
} else {
ASSERT(m_willValidate == recalcWillValidate());
}
return m_willValidate;
}
void HTMLFormControlElement::setNeedsWillValidateCheck()
{
bool newWillValidate = recalcWillValidate();
if (m_willValidateInitialized && m_willValidate == newWillValidate)
return;
m_willValidateInitialized = true;
m_willValidate = newWillValidate;
setNeedsStyleRecalc();
if (!m_willValidate)
hideVisibleValidationMessage();
}
String HTMLFormControlElement::validationMessage()
{
return validity()->validationMessage();
}
void HTMLFormControlElement::updateVisibleValidationMessage()
{
Page* page = document()->page();
if (!page)
return;
String message;
if (renderer() && willValidate()) {
message = validationMessage().stripWhiteSpace();
const AtomicString& title = getAttribute(titleAttr);
if (!message.isEmpty() && !title.isEmpty()) {
message.append('\n');
message.append(title);
}
}
if (message.isEmpty()) {
hideVisibleValidationMessage();
return;
}
if (!m_validationMessage) {
m_validationMessage = ValidationMessage::create(this);
m_validationMessage->setMessage(message);
} else {
m_validationMessage->setMessage(message);
}
}
void HTMLFormControlElement::hideVisibleValidationMessage()
{
if (m_validationMessage)
m_validationMessage->requestToHideMessage();
}
String HTMLFormControlElement::visibleValidationMessage() const
{
return m_validationMessage ? m_validationMessage->message() : String();
}
bool HTMLFormControlElement::checkValidity(Vector<RefPtr<FormAssociatedElement> >* unhandledInvalidControls)
{
if (!willValidate() || isValidFormControlElement())
return true;
RefPtr<HTMLFormControlElement> protector(this);
RefPtr<Document> originalDocument(document());
bool needsDefaultAction = dispatchEvent(Event::create(eventNames().invalidEvent, false, true));
if (needsDefaultAction && unhandledInvalidControls && inDocument() && originalDocument == document())
unhandledInvalidControls->append(this);
return false;
}
bool HTMLFormControlElement::isValidFormControlElement()
{
ASSERT(m_isValid == validity()->valid());
return m_isValid;
}
void HTMLFormControlElement::setNeedsValidityCheck()
{
bool newIsValid = validity()->valid();
if (willValidate() && newIsValid != m_isValid) {
setNeedsStyleRecalc();
}
m_isValid = newIsValid;
if (!visibleValidationMessage().isEmpty()) {
updateVisibleValidationMessage();
}
}
void HTMLFormControlElement::setCustomValidity(const String& error)
{
validity()->setCustomErrorMessage(error);
}
bool HTMLFormControlElement::validationMessageShadowTreeContains(Node* node) const
{
return m_validationMessage && m_validationMessage->shadowTreeContains(node);
}
void HTMLFormControlElement::dispatchBlurEvent(PassRefPtr<Node> newFocusedNode)
{
HTMLElement::dispatchBlurEvent(newFocusedNode);
hideVisibleValidationMessage();
}
HTMLFormElement* HTMLFormControlElement::virtualForm() const
{
return FormAssociatedElement::form();
}
bool HTMLFormControlElement::isDefaultButtonForForm() const
{
return isSuccessfulSubmitButton() && form() && form()->defaultButton() == this;
}
bool HTMLFormControlElement::autocorrect() const
{
if (hasAttribute(autocorrectAttr))
return !equalIgnoringCase(getAttribute(autocorrectAttr), "off");
if (HTMLFormElement* form = this->form())
return form->autocorrect();
return true;
}
void HTMLFormControlElement::setAutocorrect(bool b)
{
setAttribute(autocorrectAttr, b ? "on" : "off");
}
WebAutocapitalizeType HTMLFormControlElement::autocapitalizeType() const
{
const AtomicString& attrValue = getAttribute(autocapitalizeAttr);
WebAutocapitalizeType type = autocapitalizeTypeForAttributeValue(attrValue);
if (type == WebAutocapitalizeTypeDefault) {
if (HTMLFormElement* form = this->form())
return form->autocapitalizeType();
}
return type;
}
const AtomicString& HTMLFormControlElement::autocapitalize() const
{
WebAutocapitalizeType type = autocapitalizeType();
return stringForAutocapitalizeType(type);
}
void HTMLFormControlElement::setAutocapitalize(const AtomicString& value)
{
setAttribute(autocapitalizeAttr, value);
}
HTMLFormControlElement* HTMLFormControlElement::enclosingFormControlElement(Node* node)
{
for (; node; node = node->parentNode()) {
if (node->isElementNode() && toElement(node)->isFormControlElement())
return static_cast<HTMLFormControlElement*>(node);
}
return 0;
}
}