Skip to content

Commit 7e52dbf

Browse files
committed
fixed checkValidity()/reportValidity() for HtmlOutput
1 parent c14cfe2 commit 7e52dbf

3 files changed

Lines changed: 169 additions & 4 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</action>
2424
<action type="fix" dev="rbri">
2525
Fixed checkValidity()/reportValidity() and the CSS :valid/:invalid pseudo-classes
26-
for HtmlButtons, HtmlTextarea, HtmlInput's, HtmlObject.
26+
for HtmlButtons, HtmlTextarea, HtmlInput's, HtmlObject, HtmlOutput.
2727
</action>
2828
<action type="fix" dev="rbri">
2929
HtmlFileInput.reset(): a form reset no longer tries to reconstruct a fake File from the

src/main/java/org/htmlunit/javascript/host/html/HTMLOutputElement.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public NodeList getLabels() {
100100
*/
101101
@JsxFunction
102102
public boolean checkValidity() {
103-
return getDomNodeOrDie().isValid();
103+
final HtmlOutput output = getDomNodeOrDie();
104+
return !output.willValidate() || output.isValid();
104105
}
105106

106107
/**
@@ -131,7 +132,7 @@ public ValidityState getValidity() {
131132
*/
132133
@JsxGetter
133134
public boolean isWillValidate() {
134-
return ((HtmlOutput) getDomNodeOrDie()).willValidate();
135+
return getDomNodeOrDie().willValidate();
135136
}
136137

137138
/**
@@ -140,6 +141,14 @@ public boolean isWillValidate() {
140141
*/
141142
@JsxFunction
142143
public void setCustomValidity(final String message) {
143-
((HtmlOutput) getDomNodeOrDie()).setCustomValidity(message);
144+
getDomNodeOrDie().setCustomValidity(message);
145+
}
146+
147+
/**
148+
* {@inheritDoc}
149+
*/
150+
@Override
151+
public HtmlOutput getDomNodeOrDie() {
152+
return (HtmlOutput) super.getDomNodeOrDie();
144153
}
145154
}

src/test/java/org/htmlunit/javascript/host/html/HTMLOutputElementTest.java

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,160 @@ public void willValidate() throws Exception {
9494

9595
loadPageVerifyTitle2(html);
9696
}
97+
98+
/**
99+
* @throws Exception if an error occurs
100+
*/
101+
@Test
102+
@Alerts({"false", "true", "false", "true"})
103+
public void setCustomValidityOnPlainOutput() throws Exception {
104+
final String html = DOCTYPE_HTML
105+
+ "<html><head>\n"
106+
+ " <script>\n"
107+
+ LOG_TITLE_FUNCTION
108+
+ " function test() {\n"
109+
+ " var o = document.getElementById('o');\n"
110+
+ " log(o.willValidate);\n"
111+
+ " o.setCustomValidity('some error');\n"
112+
+ " log(o.validity.customError);\n"
113+
+ " log(o.validity.valid);\n"
114+
+ " log(o.checkValidity());\n"
115+
// + " log(o.validationMessage);\n"
116+
+ " }\n"
117+
+ " </script>\n"
118+
+ "</head>\n"
119+
+ "<body onload='test()'>\n"
120+
+ " <form>\n"
121+
+ " <output id='o'></output>"
122+
+ " </form>\n"
123+
+ "</body></html>";
124+
125+
loadPageVerifyTitle2(html);
126+
}
127+
128+
/**
129+
* @throws Exception if an error occurs
130+
*/
131+
@Test
132+
@Alerts({"true", "true", "true", "true", "true"})
133+
public void checkValidityMirrorsWillValidateAcrossAllCases() throws Exception {
134+
final String html = DOCTYPE_HTML
135+
+ "<html><head>\n"
136+
+ " <script>\n"
137+
+ LOG_TITLE_FUNCTION
138+
+ " function test() {\n"
139+
+ " log(document.getElementById('i1').checkValidity());\n"
140+
+ " log(document.getElementById('i2').checkValidity());\n"
141+
+ " log(document.getElementById('i3').checkValidity());\n"
142+
+ " log(document.getElementById('i4').checkValidity());\n"
143+
+ " log(document.getElementById('i5').checkValidity());\n"
144+
+ " }\n"
145+
+ " </script>\n"
146+
+ "</head>\n"
147+
+ "<body onload='test()'>\n"
148+
+ " <form>\n"
149+
+ " <output id='i1'>button</output>"
150+
+ " <output id='i2' disabled></output>"
151+
+ " <output id='i3' hidden></output>"
152+
+ " <output id='i4' readonly></output>"
153+
+ " <output id='i5' style='display: none'></output>"
154+
+ " </form>\n"
155+
+ "</body></html>";
156+
157+
loadPageVerifyTitle2(html);
158+
}
159+
160+
/**
161+
* The reportValidity() coverage, entirely absent currently -- confirms it
162+
* returns the same boolean as checkValidity() for a plain output with a
163+
* custom validity message set.
164+
* @throws Exception if an error occurs
165+
*/
166+
@Test
167+
@Alerts({"true", "true"})
168+
public void reportValidityMatchesCheckValidity() throws Exception {
169+
final String html = DOCTYPE_HTML
170+
+ "<html><head>\n"
171+
+ " <script>\n"
172+
+ LOG_TITLE_FUNCTION
173+
+ " function test() {\n"
174+
+ " var o = document.getElementById('o');\n"
175+
+ " o.setCustomValidity('some error');\n"
176+
+ " log(o.checkValidity());\n"
177+
+ " log(o.reportValidity());\n"
178+
+ " }\n"
179+
+ " </script>\n"
180+
+ "</head>\n"
181+
+ "<body onload='test()'>\n"
182+
+ " <form>\n"
183+
+ " <output id='o'></output>"
184+
+ " </form>\n"
185+
+ "</body></html>";
186+
187+
loadPageVerifyTitle2(html);
188+
}
189+
190+
/**
191+
* Clearing a previously-set custom validity message (empty string) must be
192+
* reversible -- checked via .validity.customError and .validity.valid
193+
* regardless of which way the plain-output ambiguity above resolves.
194+
* @throws Exception if an error occurs
195+
*/
196+
@Test
197+
@Alerts({"true", "false", "true"})
198+
public void clearCustomValidity() throws Exception {
199+
final String html = DOCTYPE_HTML
200+
+ "<html><head>\n"
201+
+ " <script>\n"
202+
+ LOG_TITLE_FUNCTION
203+
+ " function test() {\n"
204+
+ " var o = document.getElementById('o');\n"
205+
+ " o.setCustomValidity('some error');\n"
206+
+ " log(o.validity.customError);\n"
207+
+ " o.setCustomValidity('');\n"
208+
+ " log(o.validity.customError);\n"
209+
+ " log(o.validity.valid);\n"
210+
+ " }\n"
211+
+ " </script>\n"
212+
+ "</head>\n"
213+
+ "<body onload='test()'>\n"
214+
+ " <form>\n"
215+
+ " <output id='o'></output>"
216+
+ " </form>\n"
217+
+ "</body></html>";
218+
219+
loadPageVerifyTitle2(html);
220+
}
221+
222+
/**
223+
* An output that IS a descendant of a disabled fieldset. Since
224+
* willValidate() already reports false uniformly for output regardless of
225+
* its OWN attributes, this checks whether fieldset-ancestor disabling is
226+
* even independently observable for output at all, or whether it's just
227+
* redundant with the element's own always-false state.
228+
* @throws Exception if an error occurs
229+
*/
230+
@Test
231+
@Alerts({"false", "true"})
232+
public void outputInsideDisabledFieldset() throws Exception {
233+
final String html = DOCTYPE_HTML
234+
+ "<html><head>\n"
235+
+ " <script>\n"
236+
+ LOG_TITLE_FUNCTION
237+
+ " function test() {\n"
238+
+ " var o = document.getElementById('o');\n"
239+
+ " log(o.willValidate);\n"
240+
+ " o.setCustomValidity('some error');\n"
241+
+ " log(o.checkValidity());\n"
242+
+ " }\n"
243+
+ " </script>\n"
244+
+ "</head>\n"
245+
+ "<body onload='test()'>\n"
246+
+ " <form>\n"
247+
+ " <fieldset disabled><output id='o'></output></fieldset>"
248+
+ " </form>\n"
249+
+ "</body></html>";
250+
251+
loadPageVerifyTitle2(html);
252+
}
97253
}

0 commit comments

Comments
 (0)