2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
26 #include "core/html/FormAssociatedElement.h"
28 #include "core/HTMLNames.h"
29 #include "core/dom/IdTargetObserver.h"
30 #include "core/dom/NodeTraversal.h"
31 #include "core/html/HTMLFormControlElement.h"
32 #include "core/html/HTMLFormElement.h"
33 #include "core/html/HTMLLabelElement.h"
34 #include "core/html/HTMLObjectElement.h"
35 #include "core/html/ValidityState.h"
39 using namespace HTMLNames;
41 class FormAttributeTargetObserver : public IdTargetObserver {
42 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
44 static PassOwnPtrWillBeRawPtr<FormAttributeTargetObserver> create(const AtomicString& id, FormAssociatedElement*);
45 virtual void trace(Visitor*) override;
46 virtual void idTargetChanged() override;
49 FormAttributeTargetObserver(const AtomicString& id, FormAssociatedElement*);
51 RawPtrWillBeMember<FormAssociatedElement> m_element;
54 FormAssociatedElement::FormAssociatedElement()
55 : m_formWasSetByParser(false)
59 FormAssociatedElement::~FormAssociatedElement()
61 // We can't call setForm here because it contains virtual calls.
64 void FormAssociatedElement::trace(Visitor* visitor)
66 visitor->trace(m_formAttributeTargetObserver);
67 visitor->trace(m_form);
68 visitor->trace(m_validityState);
71 ValidityState* FormAssociatedElement::validity()
74 m_validityState = ValidityState::create(this);
76 return m_validityState.get();
79 void FormAssociatedElement::didMoveToNewDocument(Document& oldDocument)
81 HTMLElement* element = toHTMLElement(this);
82 if (element->fastHasAttribute(formAttr))
83 setFormAttributeTargetObserver(nullptr);
86 void FormAssociatedElement::insertedInto(ContainerNode* insertionPoint)
88 if (!m_formWasSetByParser || !m_form || NodeTraversal::highestAncestorOrSelf(*insertionPoint) != NodeTraversal::highestAncestorOrSelf(*m_form.get()))
91 if (!insertionPoint->inDocument())
94 HTMLElement* element = toHTMLElement(this);
95 if (element->fastHasAttribute(formAttr))
96 resetFormAttributeTargetObserver();
99 void FormAssociatedElement::removedFrom(ContainerNode* insertionPoint)
101 HTMLElement* element = toHTMLElement(this);
102 if (insertionPoint->inDocument() && element->fastHasAttribute(formAttr))
103 setFormAttributeTargetObserver(nullptr);
104 // If the form and element are both in the same tree, preserve the connection to the form.
105 // Otherwise, null out our form and remove ourselves from the form's list of elements.
106 if (m_form && NodeTraversal::highestAncestorOrSelf(*element) != NodeTraversal::highestAncestorOrSelf(*m_form.get()))
110 HTMLFormElement* FormAssociatedElement::findAssociatedForm(const HTMLElement* element)
112 const AtomicString& formId(element->fastGetAttribute(formAttr));
113 // 3. If the element is reassociateable, has a form content attribute, and
114 // is itself in a Document, then run these substeps:
115 if (!formId.isNull() && element->inDocument()) {
116 // 3.1. If the first element in the Document to have an ID that is
117 // case-sensitively equal to the element's form content attribute's
118 // value is a form element, then associate the form-associated element
119 // with that form element.
120 // 3.2. Abort the "reset the form owner" steps.
121 Element* newFormCandidate = element->treeScope().getElementById(formId);
122 return isHTMLFormElement(newFormCandidate) ? toHTMLFormElement(newFormCandidate) : 0;
124 // 4. Otherwise, if the form-associated element in question has an ancestor
125 // form element, then associate the form-associated element with the nearest
126 // such ancestor form element.
127 return element->findFormAncestor();
130 void FormAssociatedElement::formRemovedFromTree(const Node& formRoot)
133 if (NodeTraversal::highestAncestorOrSelf(toHTMLElement(*this)) == formRoot)
138 void FormAssociatedElement::associateByParser(HTMLFormElement* form)
140 if (form && form->inDocument()) {
141 m_formWasSetByParser = true;
143 form->didAssociateByParser();
147 void FormAssociatedElement::setForm(HTMLFormElement* newForm)
149 if (m_form.get() == newForm)
153 m_form->disassociate(*this);
158 m_form = newForm->createWeakPtr();
160 m_form->associate(*this);
165 m_form = WeakPtr<HTMLFormElement>();
171 void FormAssociatedElement::willChangeForm()
175 void FormAssociatedElement::didChangeForm()
179 void FormAssociatedElement::resetFormOwner()
181 m_formWasSetByParser = false;
182 HTMLElement* element = toHTMLElement(this);
183 const AtomicString& formId(element->fastGetAttribute(formAttr));
184 HTMLFormElement* nearestForm = element->findFormAncestor();
185 // 1. If the element's form owner is not null, and either the element is not
186 // reassociateable or its form content attribute is not present, and the
187 // element's form owner is its nearest form element ancestor after the
188 // change to the ancestor chain, then do nothing, and abort these steps.
189 if (m_form && formId.isNull() && m_form.get() == nearestForm)
192 HTMLFormElement* originalForm = m_form.get();
193 setForm(findAssociatedForm(element));
194 // FIXME: Move didAssociateFormControl call to didChangeForm or
195 // HTMLFormElement::associate.
196 if (m_form && m_form.get() != originalForm && m_form->inDocument())
197 element->document().didAssociateFormControl(element);
200 void FormAssociatedElement::formAttributeChanged()
203 resetFormAttributeTargetObserver();
206 bool FormAssociatedElement::customError() const
208 const HTMLElement* element = toHTMLElement(this);
209 return element->willValidate() && !m_customValidationMessage.isEmpty();
212 bool FormAssociatedElement::hasBadInput() const
217 bool FormAssociatedElement::patternMismatch() const
222 bool FormAssociatedElement::rangeOverflow() const
227 bool FormAssociatedElement::rangeUnderflow() const
232 bool FormAssociatedElement::stepMismatch() const
237 bool FormAssociatedElement::tooLong() const
242 bool FormAssociatedElement::tooShort() const
247 bool FormAssociatedElement::typeMismatch() const
252 bool FormAssociatedElement::valid() const
254 bool someError = typeMismatch() || stepMismatch() || rangeUnderflow() || rangeOverflow()
255 || tooLong() || tooShort() || patternMismatch() || valueMissing() || hasBadInput()
260 bool FormAssociatedElement::valueMissing() const
265 String FormAssociatedElement::customValidationMessage() const
267 return m_customValidationMessage;
270 String FormAssociatedElement::validationMessage() const
272 return customError() ? m_customValidationMessage : String();
275 void FormAssociatedElement::setCustomValidity(const String& error)
277 m_customValidationMessage = error;
280 void FormAssociatedElement::setFormAttributeTargetObserver(PassOwnPtrWillBeRawPtr<FormAttributeTargetObserver> newObserver)
282 if (m_formAttributeTargetObserver)
283 m_formAttributeTargetObserver->unregister();
284 m_formAttributeTargetObserver = newObserver;
287 void FormAssociatedElement::resetFormAttributeTargetObserver()
289 HTMLElement* element = toHTMLElement(this);
290 const AtomicString& formId(element->fastGetAttribute(formAttr));
291 if (!formId.isNull() && element->inDocument())
292 setFormAttributeTargetObserver(FormAttributeTargetObserver::create(formId, this));
294 setFormAttributeTargetObserver(nullptr);
297 void FormAssociatedElement::formAttributeTargetChanged()
302 const AtomicString& FormAssociatedElement::name() const
304 const AtomicString& name = toHTMLElement(this)->getNameAttribute();
305 return name.isNull() ? emptyAtom : name;
308 bool FormAssociatedElement::isFormControlElementWithState() const
313 const HTMLElement& toHTMLElement(const FormAssociatedElement& associatedElement)
315 if (associatedElement.isFormControlElement())
316 return toHTMLFormControlElement(associatedElement);
317 else if (associatedElement.isLabelElement())
318 return toHTMLLabelElement(associatedElement);
320 return toHTMLObjectElement(associatedElement);
323 const HTMLElement* toHTMLElement(const FormAssociatedElement* associatedElement)
325 ASSERT(associatedElement);
326 return &toHTMLElement(*associatedElement);
329 HTMLElement* toHTMLElement(FormAssociatedElement* associatedElement)
331 return const_cast<HTMLElement*>(toHTMLElement(static_cast<const FormAssociatedElement*>(associatedElement)));
334 HTMLElement& toHTMLElement(FormAssociatedElement& associatedElement)
336 return const_cast<HTMLElement&>(toHTMLElement(static_cast<const FormAssociatedElement&>(associatedElement)));
339 PassOwnPtrWillBeRawPtr<FormAttributeTargetObserver> FormAttributeTargetObserver::create(const AtomicString& id, FormAssociatedElement* element)
341 return adoptPtrWillBeNoop(new FormAttributeTargetObserver(id, element));
344 FormAttributeTargetObserver::FormAttributeTargetObserver(const AtomicString& id, FormAssociatedElement* element)
345 : IdTargetObserver(toHTMLElement(element)->treeScope().idTargetObserverRegistry(), id)
350 void FormAttributeTargetObserver::trace(Visitor* visitor)
352 visitor->trace(m_element);
353 IdTargetObserver::trace(visitor);
356 void FormAttributeTargetObserver::idTargetChanged()
358 m_element->formAttributeTargetChanged();