Bug 1088761 - Support reportValidity() for form controls. r=smaug

--HG--
extra : rebase_source : 56206e694b413ad1afdd7ded5dd12ef3a0d296f1
This commit is contained in:
John Dai 2016-05-11 04:24:00 +02:00
Родитель b0ac1b27cb
Коммит def16ce1d5
19 изменённых файлов: 95 добавлений и 6 удалений

Просмотреть файл

@ -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;

Просмотреть файл

@ -5299,15 +5299,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();
},