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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=345512
-->
<head>
<title>Test for Bug 345512</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<style>
input { background-color: rgb(0,0,0) !important; }
input:valid { background-color: rgb(0,255,0) !important; }
input:invalid { background-color: rgb(255,0,0) !important; }
</style>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=345512">Mozilla Bug 345512</a>
<p id="display"></p>
<div id="content" style="display: none">
<input id='i' pattern="tulip" oninvalid="invalidEventHandler(event);">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 345512 **/
var gInvalid = false;
function invalidEventHandler(e)
{
is(e.type, "invalid", "Invalid event type should be invalid");
gInvalid = true;
}
function completeValidityCheck(element, alwaysValid, isBarred)
{
// Check when pattern matches.
if (element.type == 'email') {
element.pattern = ".*@bar.com";
element.value = "foo@bar.com";
} else if (element.type == 'url') {
element.pattern = "http://.*\\.com$";
element.value = "http://mozilla.com";
} else if (element.type == 'file') {
element.pattern = "foo";
SpecialPowers.wrap(element).mozSetFileArray([new File(["foo"], "foo")]);
} else {
element.pattern = "foo";
element.value = "foo";
}
checkValidPattern(element, true, isBarred);
// Check when pattern does not match.
if (element.type == 'email') {
element.pattern = ".*@bar.com";
element.value = "foo@foo.com";
} else if (element.type == 'url') {
element.pattern = "http://.*\\.com$";
element.value = "http://mozilla.org";
} else if (element.type == 'file') {
element.pattern = "foo";
SpecialPowers.wrap(element).mozSetFileArray([new File(["bar"], "bar")]);
} else {
element.pattern = "foo";
element.value = "bar";
}
if (!alwaysValid) {
checkInvalidPattern(element, true);
} else {
checkValidPattern(element, true, isBarred);
}
}
function checkValidPattern(element, completeCheck, isBarred)
{
if (completeCheck) {
gInvalid = false;
ok(!element.validity.patternMismatch,
"Element should not suffer from pattern mismatch");
ok(element.validity.valid, "Element should be valid");
ok(element.checkValidity(), "Element should be valid");
ok(!gInvalid, "Invalid event shouldn't have been thrown");
is(element.validationMessage, '',
"Validation message should be the empty string");
if (element.type != 'radio' && element.type != 'checkbox') {
is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
isBarred ? "rgb(0, 0, 0)" : "rgb(0, 255, 0)",
"The pseudo-class is not correctly applied");
}
} else {
ok(!element.validity.patternMismatch,
"Element should not suffer from pattern mismatch");
}
}
function checkInvalidPattern(element, completeCheck)
{
if (completeCheck) {
gInvalid = false;
ok(element.validity.patternMismatch,
"Element should suffer from pattern mismatch");
ok(!element.validity.valid, "Element should not be valid");
ok(!element.checkValidity(), "Element should not be valid");
ok(gInvalid, "Invalid event should have been thrown");
is(element.validationMessage,
"Please match the requested format.",
"Validation message is not valid");
} else {
ok(element.validity.patternMismatch,
"Element should suffer from pattern mismatch");
}
if (element.type != 'radio' && element.type != 'checkbox') {
is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
"rgb(255, 0, 0)", ":invalid pseudo-class should apply");
}
}
function checkSyntaxError(element)
{
ok(!element.validity.patternMismatch,
"On SyntaxError, element should not suffer");
}
function checkPatternValidity(element)
{
element.pattern = "foo";
element.value = '';
checkValidPattern(element);
element.value = "foo";
checkValidPattern(element);
element.value = "bar";
checkInvalidPattern(element);
element.value = "foobar";
checkInvalidPattern(element);
element.value = "foofoo";
checkInvalidPattern(element);
element.pattern = "foo\"bar";
element.value = "foo\"bar";
checkValidPattern(element);
element.value = 'foo"bar';
checkValidPattern(element);
element.pattern = "foo'bar";
element.value = "foo\'bar";
checkValidPattern(element);
element.pattern = "foo\\(bar";
element.value = "foo(bar";
checkValidPattern(element);
element.value = "foo";
checkInvalidPattern(element);
element.pattern = "foo\\)bar";
element.value = "foo)bar";
checkValidPattern(element);
element.value = "foo";
checkInvalidPattern(element);
// Check for 'i' flag disabled. Should be case sensitive.
element.value = "Foo";
checkInvalidPattern(element);
// We can't check for the 'g' flag because we only test, we don't execute.
// We can't check for the 'm' flag because .value shouldn't contain line breaks.
// We need '\\\\' because '\\' will produce '\\' and we want to escape the '\'
// for the regexp.
element.pattern = "foo\\\\bar";
element.value = "foo\\bar";
checkValidPattern(element);
// We may want to escape the ' in the pattern, but this is a SyntaxError
// when unicode flag is set.
element.pattern = "foo\\'bar";
element.value = "foo'bar";
checkSyntaxError(element);
element.value = "baz";
checkSyntaxError(element);
// We should check the pattern attribute do not pollute |RegExp.lastParen|.
is(RegExp.lastParen, "", "RegExp.lastParen should be the empty string");
element.pattern = "(foo)";
element.value = "foo";
checkValidPattern(element);
is(RegExp.lastParen, "", "RegExp.lastParen should be the empty string");
// That may sound weird but the empty string is a valid pattern value.
element.pattern = "";
element.value = "";
checkValidPattern(element);
element.value = "foo";
checkInvalidPattern(element);
// Checking some complex patterns. As we are using js regexp mechanism, these
// tests doesn't aim to test the regexp mechanism.
element.pattern = "\\d{2}\\s\\d{2}\\s\\d{4}"
element.value = "01 01 2010"
checkValidPattern(element);
element.value = "01/01/2010"
checkInvalidPattern(element);
element.pattern = "[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}";
element.value = "foo@bar.com";
checkValidPattern(element);
element.value = "...@bar.com";
checkInvalidPattern(element);
element.pattern = "^(?:\\w{3,})$";
element.value = "foo";
checkValidPattern(element);
element.value = "f";
checkInvalidPattern(element);
// If @title is specified, it should be added in the validation message.
if (element.type == 'email') {
element.pattern = "foo@bar.com"
element.value = "bar@foo.com";
} else if (element.type == 'url') {
element.pattern = "http://mozilla.com";
element.value = "http://mozilla.org";
} else {
element.pattern = "foo";
element.value = "bar";
}
element.title = "this is an explanation of the regexp";
is(element.validationMessage,
"Please match the requested format: " + element.title + ".",
"Validation message is not valid");
element.title = "";
is(element.validationMessage,
"Please match the requested format.",
"Validation message is not valid");
element.pattern = "foo";
if (element.type == 'email') {
element.value = "bar@foo.com";
} else if (element.type == 'url') {
element.value = "http://mozilla.org";
} else {
element.value = "bar";
}
checkInvalidPattern(element);
element.removeAttribute('pattern');
checkValidPattern(element, true);
// Unicode pattern
for (var pattern of ["\\u{1F438}{2}", "\u{1F438}{2}",
"\\uD83D\\uDC38{2}", "\uD83D\uDC38{2}",
"\u{D83D}\u{DC38}{2}"]) {
element.pattern = pattern;
element.value = "\u{1F438}\u{1F438}";
checkValidPattern(element);
element.value = "\uD83D\uDC38\uD83D\uDC38";
checkValidPattern(element);
element.value = "\uD83D\uDC38\uDC38";
checkInvalidPattern(element);
}
element.pattern = "\\u{D83D}\\u{DC38}{2}";
element.value = "\u{1F438}\u{1F438}";
checkInvalidPattern(element);
element.value = "\uD83D\uDC38\uD83D\uDC38";
checkInvalidPattern(element);
element.value = "\uD83D\uDC38\uDC38";
checkInvalidPattern(element);
}
var input = document.getElementById('i');
// |validTypes| are the types which accept @pattern
// and |invalidTypes| are the ones which do not accept it.
var validTypes = Array('text', 'password', 'search', 'tel', 'email', 'url');
var barredTypes = Array('hidden', 'reset', 'button');
var invalidTypes = Array('checkbox', 'radio', 'file', 'number', 'range', 'date',
'time', 'color', 'submit', 'image', 'month', 'week',
'datetime-local');
for (type of validTypes) {
input.type = type;
completeValidityCheck(input, false);
checkPatternValidity(input);
}
for (type of barredTypes) {
input.type = type;
completeValidityCheck(input, true, true);
}
for (type of invalidTypes) {
input.type = type;
completeValidityCheck(input, true);
}
</script>
</pre>
</body>
</html>
|