Skip to content

Commit 1c88428

Browse files
committed
more on error handling
1 parent 84241a1 commit 1c88428

File tree

6 files changed

+72
-13
lines changed

6 files changed

+72
-13
lines changed

src/changes/changes.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,42 @@
88

99
<body>
1010
<release version="4.10.0" date="February xx, 2025" description="Bugfixes">
11+
<action type="fix" dev="rbri">
12+
HTMLTableElement/HTMLTableRowElement several fixes to throw the correct error.
13+
</action>
14+
<action type="fix" dev="rbri">
15+
Element.querySelectorAll()/querySelector() throws a SyntaxError.
16+
</action>
17+
<action type="fix" dev="rbri">
18+
HTMLOptionsCollection.add() throws a NotFoundError.
19+
</action>
20+
<action type="fix" dev="rbri">
21+
Accessing a HTMLDocument without a js peer throws a TypeError.
22+
</action>
23+
<action type="fix" dev="rbri">
24+
Document.createElementNS() throws a TypeError.
25+
</action>
26+
<action type="fix" dev="rbri">
27+
Document.createElementNS() throws a TypeError.
28+
</action>
29+
<action type="fix" dev="rbri">
30+
Selection.getRangeAt() throws an IndexSizeError.
31+
</action>
32+
<action type="fix" dev="rbri">
33+
Node.replaceChild()/removeChild()/insertBefore() several fixes to throw the correct error.
34+
</action>
35+
<action type="fix" dev="rbri">
36+
HTMLDocument.appendChild() throws HierarchyRequstError.
37+
</action>
38+
<action type="fix" dev="rbri">
39+
Document.createElement() throws InvalidCharacterError.
40+
</action>
41+
<action type="fix" dev="rbri">
42+
ImageData.ctor() various fixes to throw the correct error in different situations.
43+
</action>
44+
<action type="fix" dev="rbri">
45+
CharacterData.deleteData() throws an IndexSizeError.
46+
</action>
1147
<action type="fix" dev="rbri">
1248
Document.querySelectorAll() throws a SyntaxError if the selector is invalid.
1349
</action>

src/main/java/org/htmlunit/javascript/host/Element.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ public NodeList querySelectorAll(final String selectors) {
508508
return NodeList.staticNodeList(this, getDomNodeOrDie().querySelectorAll(selectors));
509509
}
510510
catch (final CSSException e) {
511-
throw JavaScriptEngine.reportRuntimeError("An invalid or illegal selector was specified (selector: '"
511+
throw JavaScriptEngine.syntaxError("An invalid or illegal selector was specified (selector: '"
512512
+ selectors + "' error: " + e.getMessage() + ").");
513513
}
514514
}
@@ -528,7 +528,7 @@ public Node querySelector(final String selectors) {
528528
return null;
529529
}
530530
catch (final CSSException e) {
531-
throw JavaScriptEngine.reportRuntimeError("An invalid or illegal selector was specified (selector: '"
531+
throw JavaScriptEngine.syntaxError("An invalid or illegal selector was specified (selector: '"
532532
+ selectors + "' error: " + e.getMessage() + ").");
533533
}
534534
}
@@ -929,7 +929,11 @@ public void setOuterHTML(final Object value) {
929929
final DomNode parent = domNode.getParentNode();
930930
if (null == parent) {
931931
if (getBrowserVersion().hasFeature(JS_OUTER_HTML_THROWS_FOR_DETACHED)) {
932-
throw JavaScriptEngine.reportRuntimeError("outerHTML is readonly for detached nodes");
932+
throw JavaScriptEngine.asJavaScriptException(
933+
getWindow(),
934+
new org.htmlunit.javascript.host.dom.DOMException(
935+
"outerHTML is readonly for detached nodes",
936+
org.htmlunit.javascript.host.dom.DOMException.NO_MODIFICATION_ALLOWED_ERR));
933937
}
934938
return;
935939
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.htmlunit.SgmlPage;
1818
import org.htmlunit.WebAssert;
1919
import org.htmlunit.corejs.javascript.Context;
20-
import org.htmlunit.corejs.javascript.EvaluatorException;
2120
import org.htmlunit.corejs.javascript.Scriptable;
2221
import org.htmlunit.corejs.javascript.ScriptableObject;
2322
import org.htmlunit.html.ElementFactory;
@@ -31,6 +30,7 @@
3130
import org.htmlunit.javascript.configuration.JsxGetter;
3231
import org.htmlunit.javascript.configuration.JsxSetter;
3332
import org.htmlunit.javascript.configuration.JsxSymbol;
33+
import org.htmlunit.javascript.host.dom.DOMException;
3434

3535
/**
3636
* This is the array returned by the "options" property of Select.
@@ -257,7 +257,12 @@ public void add(final Object newOptionObject, final Object beforeOptionObject) {
257257
else if (beforeOptionObject instanceof HTMLOptionElement) {
258258
beforeOption = (HtmlOption) ((HTMLOptionElement) beforeOptionObject).getDomNodeOrDie();
259259
if (beforeOption.getParentNode() != htmlSelect_) {
260-
throw new EvaluatorException("Unknown option.");
260+
throw JavaScriptEngine.asJavaScriptException(
261+
getWindow(),
262+
new DOMException(
263+
"Unknown option.",
264+
DOMException.NOT_FOUND_ERR));
265+
261266
}
262267
}
263268

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public HtmlUnitScriptable getCaption() {
8080
@JsxSetter
8181
public void setCaption(final Object o) {
8282
if (!(o instanceof HTMLTableCaptionElement)) {
83-
throw JavaScriptEngine.reportRuntimeError("Not a caption");
83+
throw JavaScriptEngine.typeError("Not a caption");
8484
}
8585

8686
// remove old caption (if any)
@@ -112,7 +112,7 @@ public HtmlUnitScriptable getTFoot() {
112112
public void setTFoot(final Object o) {
113113
if (!(o instanceof HTMLTableSectionElement
114114
&& "TFOOT".equals(((HTMLTableSectionElement) o).getTagName()))) {
115-
throw JavaScriptEngine.reportRuntimeError("Not a tFoot");
115+
throw JavaScriptEngine.typeError("Not a tFoot");
116116
}
117117

118118
// remove old caption (if any)
@@ -144,7 +144,7 @@ public HtmlUnitScriptable getTHead() {
144144
public void setTHead(final Object o) {
145145
if (!(o instanceof HTMLTableSectionElement
146146
&& "THEAD".equals(((HTMLTableSectionElement) o).getTagName()))) {
147-
throw JavaScriptEngine.reportRuntimeError("Not a tHead");
147+
throw JavaScriptEngine.typeError("Not a tHead");
148148
}
149149

150150
// remove old caption (if any)

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.htmlunit.javascript.configuration.JsxFunction;
3232
import org.htmlunit.javascript.configuration.JsxGetter;
3333
import org.htmlunit.javascript.configuration.JsxSetter;
34+
import org.htmlunit.javascript.host.dom.DOMException;
3435

3536
/**
3637
* The JavaScript object {@code HTMLTableRowElement}.
@@ -153,7 +154,11 @@ public HtmlUnitScriptable insertCell(final Object index) {
153154
}
154155
return getScriptableFor(newCell);
155156
}
156-
throw JavaScriptEngine.reportRuntimeError("Index or size is negative or greater than the allowed amount");
157+
throw JavaScriptEngine.asJavaScriptException(
158+
getWindow(),
159+
new DOMException(
160+
"Index or size is negative or greater than the allowed amount",
161+
DOMException.INDEX_SIZE_ERR));
157162
}
158163

159164
/**
@@ -166,7 +171,7 @@ public HtmlUnitScriptable insertCell(final Object index) {
166171
@JsxFunction
167172
public void deleteCell(final Object index) {
168173
if (JavaScriptEngine.isUndefined(index)) {
169-
throw JavaScriptEngine.reportRuntimeError("No enough arguments");
174+
throw JavaScriptEngine.typeError("No enough arguments");
170175
}
171176

172177
int position = (int) JavaScriptEngine.toNumber(index);
@@ -178,7 +183,11 @@ public void deleteCell(final Object index) {
178183
}
179184
final boolean indexValid = position >= -1 && position <= htmlRow.getCells().size();
180185
if (!indexValid) {
181-
throw JavaScriptEngine.reportRuntimeError("Index or size is negative or greater than the allowed amount");
186+
throw JavaScriptEngine.asJavaScriptException(
187+
getWindow(),
188+
new DOMException(
189+
"Index or size is negative or greater than the allowed amount",
190+
DOMException.INDEX_SIZE_ERR));
182191
}
183192

184193
htmlRow.getCell(position).remove();

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.htmlunit.javascript.configuration.JsxFunction;
2828
import org.htmlunit.javascript.configuration.JsxGetter;
2929
import org.htmlunit.javascript.configuration.JsxSetter;
30+
import org.htmlunit.javascript.host.dom.DOMException;
3031

3132
/**
3233
* Superclass for all row-containing JavaScript host classes, including tables,
@@ -107,8 +108,12 @@ public HtmlUnitScriptable insertRow(final Object index) {
107108
}
108109

109110
if (r < 0 || r > rowCount) {
110-
throw JavaScriptEngine.reportRuntimeError("Index or size is negative or greater than the allowed amount "
111-
+ "(index: " + rowIndex + ", " + rowCount + " rows)");
111+
throw JavaScriptEngine.asJavaScriptException(
112+
getWindow(),
113+
new DOMException(
114+
"Index or size is negative or greater than the allowed amount "
115+
+ "(index: " + rowIndex + ", " + rowCount + " rows)",
116+
DOMException.INDEX_SIZE_ERR));
112117
}
113118

114119
return insertRow(r);

0 commit comments

Comments
 (0)