Skip to content

Commit

Permalink
build the correct error
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Feb 3, 2025
1 parent 20a7f75 commit 874e470
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
17 changes: 13 additions & 4 deletions src/main/java/org/htmlunit/javascript/host/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.htmlunit.javascript.configuration.JsxSetter;
import org.htmlunit.javascript.host.css.CSSStyleDeclaration;
import org.htmlunit.javascript.host.dom.Attr;
import org.htmlunit.javascript.host.dom.DOMException;
import org.htmlunit.javascript.host.dom.DOMTokenList;
import org.htmlunit.javascript.host.dom.Node;
import org.htmlunit.javascript.host.dom.NodeList;
Expand Down Expand Up @@ -508,8 +509,12 @@ public NodeList querySelectorAll(final String selectors) {
return NodeList.staticNodeList(this, getDomNodeOrDie().querySelectorAll(selectors));
}
catch (final CSSException e) {
throw JavaScriptEngine.syntaxError("An invalid or illegal selector was specified (selector: '"
+ selectors + "' error: " + e.getMessage() + ").");
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"An invalid or illegal selector was specified (selector: '"
+ selectors + "' error: " + e.getMessage() + ").",
DOMException.SYNTAX_ERR));
}
}

Expand All @@ -528,8 +533,12 @@ public Node querySelector(final String selectors) {
return null;
}
catch (final CSSException e) {
throw JavaScriptEngine.syntaxError("An invalid or illegal selector was specified (selector: '"
+ selectors + "' error: " + e.getMessage() + ").");
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"An invalid or illegal selector was specified (selector: '"
+ selectors + "' error: " + e.getMessage() + ").",
DOMException.SYNTAX_ERR));
}
}

Expand Down
30 changes: 25 additions & 5 deletions src/main/java/org/htmlunit/javascript/host/dom/DOMTokenList.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ public static void add(final Context context, final Scriptable scope,
final String token = JavaScriptEngine.toString(arg);

if (StringUtils.isEmpty(token)) {
throw JavaScriptEngine.syntaxError("DOMTokenList: add() does not support empty tokens");
throw JavaScriptEngine.asJavaScriptException(
(HtmlUnitScriptable) getTopLevelScope(thisObj),
new DOMException(
"DOMTokenList: add() does not support empty tokens",
DOMException.SYNTAX_ERR));
}
if (StringUtils.containsAny(token, WHITESPACE_CHARS)) {
throw JavaScriptEngine.asJavaScriptException(
Expand Down Expand Up @@ -199,7 +203,11 @@ public static void remove(final Context context, final Scriptable scope,
final String token = JavaScriptEngine.toString(arg);

if (StringUtils.isEmpty(token)) {
throw JavaScriptEngine.syntaxError("DOMTokenList: remove() does not support empty tokens");
throw JavaScriptEngine.asJavaScriptException(
(HtmlUnitScriptable) getTopLevelScope(thisObj),
new DOMException(
"DOMTokenList: remove() does not support empty tokens",
DOMException.SYNTAX_ERR));
}
if (StringUtils.containsAny(token, WHITESPACE_CHARS)) {
throw JavaScriptEngine.asJavaScriptException(
Expand All @@ -225,7 +233,11 @@ public static void remove(final Context context, final Scriptable scope,
@JsxFunction
public boolean replace(final String oldToken, final String newToken) {
if (StringUtils.isEmpty(oldToken)) {
throw JavaScriptEngine.syntaxError("Empty oldToken not allowed");
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"Empty oldToken not allowed",
DOMException.SYNTAX_ERR));
}
if (StringUtils.containsAny(oldToken, WHITESPACE_CHARS)) {
throw JavaScriptEngine.asJavaScriptException(
Expand All @@ -236,7 +248,11 @@ public boolean replace(final String oldToken, final String newToken) {
}

if (StringUtils.isEmpty(newToken)) {
throw JavaScriptEngine.syntaxError("Empty newToken not allowed");
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"Empty newToken not allowed",
DOMException.SYNTAX_ERR));
}
if (StringUtils.containsAny(newToken, WHITESPACE_CHARS)) {
throw JavaScriptEngine.asJavaScriptException(
Expand Down Expand Up @@ -269,7 +285,11 @@ public boolean replace(final String oldToken, final String newToken) {
@JsxFunction
public boolean toggle(final String token) {
if (StringUtils.isEmpty(token)) {
throw JavaScriptEngine.syntaxError("DOMTokenList: toggle() does not support empty tokens");
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"DOMTokenList: toggle() does not support empty tokens",
DOMException.SYNTAX_ERR));
}
if (StringUtils.containsAny(token, WHITESPACE_CHARS)) {
throw JavaScriptEngine.asJavaScriptException(
Expand Down

0 comments on commit 874e470

Please sign in to comment.