summaryrefslogtreecommitdiffstats
path: root/dom/html/test/test_bug610687.html
blob: f319685edf97e96e67004c4fbe85550a477bc7fd (plain)
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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=610687
-->
<head>
  <title>Test for Bug 610687</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=610687">Mozilla Bug 610687</a>
<p id="display"></p>
<div id="content" style="display: none">
  <form>
    <input type='radio' name='a'>
    <input type='radio' name='a'>
    <input type='radio' name='b'>
  </form>
  <input type='radio' name='a'>
  <input type='radio' name='a'>
  <input type='radio' name='b'>
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 610687 **/

function checkPseudoClasses(aElement, aValid, aValidUI, aInvalidUI)
{
  if (aValid) {
    ok(aElement.matches(":valid"), ":valid should apply");
  } else {
    ok(aElement.matches(":invalid"), ":invalid should apply");
  }

  is(aElement.matches(":-moz-ui-valid"), aValidUI,
     aValid ? ":-moz-ui-valid should apply" : ":-moz-ui-valid should not apply");

  is(aElement.matches(":-moz-ui-invalid"), aInvalidUI,
     aInvalidUI ? ":-moz-ui-invalid should apply" : ":-moz-ui-invalid should not apply");

  if (aInvalidUI && (aValid || aValidUI)) {
    ok(false, ":invalid can't apply with :valid or :-moz-valid-ui");
  }
}

/**
 * r1 and r2 should be in the same group.
 * r3 should be in another group.
 * form can be null.
 */
function checkRadios(r1, r2, r3, form)
{
  // Default state.
  checkPseudoClasses(r1, true, false, false);
  checkPseudoClasses(r2, true, false, false);
  checkPseudoClasses(r3, true, false, false);

  // Suffering from being missing (without ui-invalid).
  r1.required = true;
  checkPseudoClasses(r1, false, false, false);
  checkPseudoClasses(r2, false, false, false);
  checkPseudoClasses(r3, true, false, false);

  // Suffering from being missing (with ui-invalid).
  r1.checked = false;
  checkPseudoClasses(r1, false, false, true);
  checkPseudoClasses(r2, false, false, true);
  checkPseudoClasses(r3, true, false, false);

  // Do not suffer from being missing (with ui-valid).
  r1.checked = true;
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, true, true, false);
  checkPseudoClasses(r3, true, false, false);

  // Do not suffer from being missing (with ui-valid).
  r1.checked = false;
  r1.required = false;
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, true, true, false);
  checkPseudoClasses(r3, true, false, false);

  // Suffering from being missing (with ui-invalid) with required set on one radio
  // and the checked state changed on another.
  r1.required = true;
  r2.checked = false;
  checkPseudoClasses(r1, false, false, true);
  checkPseudoClasses(r2, false, false, true);
  checkPseudoClasses(r3, true, false, false);

  // Do not suffer from being missing (with ui-valid) by checking the radio which
  // hasn't the required attribute.
  r2.checked = true;
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, true, true, false);
  checkPseudoClasses(r3, true, false, false);

  // .setCustomValidity() should not affect the entire group.
  r1.checked = r2.checked = r3.checked = false;
  r1.required = false;
  r1.setCustomValidity('foo');
  checkPseudoClasses(r1, false, false, true);
  checkPseudoClasses(r2, true, true, false);
  checkPseudoClasses(r3, true, true, false);

  r1.setCustomValidity('');
  r2.setCustomValidity('foo');
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, false, false, true);
  checkPseudoClasses(r3, true, true, false);

  r2.setCustomValidity('');
  r3.setCustomValidity('foo');
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, true, true, false);
  checkPseudoClasses(r3, false, false, true);

  // Removing the radio with the required attribute should make the group valid.
  r1.setCustomValidity('');
  r2.setCustomValidity('');
  r1.required = false;
  r2.required = true;
  r1.checked = r2.checked = false;
  checkPseudoClasses(r1, false, false, true);
  checkPseudoClasses(r2, false, false, true);

  var p = r2.parentNode;
  p.removeChild(r2);
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, false, false, true);

  p.appendChild(r2);
  checkPseudoClasses(r1, false, false, true);
  checkPseudoClasses(r2, false, false, true);

  // Adding a radio element to an invalid group should make it invalid.
  p.removeChild(r1);
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, false, false, true);

  p.appendChild(r1);
  checkPseudoClasses(r1, false, false, true);
  checkPseudoClasses(r2, false, false, true);

  // Adding a checked radio element to an invalid group should make it valid.
  p.removeChild(r1);
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, false, false, true);

  r1.checked = true;
  p.appendChild(r1);
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, true, true, false);
  r1.checked = false;

  // Adding an invalid radio element by changing the name attribute.
  r2.name = 'c';
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, false, false, true);

  r2.name = 'a';
  checkPseudoClasses(r1, false, false, true);
  checkPseudoClasses(r2, false, false, true);

  // Adding an element to an invalid radio group by changing the name attribute.
  r1.name = 'c';
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, false, false, true);

  r1.name = 'a';
  checkPseudoClasses(r1, false, false, true);
  checkPseudoClasses(r2, false, false, true);

  // Adding a checked element to an invalid radio group with the name attribute.
  r1.name = 'c';
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, false, false, true);

  r1.checked = true;
  r1.name = 'a';
  checkPseudoClasses(r1, true, true, false);
  checkPseudoClasses(r2, true, true, false);
  r1.checked = false;
}

var r1 = document.getElementsByTagName('input')[0];
var r2 = document.getElementsByTagName('input')[1];
var r3 = document.getElementsByTagName('input')[2];
checkRadios(r1, r2, r3);

r1 = document.getElementsByTagName('input')[3];
r2 = document.getElementsByTagName('input')[4];
r3 = document.getElementsByTagName('input')[5];
checkRadios(r1, r2, r3);

</script>
</pre>
</body>
</html>