1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Handles the validation callback from nsIFormFillController and
* the display of the help panel on invalid elements.
*/
import { LayoutUtils } from "resource://gre/modules/LayoutUtils.sys.mjs";
export class FormValidationChild extends JSWindowActorChild {
constructor() {
super();
this._validationMessage = "";
this._element = null;
}
/*
* Events
*/
handleEvent(aEvent) {
switch (aEvent.type) {
case "MozInvalidForm":
aEvent.preventDefault();
this.notifyInvalidSubmit(aEvent.detail);
break;
case "pageshow":
if (this._isRootDocumentEvent(aEvent)) {
this._hidePopup();
}
break;
case "pagehide":
// Act as if the element is being blurred. This will remove any
// listeners and hide the popup.
this._onBlur();
break;
case "input":
this._onInput(aEvent);
break;
case "blur":
this._onBlur(aEvent);
break;
}
}
notifyInvalidSubmit(aInvalidElements) {
// Show a validation message on the first focusable element.
for (let element of aInvalidElements) {
// Insure that this is the FormSubmitObserver associated with the
// element / window this notification is about.
if (this.contentWindow != element.ownerGlobal.document.defaultView) {
return;
}
if (
!(
ChromeUtils.getClassName(element) === "HTMLInputElement" ||
ChromeUtils.getClassName(element) === "HTMLTextAreaElement" ||
ChromeUtils.getClassName(element) === "HTMLSelectElement" ||
ChromeUtils.getClassName(element) === "HTMLButtonElement" ||
element.isFormAssociatedCustomElements
)
) {
continue;
}
let validationMessage = element.isFormAssociatedCustomElements
? element.internals.validationMessage
: element.validationMessage;
if (element.isFormAssociatedCustomElements) {
// For element that are form-associated custom elements, user agents
// should use their validation anchor instead.
// It is not clear how constraint validation should work for FACE in
// spec if the validation anchor is null, see
// https://github.com/whatwg/html/issues/10155. Blink seems fallback to
// FACE itself when validation anchor is null, which looks reasonable.
element = element.internals.validationAnchor || element;
}
if (!element || !Services.focus.elementIsFocusable(element, 0)) {
continue;
}
// Update validation message before showing notification
this._validationMessage = validationMessage;
// Don't connect up to the same element more than once.
if (this._element == element) {
this._showPopup(element);
break;
}
this._element = element;
element.focus();
// Watch for input changes which may change the validation message.
element.addEventListener("input", this);
// Watch for focus changes so we can disconnect our listeners and
// hide the popup.
element.addEventListener("blur", this);
this._showPopup(element);
break;
}
}
/*
* Internal
*/
/*
* Handles input changes on the form element we've associated a popup
* with. Updates the validation message or closes the popup if form data
* becomes valid.
*/
_onInput(aEvent) {
let element = aEvent.originalTarget;
// If the form input is now valid, hide the popup.
if (element.validity.valid) {
this._hidePopup();
return;
}
// If the element is still invalid for a new reason, we should update
// the popup error message.
if (this._validationMessage != element.validationMessage) {
this._validationMessage = element.validationMessage;
this._showPopup(element);
}
}
/*
* Blur event handler in which we disconnect from the form element and
* hide the popup.
*/
_onBlur(aEvent) {
if (this._element) {
this._element.removeEventListener("input", this);
this._element.removeEventListener("blur", this);
}
this._hidePopup();
this._element = null;
}
/*
* Send the show popup message to chrome with appropriate position
* information. Can be called repetitively to update the currently
* displayed popup position and text.
*/
_showPopup(aElement) {
// Collect positional information and show the popup
let panelData = {};
panelData.message = this._validationMessage;
panelData.screenRect = LayoutUtils.getElementBoundingScreenRect(aElement);
// We want to show the popup at the middle of checkbox and radio buttons
// and where the content begin for the other elements.
if (
aElement.tagName == "INPUT" &&
(aElement.type == "radio" || aElement.type == "checkbox")
) {
panelData.position = "bottomcenter topleft";
} else {
panelData.position = "after_start";
}
this.sendAsyncMessage("FormValidation:ShowPopup", panelData);
aElement.ownerGlobal.addEventListener("pagehide", this, {
mozSystemGroup: true,
});
}
_hidePopup() {
this.sendAsyncMessage("FormValidation:HidePopup", {});
this._element.ownerGlobal.removeEventListener("pagehide", this, {
mozSystemGroup: true,
});
}
_isRootDocumentEvent(aEvent) {
if (this.contentWindow == null) {
return true;
}
let target = aEvent.originalTarget;
return (
target == this.document ||
(target.ownerDocument && target.ownerDocument == this.document)
);
}
}
|