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/HTMLObjectElement.h"
34 #include "core/html/ValidityState.h"
38 using namespace HTMLNames;
40 class FormAttributeTargetObserver : public IdTargetObserver {
41 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
43 static PassOwnPtrWillBeRawPtr<FormAttributeTargetObserver> create(const AtomicString& id, FormAssociatedElement*);
44 virtual void trace(Visitor*) OVERRIDE;
45 virtual void idTargetChanged() OVERRIDE;
48 FormAttributeTargetObserver(const AtomicString& id, FormAssociatedElement*);
50 RawPtrWillBeMember<FormAssociatedElement> m_element;
53 FormAssociatedElement::FormAssociatedElement()
54 : m_formWasSetByParser(false)
58 FormAssociatedElement::~FormAssociatedElement()
60 // We can't call setForm here because it contains virtual calls.
63 void FormAssociatedElement::trace(Visitor* visitor)
65 visitor->trace(m_formAttributeTargetObserver);
66 visitor->trace(m_form);
67 visitor->trace(m_validityState);
70 ValidityState* FormAssociatedElement::validity()
73 m_validityState = ValidityState::create(this);
75 return m_validityState.get();
78 void FormAssociatedElement::didMoveToNewDocument(Document& oldDocument)
80 HTMLElement* element = toHTMLElement(this);
81 if (element->fastHasAttribute(formAttr))
82 setFormAttributeTargetObserver(nullptr);
85 void FormAssociatedElement::insertedInto(ContainerNode* insertionPoint)
87 if (!m_formWasSetByParser || NodeTraversal::highestAncestorOrSelf(*insertionPoint) != NodeTraversal::highestAncestorOrSelf(*m_form.get()))
90 if (!insertionPoint->inDocument())
93 HTMLElement* element = toHTMLElement(this);
94 if (element->fastHasAttribute(formAttr))
95 resetFormAttributeTargetObserver();
98 void FormAssociatedElement::removedFrom(ContainerNode* insertionPoint)
100 HTMLElement* element = toHTMLElement(this);
101 if (insertionPoint->inDocument() && element->fastHasAttribute(formAttr))
102 setFormAttributeTargetObserver(nullptr);
103 // If the form and element are both in the same tree, preserve the connection to the form.
104 // Otherwise, null out our form and remove ourselves from the form's list of elements.
105 if (m_form && NodeTraversal::highestAncestorOrSelf(*element) != NodeTraversal::highestAncestorOrSelf(*m_form.get()))
109 HTMLFormElement* FormAssociatedElement::findAssociatedForm(const HTMLElement* element)
111 const AtomicString& formId(element->fastGetAttribute(formAttr));
112 // 3. If the element is reassociateable, has a form content attribute, and
113 // is itself in a Document, then run these substeps:
114 if (!formId.isNull() && element->inDocument()) {
115 // 3.1. If the first element in the Document to have an ID that is
116 // case-sensitively equal to the element's form content attribute's
117 // value is a form element, then associate the form-associated element
118 // with that form element.
119 // 3.2. Abort the "reset the form owner" steps.
120 Element* newFormCandidate = element->treeScope().getElementById(formId);
121 return isHTMLFormElement(newFormCandidate) ? toHTMLFormElement(newFormCandidate) : 0;
123 // 4. Otherwise, if the form-associated element in question has an ancestor
124 // form element, then associate the form-associated element with the nearest
125 // such ancestor form element.
126 return element->findFormAncestor();
129 void FormAssociatedElement::formRemovedFromTree(const Node& formRoot)
132 if (NodeTraversal::highestAncestorOrSelf(toHTMLElement(*this)) == formRoot)
137 void FormAssociatedElement::associateByParser(HTMLFormElement* form)
139 if (form && form->inDocument()) {
140 m_formWasSetByParser = true;
142 form->didAssociateByParser();
146 void FormAssociatedElement::setForm(HTMLFormElement* newForm)
148 if (m_form.get() == newForm)
152 m_form->disassociate(*this);
157 m_form = newForm->createWeakPtr();
159 m_form->associate(*this);
164 m_form = WeakPtr<HTMLFormElement>();
170 void FormAssociatedElement::willChangeForm()
174 void FormAssociatedElement::didChangeForm()
178 void FormAssociatedElement::resetFormOwner()
180 m_formWasSetByParser = false;
181 HTMLElement* element = toHTMLElement(this);
182 const AtomicString& formId(element->fastGetAttribute(formAttr));
183 HTMLFormElement* nearestForm = element->findFormAncestor();
184 // 1. If the element's form owner is not null, and either the element is not
185 // reassociateable or its form content attribute is not present, and the
186 // element's form owner is its nearest form element ancestor after the
187 // change to the ancestor chain, then do nothing, and abort these steps.
188 if (m_form && formId.isNull() && m_form.get() == nearestForm)
191 HTMLFormElement* originalForm = m_form.get();
192 setForm(findAssociatedForm(element));
193 // FIXME: Move didAssociateFormControl call to didChangeForm or
194 // HTMLFormElement::associate.
195 if (m_form && m_form.get() != originalForm && m_form->inDocument())
196 element->document().didAssociateFormControl(element);
199 void FormAssociatedElement::formAttributeChanged()
202 resetFormAttributeTargetObserver();
205 bool FormAssociatedElement::customError() const
207 const HTMLElement* element = toHTMLElement(this);
208 return element->willValidate() && !m_customValidationMessage.isEmpty();
211 bool FormAssociatedElement::hasBadInput() const
216 bool FormAssociatedElement::patternMismatch() const
221 bool FormAssociatedElement::rangeOverflow() const
226 bool FormAssociatedElement::rangeUnderflow() const
231 bool FormAssociatedElement::stepMismatch() const
236 bool FormAssociatedElement::tooLong() const
241 bool FormAssociatedElement::typeMismatch() const
246 bool FormAssociatedElement::valid() const
248 bool someError = typeMismatch() || stepMismatch() || rangeUnderflow() || rangeOverflow()
249 || tooLong() || patternMismatch() || valueMissing() || hasBadInput() || customError();
253 bool FormAssociatedElement::valueMissing() const
258 String FormAssociatedElement::customValidationMessage() const
260 return m_customValidationMessage;
263 String FormAssociatedElement::validationMessage() const
265 return customError() ? m_customValidationMessage : String();
268 void FormAssociatedElement::setCustomValidity(const String& error)
270 m_customValidationMessage = error;
273 void FormAssociatedElement::setFormAttributeTargetObserver(PassOwnPtrWillBeRawPtr<FormAttributeTargetObserver> newObserver)
275 if (m_formAttributeTargetObserver)
276 m_formAttributeTargetObserver->unregister();
277 m_formAttributeTargetObserver = newObserver;
280 void FormAssociatedElement::resetFormAttributeTargetObserver()
282 HTMLElement* element = toHTMLElement(this);
283 const AtomicString& formId(element->fastGetAttribute(formAttr));
284 if (!formId.isNull() && element->inDocument())
285 setFormAttributeTargetObserver(FormAttributeTargetObserver::create(formId, this));
287 setFormAttributeTargetObserver(nullptr);
290 void FormAssociatedElement::formAttributeTargetChanged()
295 const AtomicString& FormAssociatedElement::name() const
297 const AtomicString& name = toHTMLElement(this)->getNameAttribute();
298 return name.isNull() ? emptyAtom : name;
301 bool FormAssociatedElement::isFormControlElementWithState() const
306 const HTMLElement& toHTMLElement(const FormAssociatedElement& associatedElement)
308 if (associatedElement.isFormControlElement())
309 return toHTMLFormControlElement(associatedElement);
310 // Assumes the element is an HTMLObjectElement
311 return toHTMLObjectElement(associatedElement);
314 const HTMLElement* toHTMLElement(const FormAssociatedElement* associatedElement)
316 ASSERT(associatedElement);
317 return &toHTMLElement(*associatedElement);
320 HTMLElement* toHTMLElement(FormAssociatedElement* associatedElement)
322 return const_cast<HTMLElement*>(toHTMLElement(static_cast<const FormAssociatedElement*>(associatedElement)));
325 HTMLElement& toHTMLElement(FormAssociatedElement& associatedElement)
327 return const_cast<HTMLElement&>(toHTMLElement(static_cast<const FormAssociatedElement&>(associatedElement)));
330 PassOwnPtrWillBeRawPtr<FormAttributeTargetObserver> FormAttributeTargetObserver::create(const AtomicString& id, FormAssociatedElement* element)
332 return adoptPtrWillBeNoop(new FormAttributeTargetObserver(id, element));
335 FormAttributeTargetObserver::FormAttributeTargetObserver(const AtomicString& id, FormAssociatedElement* element)
336 : IdTargetObserver(toHTMLElement(element)->treeScope().idTargetObserverRegistry(), id)
341 void FormAttributeTargetObserver::trace(Visitor* visitor)
343 visitor->trace(m_element);
344 IdTargetObserver::trace(visitor);
347 void FormAttributeTargetObserver::idTargetChanged()
349 m_element->formAttributeTargetChanged();
352 } // namespace Webcore