mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-09 03:15:11 +00:00
Bug 1088761 - Support reportValidity() for form controls. r=smaug
From 2c2f7223e9d88f431f3e3fee7384dffbca07d8a9 Mon Sep 17 00:00:00 2001 --HG-- extra : rebase_source : 7daace21a903c55c893f0c6e7b0e0380b6ddca12
This commit is contained in:
parent
e05f13cdbb
commit
1545cb9d2f
@ -104,13 +104,13 @@ FormSubmitObserver.prototype =
|
||||
return;
|
||||
}
|
||||
|
||||
// Insure that this is the FormSubmitObserver associated with the form
|
||||
// Insure that this is the FormSubmitObserver associated with the
|
||||
// element / window this notification is about.
|
||||
if (this._content != aFormElement.ownerDocument.defaultView.top.document.defaultView) {
|
||||
let element = aInvalidElements.queryElementAt(0, Ci.nsISupports);
|
||||
if (this._content != element.ownerDocument.defaultView.top.document.defaultView) {
|
||||
return;
|
||||
}
|
||||
|
||||
let element = aInvalidElements.queryElementAt(0, Ci.nsISupports);
|
||||
if (!(element instanceof HTMLInputElement ||
|
||||
element instanceof HTMLTextAreaElement ||
|
||||
element instanceof HTMLSelectElement ||
|
||||
|
@ -165,6 +165,7 @@ public:
|
||||
// nsIConstraintValidation::GetValidationMessage() is fine.
|
||||
// nsIConstraintValidation::CheckValidity() is fine.
|
||||
using nsIConstraintValidation::CheckValidity;
|
||||
using nsIConstraintValidation::ReportValidity;
|
||||
// nsIConstraintValidation::SetCustomValidity() is fine.
|
||||
|
||||
protected:
|
||||
|
@ -26,6 +26,7 @@ public:
|
||||
using nsGenericHTMLFormElement::GetForm;
|
||||
using nsIConstraintValidation::Validity;
|
||||
using nsIConstraintValidation::CheckValidity;
|
||||
using nsIConstraintValidation::ReportValidity;
|
||||
using nsIConstraintValidation::GetValidationMessage;
|
||||
|
||||
explicit HTMLFieldSetElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo);
|
||||
|
@ -387,6 +387,11 @@ public:
|
||||
return CheckFormValidity(nullptr);
|
||||
}
|
||||
|
||||
bool ReportValidity()
|
||||
{
|
||||
return CheckValidFormSubmission();
|
||||
}
|
||||
|
||||
Element*
|
||||
IndexedGetter(uint32_t aIndex, bool &aFound);
|
||||
|
||||
|
@ -109,6 +109,7 @@ class HTMLInputElement final : public nsGenericHTMLFormElementWithState,
|
||||
public:
|
||||
using nsIConstraintValidation::GetValidationMessage;
|
||||
using nsIConstraintValidation::CheckValidity;
|
||||
using nsIConstraintValidation::ReportValidity;
|
||||
using nsIConstraintValidation::WillValidate;
|
||||
using nsIConstraintValidation::Validity;
|
||||
using nsGenericHTMLFormElementWithState::GetForm;
|
||||
|
@ -156,6 +156,7 @@ public:
|
||||
using nsObjectLoadingContent::GetContentDocument;
|
||||
nsPIDOMWindowOuter* GetContentWindow();
|
||||
using nsIConstraintValidation::CheckValidity;
|
||||
using nsIConstraintValidation::ReportValidity;
|
||||
using nsIConstraintValidation::GetValidationMessage;
|
||||
void GetAlign(DOMString& aValue)
|
||||
{
|
||||
|
@ -267,6 +267,7 @@ public:
|
||||
// nsIConstraintValidation::GetValidationMessage() is fine.
|
||||
// nsIConstraintValidation::CheckValidity() is fine.
|
||||
using nsIConstraintValidation::CheckValidity;
|
||||
using nsIConstraintValidation::ReportValidity;
|
||||
// nsIConstraintValidation::SetCustomValidity() is fine.
|
||||
|
||||
using nsINode::Remove;
|
||||
|
@ -265,6 +265,7 @@ public:
|
||||
// nsIConstraintValidation::GetValidationMessage() is fine.
|
||||
// nsIConstraintValidation::CheckValidity() is fine.
|
||||
using nsIConstraintValidation::CheckValidity;
|
||||
using nsIConstraintValidation::ReportValidity;
|
||||
// nsIConstraintValidation::SetCustomValidity() is fine.
|
||||
// XPCOM Select is fine
|
||||
uint32_t GetSelectionStart(ErrorResult& aError);
|
||||
|
@ -10,10 +10,14 @@
|
||||
#include "nsGenericHTMLElement.h"
|
||||
#include "mozilla/dom/HTMLFormElement.h"
|
||||
#include "mozilla/dom/HTMLFieldSetElement.h"
|
||||
#include "mozilla/dom/HTMLInputElement.h"
|
||||
#include "mozilla/dom/ValidityState.h"
|
||||
#include "nsIFormControl.h"
|
||||
#include "nsContentUtils.h"
|
||||
|
||||
#include "nsIFormSubmitObserver.h"
|
||||
#include "nsIObserverService.h"
|
||||
|
||||
const uint16_t nsIConstraintValidation::sContentSpecifiedMaxLengthMessage = 256;
|
||||
|
||||
using namespace mozilla;
|
||||
@ -126,6 +130,71 @@ nsIConstraintValidation::CheckValidity(bool* aValidity)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
bool
|
||||
nsIConstraintValidation::ReportValidity()
|
||||
{
|
||||
if (!IsCandidateForConstraintValidation() || IsValid()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIContent> content = do_QueryInterface(this);
|
||||
MOZ_ASSERT(content, "This class should be inherited by HTML elements only!");
|
||||
|
||||
bool defaultAction = true;
|
||||
nsContentUtils::DispatchTrustedEvent(content->OwnerDoc(), content,
|
||||
NS_LITERAL_STRING("invalid"),
|
||||
false, true, &defaultAction);
|
||||
if (!defaultAction) {
|
||||
return false;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIObserverService> service =
|
||||
mozilla::services::GetObserverService();
|
||||
if (!service) {
|
||||
NS_WARNING("No observer service available!");
|
||||
return true;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsISimpleEnumerator> theEnum;
|
||||
nsresult rv = service->EnumerateObservers(NS_INVALIDFORMSUBMIT_SUBJECT,
|
||||
getter_AddRefs(theEnum));
|
||||
|
||||
// Return true on error here because that's what we always did
|
||||
NS_ENSURE_SUCCESS(rv, true);
|
||||
|
||||
bool hasObserver = false;
|
||||
rv = theEnum->HasMoreElements(&hasObserver);
|
||||
|
||||
nsCOMPtr<nsIMutableArray> invalidElements =
|
||||
do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
|
||||
invalidElements->AppendElement(content, false);
|
||||
|
||||
NS_ENSURE_SUCCESS(rv, true);
|
||||
nsCOMPtr<nsISupports> inst;
|
||||
nsCOMPtr<nsIFormSubmitObserver> observer;
|
||||
bool more = true;
|
||||
while (NS_SUCCEEDED(theEnum->HasMoreElements(&more)) && more) {
|
||||
theEnum->GetNext(getter_AddRefs(inst));
|
||||
observer = do_QueryInterface(inst);
|
||||
|
||||
if (observer) {
|
||||
observer->NotifyInvalidSubmit(nullptr, invalidElements);
|
||||
}
|
||||
}
|
||||
|
||||
if (content->IsHTMLElement(nsGkAtoms::input) &&
|
||||
nsContentUtils::IsFocusedContent(content)) {
|
||||
HTMLInputElement* inputElement =
|
||||
HTMLInputElement::FromContentOrNull(content);
|
||||
|
||||
inputElement->UpdateValidityUIBits(true);
|
||||
}
|
||||
|
||||
dom::Element* element = content->AsElement();
|
||||
element->UpdateState(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
nsIConstraintValidation::SetValidityState(ValidityStateType aState,
|
||||
bool aValue)
|
||||
|
@ -73,6 +73,7 @@ public:
|
||||
}
|
||||
mozilla::dom::ValidityState* Validity();
|
||||
bool CheckValidity();
|
||||
bool ReportValidity();
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -41,6 +41,7 @@ interface HTMLButtonElement : HTMLElement {
|
||||
readonly attribute ValidityState validity;
|
||||
readonly attribute DOMString validationMessage;
|
||||
boolean checkValidity();
|
||||
boolean reportValidity();
|
||||
void setCustomValidity(DOMString error);
|
||||
|
||||
// Not yet implemented:
|
||||
|
@ -27,6 +27,7 @@ interface HTMLFieldSetElement : HTMLElement {
|
||||
readonly attribute DOMString validationMessage;
|
||||
|
||||
boolean checkValidity();
|
||||
boolean reportValidity();
|
||||
|
||||
void setCustomValidity(DOMString error);
|
||||
};
|
||||
|
@ -47,6 +47,7 @@ interface HTMLFormElement : HTMLElement {
|
||||
void submit();
|
||||
void reset();
|
||||
boolean checkValidity();
|
||||
boolean reportValidity();
|
||||
|
||||
[Pref="dom.forms.requestAutocomplete"]
|
||||
void requestAutocomplete();
|
||||
|
@ -106,6 +106,7 @@ interface HTMLInputElement : HTMLElement {
|
||||
[GetterThrows]
|
||||
readonly attribute DOMString validationMessage;
|
||||
boolean checkValidity();
|
||||
boolean reportValidity();
|
||||
void setCustomValidity(DOMString error);
|
||||
|
||||
// Bug 850365 readonly attribute NodeList labels;
|
||||
|
@ -40,6 +40,7 @@ interface HTMLObjectElement : HTMLElement {
|
||||
readonly attribute ValidityState validity;
|
||||
readonly attribute DOMString validationMessage;
|
||||
boolean checkValidity();
|
||||
boolean reportValidity();
|
||||
void setCustomValidity(DOMString error);
|
||||
|
||||
[Throws]
|
||||
|
@ -30,6 +30,7 @@ interface HTMLOutputElement : HTMLElement {
|
||||
readonly attribute ValidityState validity;
|
||||
readonly attribute DOMString validationMessage;
|
||||
boolean checkValidity();
|
||||
boolean reportValidity();
|
||||
void setCustomValidity(DOMString error);
|
||||
|
||||
// Not yet implemented (bug 556743).
|
||||
|
@ -50,6 +50,7 @@ interface HTMLSelectElement : HTMLElement {
|
||||
readonly attribute ValidityState validity;
|
||||
readonly attribute DOMString validationMessage;
|
||||
boolean checkValidity();
|
||||
boolean reportValidity();
|
||||
void setCustomValidity(DOMString error);
|
||||
|
||||
// NYI: readonly attribute NodeList labels;
|
||||
|
@ -52,6 +52,7 @@ interface HTMLTextAreaElement : HTMLElement {
|
||||
readonly attribute ValidityState validity;
|
||||
readonly attribute DOMString validationMessage;
|
||||
boolean checkValidity();
|
||||
boolean reportValidity();
|
||||
void setCustomValidity(DOMString error);
|
||||
|
||||
// readonly attribute NodeList labels;
|
||||
|
@ -5322,15 +5322,15 @@ var FormAssistant = {
|
||||
if (!aInvalidElements.length)
|
||||
return;
|
||||
|
||||
// Ignore this notificaiton if the current tab doesn't contain the invalid form
|
||||
// Ignore this notificaiton if the current tab doesn't contain the invalid element
|
||||
let currentElement = aInvalidElements.queryElementAt(0, Ci.nsISupports);
|
||||
if (BrowserApp.selectedBrowser.contentDocument !=
|
||||
aFormElement.ownerDocument.defaultView.top.document)
|
||||
currentElement.ownerDocument.defaultView.top.document)
|
||||
return;
|
||||
|
||||
this._invalidSubmit = true;
|
||||
|
||||
// Our focus listener will show the element's validation message
|
||||
let currentElement = aInvalidElements.queryElementAt(0, Ci.nsISupports);
|
||||
currentElement.focus();
|
||||
},
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user