This source file includes following definitions.
- validity
- didMoveToNewDocument
- insertedInto
- removedFrom
- findAssociatedForm
- formRemovedFromTree
- associateByParser
- setForm
- willChangeForm
- didChangeForm
- resetFormOwner
- formAttributeChanged
- customError
- hasBadInput
- patternMismatch
- rangeOverflow
- rangeUnderflow
- stepMismatch
- tooLong
- typeMismatch
- valid
- valueMissing
- customValidationMessage
- validationMessage
- setCustomValidity
- resetFormAttributeTargetObserver
- formAttributeTargetChanged
- name
- isFormControlElementWithState
- toHTMLElement
- toHTMLElement
- toHTMLElement
- toHTMLElement
- create
- m_element
- idTargetChanged
#include "config.h"
#include "core/html/FormAssociatedElement.h"
#include "HTMLNames.h"
#include "core/dom/IdTargetObserver.h"
#include "core/html/HTMLFormControlElement.h"
#include "core/html/HTMLFormElement.h"
#include "core/html/HTMLObjectElement.h"
#include "core/html/ValidityState.h"
namespace WebCore {
using namespace HTMLNames;
class FormAttributeTargetObserver : IdTargetObserver {
WTF_MAKE_FAST_ALLOCATED;
public:
static PassOwnPtr<FormAttributeTargetObserver> create(const AtomicString& id, FormAssociatedElement*);
virtual void idTargetChanged() OVERRIDE;
private:
FormAttributeTargetObserver(const AtomicString& id, FormAssociatedElement*);
FormAssociatedElement* m_element;
};
FormAssociatedElement::FormAssociatedElement()
: m_formWasSetByParser(false)
{
}
FormAssociatedElement::~FormAssociatedElement()
{
}
ValidityState* FormAssociatedElement::validity()
{
if (!m_validityState)
m_validityState = ValidityState::create(this);
return m_validityState.get();
}
void FormAssociatedElement::didMoveToNewDocument(Document& oldDocument)
{
HTMLElement* element = toHTMLElement(this);
if (element->fastHasAttribute(formAttr))
m_formAttributeTargetObserver = nullptr;
}
void FormAssociatedElement::insertedInto(ContainerNode* insertionPoint)
{
if (!m_formWasSetByParser || insertionPoint->highestAncestor() != m_form->highestAncestor())
resetFormOwner();
if (!insertionPoint->inDocument())
return;
HTMLElement* element = toHTMLElement(this);
if (element->fastHasAttribute(formAttr))
resetFormAttributeTargetObserver();
}
void FormAssociatedElement::removedFrom(ContainerNode* insertionPoint)
{
HTMLElement* element = toHTMLElement(this);
if (insertionPoint->inDocument() && element->fastHasAttribute(formAttr))
m_formAttributeTargetObserver = nullptr;
if (m_form && element->highestAncestor() != m_form->highestAncestor())
resetFormOwner();
}
HTMLFormElement* FormAssociatedElement::findAssociatedForm(const HTMLElement* element)
{
const AtomicString& formId(element->fastGetAttribute(formAttr));
if (!formId.isNull() && element->inDocument()) {
Element* newFormCandidate = element->treeScope().getElementById(formId);
return isHTMLFormElement(newFormCandidate) ? toHTMLFormElement(newFormCandidate) : 0;
}
return element->findFormAncestor();
}
void FormAssociatedElement::formRemovedFromTree(const Node& formRoot)
{
ASSERT(m_form);
if (toHTMLElement(this)->highestAncestor() == formRoot)
return;
resetFormOwner();
}
void FormAssociatedElement::associateByParser(HTMLFormElement* form)
{
if (form && form->inDocument()) {
m_formWasSetByParser = true;
setForm(form);
form->didAssociateByParser();
}
}
void FormAssociatedElement::setForm(HTMLFormElement* newForm)
{
if (m_form.get() == newForm)
return;
willChangeForm();
if (m_form)
m_form->disassociate(*this);
if (newForm) {
m_form = newForm->createWeakPtr();
m_form->associate(*this);
} else {
m_form = WeakPtr<HTMLFormElement>();
}
didChangeForm();
}
void FormAssociatedElement::willChangeForm()
{
}
void FormAssociatedElement::didChangeForm()
{
}
void FormAssociatedElement::resetFormOwner()
{
m_formWasSetByParser = false;
HTMLElement* element = toHTMLElement(this);
const AtomicString& formId(element->fastGetAttribute(formAttr));
HTMLFormElement* nearestForm = element->findFormAncestor();
if (m_form && formId.isNull() && m_form.get() == nearestForm)
return;
HTMLFormElement* originalForm = m_form.get();
setForm(findAssociatedForm(element));
if (m_form && m_form.get() != originalForm && m_form->inDocument())
element->document().didAssociateFormControl(element);
}
void FormAssociatedElement::formAttributeChanged()
{
resetFormOwner();
resetFormAttributeTargetObserver();
}
bool FormAssociatedElement::customError() const
{
const HTMLElement* element = toHTMLElement(this);
return element->willValidate() && !m_customValidationMessage.isEmpty();
}
bool FormAssociatedElement::hasBadInput() const
{
return false;
}
bool FormAssociatedElement::patternMismatch() const
{
return false;
}
bool FormAssociatedElement::rangeOverflow() const
{
return false;
}
bool FormAssociatedElement::rangeUnderflow() const
{
return false;
}
bool FormAssociatedElement::stepMismatch() const
{
return false;
}
bool FormAssociatedElement::tooLong() const
{
return false;
}
bool FormAssociatedElement::typeMismatch() const
{
return false;
}
bool FormAssociatedElement::valid() const
{
bool someError = typeMismatch() || stepMismatch() || rangeUnderflow() || rangeOverflow()
|| tooLong() || patternMismatch() || valueMissing() || hasBadInput() || customError();
return !someError;
}
bool FormAssociatedElement::valueMissing() const
{
return false;
}
String FormAssociatedElement::customValidationMessage() const
{
return m_customValidationMessage;
}
String FormAssociatedElement::validationMessage() const
{
return customError() ? m_customValidationMessage : String();
}
void FormAssociatedElement::setCustomValidity(const String& error)
{
m_customValidationMessage = error;
}
void FormAssociatedElement::resetFormAttributeTargetObserver()
{
HTMLElement* element = toHTMLElement(this);
const AtomicString& formId(element->fastGetAttribute(formAttr));
if (!formId.isNull() && element->inDocument())
m_formAttributeTargetObserver = FormAttributeTargetObserver::create(formId, this);
else
m_formAttributeTargetObserver = nullptr;
}
void FormAssociatedElement::formAttributeTargetChanged()
{
resetFormOwner();
}
const AtomicString& FormAssociatedElement::name() const
{
const AtomicString& name = toHTMLElement(this)->getNameAttribute();
return name.isNull() ? emptyAtom : name;
}
bool FormAssociatedElement::isFormControlElementWithState() const
{
return false;
}
const HTMLElement& toHTMLElement(const FormAssociatedElement& associatedElement)
{
if (associatedElement.isFormControlElement())
return toHTMLFormControlElement(associatedElement);
return toHTMLObjectElement(associatedElement);
}
const HTMLElement* toHTMLElement(const FormAssociatedElement* associatedElement)
{
ASSERT(associatedElement);
return &toHTMLElement(*associatedElement);
}
HTMLElement* toHTMLElement(FormAssociatedElement* associatedElement)
{
return const_cast<HTMLElement*>(toHTMLElement(static_cast<const FormAssociatedElement*>(associatedElement)));
}
HTMLElement& toHTMLElement(FormAssociatedElement& associatedElement)
{
return const_cast<HTMLElement&>(toHTMLElement(static_cast<const FormAssociatedElement&>(associatedElement)));
}
PassOwnPtr<FormAttributeTargetObserver> FormAttributeTargetObserver::create(const AtomicString& id, FormAssociatedElement* element)
{
return adoptPtr(new FormAttributeTargetObserver(id, element));
}
FormAttributeTargetObserver::FormAttributeTargetObserver(const AtomicString& id, FormAssociatedElement* element)
: IdTargetObserver(toHTMLElement(element)->treeScope().idTargetObserverRegistry(), id)
, m_element(element)
{
}
void FormAttributeTargetObserver::idTargetChanged()
{
m_element->formAttributeTargetChanged();
}
}