View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.dom;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link DOMException}.
23   *
24   * @author Marc Guillemot
25   * @author Frank Danek
26   * @author Ronald Brill
27   */
28  public class DOMExceptionTest extends WebDriverTestCase {
29  
30      /**
31       * @throws Exception if the test fails
32       */
33      @Test
34      @Alerts({"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"})
35      public void constants() throws Exception {
36          final String html = DOCTYPE_HTML
37              + "<html><head>\n"
38              + "<script>\n"
39              + LOG_TITLE_FUNCTION
40              + "  var properties = ['INDEX_SIZE_ERR', 'DOMSTRING_SIZE_ERR', 'HIERARCHY_REQUEST_ERR',"
41              + " 'WRONG_DOCUMENT_ERR', 'INVALID_CHARACTER_ERR', 'NO_DATA_ALLOWED_ERR', 'NO_MODIFICATION_ALLOWED_ERR',"
42              + " 'NOT_FOUND_ERR', 'NOT_SUPPORTED_ERR', 'INUSE_ATTRIBUTE_ERR', 'INVALID_STATE_ERR', 'SYNTAX_ERR',"
43              + " 'INVALID_MODIFICATION_ERR', 'NAMESPACE_ERR', 'INVALID_ACCESS_ERR'];\n"
44              + "  try {\n"
45              + "    for (var i = 0; i < properties.length; i++) {\n"
46              + "      log(DOMException[properties[i]]);\n"
47              + "    }\n"
48              + "  } catch(e) { logEx(e);}\n"
49              + "</script></head>\n"
50              + "<body></body></html>";
51  
52          loadPageVerifyTitle2(html);
53      }
54  
55      /**
56       * @throws Exception if the test fails
57       */
58      @Test
59      @Alerts({"IndexSizeError - 1 IndexSizeError",
60               "INDEX_SIZE_ERR - 0 INDEX_SIZE_ERR",
61               "HierarchyRequestError - 3 HierarchyRequestError",
62               "HIERARCHY_REQUEST_ERR - 0 HIERARCHY_REQUEST_ERR",
63               "WrongDocumentError - 4 WrongDocumentError",
64               "WRONG_DOCUMENT_ERR - 0 WRONG_DOCUMENT_ERR",
65               "InvalidCharacterError - 5 InvalidCharacterError",
66               "INVALID_CHARACTER_ERR - 0 INVALID_CHARACTER_ERR",
67               "NoModificationAllowedError - 7 NoModificationAllowedError",
68               "NO_MODIFICATION_ALLOWED_ERR - 0 NO_MODIFICATION_ALLOWED_ERR",
69               "NotFoundError - 8 NotFoundError",
70               "NOT_FOUND_ERR - 0 NOT_FOUND_ERR",
71               "NotSupportedError - 9 NotSupportedError",
72               "NOT_SUPPORTED_ERR - 0 NOT_SUPPORTED_ERR",
73               "InvalidStateError - 11 InvalidStateError",
74               "INVALID_STATE_ERR - 0 INVALID_STATE_ERR",
75               "InUseAttributeError - 10 InUseAttributeError",
76               "INUSE_ATTRIBUTE_ERR - 0 INUSE_ATTRIBUTE_ERR",
77               "SyntaxError - 12 SyntaxError",
78               "SYNTAX_ERR - 0 SYNTAX_ERR",
79               "InvalidModificationError - 13 InvalidModificationError",
80               "INVALID_MODIFICATION_ERR - 0 INVALID_MODIFICATION_ERR",
81               "NamespaceError - 14 NamespaceError",
82               "NAMESPACE_ERR - 0 NAMESPACE_ERR",
83               "InvalidAccessError - 15 InvalidAccessError",
84               "INVALID_ACCESS_ERR - 0 INVALID_ACCESS_ERR",
85               "TypeMismatchError - 17 TypeMismatchError",
86               "TYPE_MISMATCH_ERR - 0 TYPE_MISMATCH_ERR",
87               "SecurityError - 18 SecurityError",
88               "SECURITY_ERR - 0 SECURITY_ERR",
89               "NetworkError - 19 NetworkError",
90               "NETWORK_ERR - 0 NETWORK_ERR",
91               "AbortError - 20 AbortError",
92               "ABORT_ERR - 0 ABORT_ERR",
93               "URLMismatchError - 21 URLMismatchError",
94               "URL_MISMATCH_ERR - 0 URL_MISMATCH_ERR",
95               "QuotaExceededError - 22 QuotaExceededError",
96               "QUOTA_EXCEEDED_ERR - 0 QUOTA_EXCEEDED_ERR",
97               "TimeoutError - 23 TimeoutError",
98               "TIMEOUT_ERR - 0 TIMEOUT_ERR",
99               "InvalidNodeTypeError - 24 InvalidNodeTypeError",
100              "INVALID_NODE_TYPE_ERR - 0 INVALID_NODE_TYPE_ERR",
101              "DataCloneError - 25 DataCloneError",
102              "DATA_CLONE_ERR - 0 DATA_CLONE_ERR",
103              "EncodingError - 0 EncodingError",
104              "NotReadableError - 0 NotReadableError",
105              "UnknownError - 0 UnknownError",
106              "ConstraintError - 0 ConstraintError",
107              "DataError - 0 DataError",
108              "TransactionInactiveError - 0 TransactionInactiveError",
109              "ReadOnlyError - 0 ReadOnlyError",
110              "VersionError - 0 VersionError",
111              "OperationError - 0 OperationError",
112              "NotAllowedError - 0 NotAllowedError"})
113     public void name() throws Exception {
114         final String html = DOCTYPE_HTML
115             + "<html><head>\n"
116             + "<script>\n"
117             + LOG_TITLE_FUNCTION
118             // https://developer.mozilla.org/en-US/docs/Web/API/DOMException#error_names
119             + "  var commonErrorNames = ["
120                 + "'IndexSizeError', 'INDEX_SIZE_ERR',"
121                 + "'HierarchyRequestError', 'HIERARCHY_REQUEST_ERR',"
122                 + "'WrongDocumentError', 'WRONG_DOCUMENT_ERR',"
123                 + "'InvalidCharacterError', 'INVALID_CHARACTER_ERR',"
124                 + "'NoModificationAllowedError', 'NO_MODIFICATION_ALLOWED_ERR',"
125                 + "'NotFoundError', 'NOT_FOUND_ERR',"
126                 + "'NotSupportedError', 'NOT_SUPPORTED_ERR',"
127                 + "'InvalidStateError', 'INVALID_STATE_ERR',"
128                 + "'InUseAttributeError', 'INUSE_ATTRIBUTE_ERR',"
129                 + "'SyntaxError', 'SYNTAX_ERR',"
130                 + "'InvalidModificationError', 'INVALID_MODIFICATION_ERR',"
131                 + "'NamespaceError', 'NAMESPACE_ERR',"
132                 + "'InvalidAccessError', 'INVALID_ACCESS_ERR',"
133                 + "'TypeMismatchError', 'TYPE_MISMATCH_ERR',"
134                 + "'SecurityError', 'SECURITY_ERR',"
135                 + "'NetworkError', 'NETWORK_ERR',"
136                 + "'AbortError', 'ABORT_ERR',"
137                 + "'URLMismatchError', 'URL_MISMATCH_ERR',"
138                 + "'QuotaExceededError', 'QUOTA_EXCEEDED_ERR',"
139                 + "'TimeoutError', 'TIMEOUT_ERR',"
140                 + "'InvalidNodeTypeError', 'INVALID_NODE_TYPE_ERR',"
141                 + "'DataCloneError', 'DATA_CLONE_ERR',"
142                 + "'EncodingError',"
143                 + "'NotReadableError',"
144                 + "'UnknownError',"
145                 + "'ConstraintError',"
146                 + "'DataError',"
147                 + "'TransactionInactiveError',"
148                 + "'ReadOnlyError',"
149                 + "'VersionError',"
150                 + "'OperationError',"
151                 + "'NotAllowedError'"
152                 + "];\n"
153             + "  try {\n"
154             + "    for (var i = 0; i < commonErrorNames.length; i++) {\n"
155             + "      let ex = new DOMException('test', commonErrorNames[i]);"
156             + "      log(commonErrorNames[i] + ' - ' + ex.code + ' ' + ex.name);\n"
157             + "    }\n"
158             + "  } catch(e) { logEx(e);}\n"
159             + "</script></head>\n"
160             + "<body></body></html>";
161 
162         loadPageVerifyTitle2(html);
163     }
164 
165     /**
166      * @throws Exception if the test fails
167      */
168     @Test
169     @Alerts({"0", "urlMismatchERRoR"})
170     public void nameCaseSensitive() throws Exception {
171         final String html = DOCTYPE_HTML
172             + "<html><head>\n"
173             + "<script>\n"
174             + LOG_TITLE_FUNCTION
175             + "  try {\n"
176             + "    let ex = new DOMException('test', 'urlMismatchERRoR');"
177             + "    log(ex.code);\n"
178             + "    log(ex.name);\n"
179             + "  } catch(e) { logEx(e);}\n"
180             + "</script></head>\n"
181             + "<body></body></html>";
182 
183         loadPageVerifyTitle2(html);
184     }
185 
186     /**
187      * @throws Exception if the test fails
188      */
189     @Test
190     @Alerts({"0", "Error"})
191     public void nameNotProvided() throws Exception {
192         final String html = DOCTYPE_HTML
193             + "<html><head>\n"
194             + "<script>\n"
195             + LOG_TITLE_FUNCTION
196             + "  try {\n"
197             + "    let ex = new DOMException('test');"
198             + "    log(ex.code);\n"
199             + "    log(ex.name);\n"
200             + "  } catch(e) { logEx(e);}\n"
201             + "</script></head>\n"
202             + "<body></body></html>";
203 
204         loadPageVerifyTitle2(html);
205     }
206 
207     /**
208      * @throws Exception if the test fails
209      */
210     @Test
211     @Alerts({"0", "null"})
212     public void nameNull() throws Exception {
213         final String html = DOCTYPE_HTML
214             + "<html><head>\n"
215             + "<script>\n"
216             + LOG_TITLE_FUNCTION
217             + "  try {\n"
218             + "    let ex = new DOMException('test', null);"
219             + "    log(ex.code);\n"
220             + "    log(ex.name);\n"
221             + "  } catch(e) { logEx(e);}\n"
222             + "</script></head>\n"
223             + "<body></body></html>";
224 
225         loadPageVerifyTitle2(html);
226     }
227 
228     /**
229      * @throws Exception if the test fails
230      */
231     @Test
232     @Alerts({"0", "Error"})
233     public void nameUndefined() throws Exception {
234         final String html = DOCTYPE_HTML
235             + "<html><head>\n"
236             + "<script>\n"
237             + LOG_TITLE_FUNCTION
238             + "  try {\n"
239             + "    let ex = new DOMException('test', undefined);"
240             + "    log(ex.code);\n"
241             + "    log(ex.name);\n"
242             + "  } catch(e) { logEx(e);}\n"
243             + "</script></head>\n"
244             + "<body></body></html>";
245 
246         loadPageVerifyTitle2(html);
247     }
248 
249     /**
250      * @throws Exception if the test fails
251      */
252     @Test
253     @Alerts({"0", "unKnown"})
254     public void nameUnknown() throws Exception {
255         final String html = DOCTYPE_HTML
256             + "<html><head>\n"
257             + "<script>\n"
258             + LOG_TITLE_FUNCTION
259             + "  try {\n"
260             + "    let ex = new DOMException('test', 'unKnown');"
261             + "    log(ex.code);\n"
262             + "    log(ex.name);\n"
263             + "  } catch(e) { logEx(e);}\n"
264             + "</script></head>\n"
265             + "<body></body></html>";
266 
267         loadPageVerifyTitle2(html);
268     }
269 
270     /**
271      * @throws Exception if the test fails
272      */
273     @Test
274     @Alerts({"0", "7"})
275     public void nameNumber() throws Exception {
276         final String html = DOCTYPE_HTML
277             + "<html><head>\n"
278             + "<script>\n"
279             + LOG_TITLE_FUNCTION
280             + "  try {\n"
281             + "    let ex = new DOMException('test', 7);"
282             + "    log(ex.code);\n"
283             + "    log(ex.name);\n"
284             + "  } catch(e) { logEx(e);}\n"
285             + "</script></head>\n"
286             + "<body></body></html>";
287 
288         loadPageVerifyTitle2(html);
289     }
290 
291     /**
292      * @throws Exception if the test fails
293      */
294     @Test
295     @Alerts({"undefined", "undefined", "undefined", "undefined"})
296     public void properties() throws Exception {
297         final String html = DOCTYPE_HTML
298             + "<html><head>\n"
299             + "<script>\n"
300             + LOG_TITLE_FUNCTION
301             + "  try {\n"
302             + "    log(DOMException.code);\n"
303             + "    log(DOMException.filename);\n"
304             + "    log(DOMException.lineNumber);\n"
305             + "    log(DOMException.message);\n"
306             + "  } catch(e) { logEx(e);}\n"
307             + "</script></head>\n"
308             + "<body></body></html>";
309 
310         loadPageVerifyTitle2(html);
311     }
312 
313     /**
314      * Test exception throw by an illegal DOM appendChild.
315      * @throws Exception if the test fails
316      */
317     @Test
318     @Alerts(DEFAULT = {"3", "true", "undefined", "undefined", "HIERARCHY_REQUEST_ERR: 3", "1"},
319             FF = {"3", "true", "26", "§§URL§§", "HIERARCHY_REQUEST_ERR: 3", "1"},
320             FF_ESR = {"3", "true", "26", "§§URL§§", "HIERARCHY_REQUEST_ERR: 3", "1"})
321     /*
322      * Messages:
323      * CHROME: "A Node was inserted somewhere it doesn't belong."
324      * FF: "Node cannot be inserted at the specified point in the hierarchy"
325      */
326     public void appendChild_illegal_node() throws Exception {
327         final String html = DOCTYPE_HTML
328             + "<html><head>\n"
329             + "<script>\n"
330             + LOG_TITLE_FUNCTION
331             + "function test() {\n"
332             + "  var htmlNode = document.documentElement;\n"
333             + "  var body = document.body;\n"
334             + "  try {\n"
335             + "    body.appendChild(htmlNode);\n"
336             + "  } catch(e) {\n"
337             + "    log(e.code);\n"
338             + "    log(e.message != null);\n"
339             + "    log(e.lineNumber);\n"
340             + "    log(e.filename);\n"
341             + "    log('HIERARCHY_REQUEST_ERR: ' + e.HIERARCHY_REQUEST_ERR);\n"
342             + "  }\n"
343             + "  log(body.childNodes.length);\n"
344             + "}\n"
345             + "</script></head>\n"
346             + "<body onload='test()'><span>hi</span></body></html>";
347 
348         expandExpectedAlertsVariables(URL_FIRST);
349         loadPageVerifyTitle2(html);
350     }
351 }