Skip to content

Commit c14cfe2

Browse files
committed
fixed checkValidity()/reportValidity() for HtmlObject
1 parent 7665b1c commit c14cfe2

3 files changed

Lines changed: 176 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.
26+
for HtmlButtons, HtmlTextarea, HtmlInput's, HtmlObject.
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/HTMLObjectElement.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ public HTMLFormElement getForm() {
173173
*/
174174
@JsxFunction
175175
public boolean checkValidity() {
176-
return getDomNodeOrDie().isValid();
176+
final HtmlObject object = getDomNodeOrDie();
177+
return !object.willValidate() || object.isValid();
177178
}
178179

179180
/**
@@ -204,7 +205,7 @@ public ValidityState getValidity() {
204205
*/
205206
@JsxGetter
206207
public boolean isWillValidate() {
207-
return ((HtmlObject) getDomNodeOrDie()).willValidate();
208+
return getDomNodeOrDie().willValidate();
208209
}
209210

210211
/**
@@ -213,6 +214,14 @@ public boolean isWillValidate() {
213214
*/
214215
@JsxFunction
215216
public void setCustomValidity(final String message) {
216-
((HtmlObject) getDomNodeOrDie()).setCustomValidity(message);
217+
getDomNodeOrDie().setCustomValidity(message);
218+
}
219+
220+
/**
221+
* {@inheritDoc}
222+
*/
223+
@Override
224+
public HtmlObject getDomNodeOrDie() {
225+
return (HtmlObject) super.getDomNodeOrDie();
217226
}
218227
}

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

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,167 @@ public void responseXML_htmlObject() throws Exception {
8181
loadPage2(html);
8282
verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
8383
}
84+
85+
/**
86+
* Method willValidate must always be false
87+
* for an &lt;object&gt;, regardless of hidden/style/disabled-ancestor
88+
* state.
89+
* @throws Exception if an error occurs
90+
*/
91+
@Test
92+
@Alerts({"false", "false", "false", "false"})
93+
public void willValidateAlwaysFalse() throws Exception {
94+
final String html = DOCTYPE_HTML
95+
+ "<html><head>\n"
96+
+ " <script>\n"
97+
+ LOG_TITLE_FUNCTION
98+
+ " function test() {\n"
99+
+ " log(document.getElementById('i1').willValidate);\n"
100+
+ " log(document.getElementById('i2').willValidate);\n"
101+
+ " log(document.getElementById('i3').willValidate);\n"
102+
+ " log(document.getElementById('i4').willValidate);\n"
103+
+ " }\n"
104+
+ " </script>\n"
105+
+ "</head>\n"
106+
+ "<body onload='test()'>\n"
107+
+ " <form>\n"
108+
+ " <object id='i1'></object>"
109+
+ " <object id='i2' hidden></object>"
110+
+ " <object id='i3' style='display: none'></object>"
111+
+ " <fieldset disabled><object id='i4'></object></fieldset>"
112+
+ " </form>\n"
113+
+ "</body></html>";
114+
115+
loadPageVerifyTitle2(html);
116+
}
117+
118+
/**
119+
* The checkValidity() must always return true on an &lt;object&gt;, regardless
120+
* of hidden/style/disabled-ancestor state.
121+
* @throws Exception if an error occurs
122+
*/
123+
@Test
124+
@Alerts({"true", "true", "true", "true"})
125+
public void checkValidityAlwaysTrue() throws Exception {
126+
final String html = DOCTYPE_HTML
127+
+ "<html><head>\n"
128+
+ " <script>\n"
129+
+ LOG_TITLE_FUNCTION
130+
+ " function test() {\n"
131+
+ " log(document.getElementById('i1').checkValidity());\n"
132+
+ " log(document.getElementById('i2').checkValidity());\n"
133+
+ " log(document.getElementById('i3').checkValidity());\n"
134+
+ " log(document.getElementById('i4').checkValidity());\n"
135+
+ " }\n"
136+
+ " </script>\n"
137+
+ "</head>\n"
138+
+ "<body onload='test()'>\n"
139+
+ " <form>\n"
140+
+ " <object id='i1'></object>"
141+
+ " <object id='i2' hidden></object>"
142+
+ " <object id='i3' style='display: none'></object>"
143+
+ " <fieldset disabled><object id='i4'></object></fieldset>"
144+
+ " </form>\n"
145+
+ "</body></html>";
146+
147+
loadPageVerifyTitle2(html);
148+
}
149+
150+
/**
151+
* The reportValidity() must always return true on an &lt;object&gt;, mirroring
152+
* checkValidity().
153+
* @throws Exception if an error occurs
154+
*/
155+
@Test
156+
@Alerts({"true", "true", "true", "true"})
157+
public void reportValidityAlwaysTrue() throws Exception {
158+
final String html = DOCTYPE_HTML
159+
+ "<html><head>\n"
160+
+ " <script>\n"
161+
+ LOG_TITLE_FUNCTION
162+
+ " function test() {\n"
163+
+ " log(document.getElementById('i1').reportValidity());\n"
164+
+ " log(document.getElementById('i2').reportValidity());\n"
165+
+ " log(document.getElementById('i3').reportValidity());\n"
166+
+ " log(document.getElementById('i4').reportValidity());\n"
167+
+ " }\n"
168+
+ " </script>\n"
169+
+ "</head>\n"
170+
+ "<body onload='test()'>\n"
171+
+ " <form>\n"
172+
+ " <object id='i1'></object>"
173+
+ " <object id='i2' hidden></object>"
174+
+ " <object id='i3' style='display: none'></object>"
175+
+ " <fieldset disabled><object id='i4'></object></fieldset>"
176+
+ " </form>\n"
177+
+ "</body></html>";
178+
179+
loadPageVerifyTitle2(html);
180+
}
181+
182+
/**
183+
* Since an &lt;object&gt; is never itself a candidate for
184+
* constraint validation, a custom validity message set on it must have NO
185+
* effect on checkValidity() -- must still report true, and
186+
* .validity.valid must still be true.
187+
* @throws Exception if an error occurs
188+
*/
189+
@Test
190+
@Alerts({"false", "false", "true"})
191+
public void setCustomValidityDoesNotAffectCheckValidity() throws Exception {
192+
final String html = DOCTYPE_HTML
193+
+ "<html><head>\n"
194+
+ " <script>\n"
195+
+ LOG_TITLE_FUNCTION
196+
+ " function test() {\n"
197+
+ " var o = document.getElementById('o');\n"
198+
+ " o.setCustomValidity('some error');\n"
199+
+ " log(o.willValidate);\n"
200+
+ " log(o.validity.valid);\n"
201+
+ " log(o.checkValidity());\n"
202+
// + " log(o.validationMessage);\n"
203+
+ " }\n"
204+
+ " </script>\n"
205+
+ "</head>\n"
206+
+ "<body onload='test()'>\n"
207+
+ " <form>\n"
208+
+ " <object id='o'></object>"
209+
+ " </form>\n"
210+
+ "</body></html>";
211+
212+
loadPageVerifyTitle2(html);
213+
}
214+
//
215+
// /**
216+
// * The validationMessage must always be empty on an &lt;object&gt;, even with a
217+
// * custom validity message set and even when additionally barred via an
218+
// * ancestor disabled fieldset.
219+
// * @throws Exception if an error occurs
220+
// */
221+
// @Test
222+
// @Alerts({"", ""})
223+
// public void validationMessageAlwaysEmpty() throws Exception {
224+
// final String html = DOCTYPE_HTML
225+
// + "<html><head>\n"
226+
// + " <script>\n"
227+
// + LOG_TITLE_FUNCTION
228+
// + " function test() {\n"
229+
// + " var plain = document.getElementById('plain');\n"
230+
// + " var inFieldset = document.getElementById('inFieldset');\n"
231+
// + " plain.setCustomValidity('error1');\n"
232+
// + " inFieldset.setCustomValidity('error2');\n"
233+
// + " log(plain.validationMessage);\n"
234+
// + " log(inFieldset.validationMessage);\n"
235+
// + " }\n"
236+
// + " </script>\n"
237+
// + "</head>\n"
238+
// + "<body onload='test()'>\n"
239+
// + " <form>\n"
240+
// + " <object id='plain'></object>"
241+
// + " <fieldset disabled><object id='inFieldset'></object></fieldset>"
242+
// + " </form>\n"
243+
// + "</body></html>";
244+
//
245+
// loadPageVerifyTitle2(html);
246+
// }
84247
}

0 commit comments

Comments
 (0)