Skip to content

Commit

Permalink
DOMTokenList - several fixes to throw the correct error
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Feb 2, 2025
1 parent 3f591ef commit 682e763
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
<body>
<release version="4.10.0" date="February xx, 2025" description="Bugfixes">
<action type="fix" dev="rbri">
CSSGroupingRule several fixes to throw the correct error.
DOMTokenList - several fixes to throw the correct error.
</action>
<action type="fix" dev="rbri">
CSSGroupingRule - several fixes to throw the correct error.
</action>
<action type="fix" dev="rbri">
HTMLInputElement value/selectionStart/selectionEnd properties throws the correct error (InvalidStateError).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ public int addRule(final String selector, final String rule) {
refreshCssRules();
}
catch (final DOMException ex) {
throw JavaScriptEngine.throwAsScriptRuntimeEx(ex);
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new org.htmlunit.javascript.host.dom.DOMException(
ex.getMessage(),
ex.code));
}
}
return -1;
Expand Down
43 changes: 30 additions & 13 deletions src/main/java/org/htmlunit/javascript/host/dom/DOMTokenList.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,14 @@ public static void add(final Context context, final Scriptable scope,
final String token = JavaScriptEngine.toString(arg);

if (StringUtils.isEmpty(token)) {
throw JavaScriptEngine.reportRuntimeError("DOMTokenList: add() does not support empty tokens");
throw JavaScriptEngine.syntaxError("DOMTokenList: add() does not support empty tokens");
}
if (StringUtils.containsAny(token, WHITESPACE_CHARS)) {
throw JavaScriptEngine.reportRuntimeError(
"DOMTokenList: add() does not support whitespace chars in tokens");
throw JavaScriptEngine.asJavaScriptException(
(HtmlUnitScriptable) getTopLevelScope(thisObj),
new DOMException(
"DOMTokenList: add() does not support whitespace chars in tokens",
DOMException.INVALID_CHARACTER_ERR));
}

if (!parts.contains(token)) {
Expand Down Expand Up @@ -196,11 +199,14 @@ public static void remove(final Context context, final Scriptable scope,
final String token = JavaScriptEngine.toString(arg);

if (StringUtils.isEmpty(token)) {
throw JavaScriptEngine.reportRuntimeError("DOMTokenList: remove() does not support empty tokens");
throw JavaScriptEngine.syntaxError("DOMTokenList: remove() does not support empty tokens");
}
if (StringUtils.containsAny(token, WHITESPACE_CHARS)) {
throw JavaScriptEngine.reportRuntimeError(
"DOMTokenList: remove() does not support whitespace chars in tokens");
throw JavaScriptEngine.asJavaScriptException(
(HtmlUnitScriptable) getTopLevelScope(thisObj),
new DOMException(
"DOMTokenList: remove() does not support whitespace chars in tokens",
DOMException.INVALID_CHARACTER_ERR));
}

parts.remove(token);
Expand All @@ -219,17 +225,25 @@ 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.reportRuntimeError("Empty oldToken not allowed");
throw JavaScriptEngine.syntaxError("Empty oldToken not allowed");
}
if (StringUtils.containsAny(oldToken, WHITESPACE_CHARS)) {
throw JavaScriptEngine.reportRuntimeError("oldToken contains whitespace");
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"DOMTokenList: replace() oldToken contains whitespace",
DOMException.INVALID_CHARACTER_ERR));
}

if (StringUtils.isEmpty(newToken)) {
throw JavaScriptEngine.reportRuntimeError("Empty newToken not allowed");
throw JavaScriptEngine.syntaxError("Empty newToken not allowed");
}
if (StringUtils.containsAny(newToken, WHITESPACE_CHARS)) {
throw JavaScriptEngine.reportRuntimeError("newToken contains whitespace");
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"DOMTokenList: replace() newToken contains whitespace",
DOMException.INVALID_CHARACTER_ERR));
}

final String value = getValue();
Expand All @@ -255,11 +269,14 @@ public boolean replace(final String oldToken, final String newToken) {
@JsxFunction
public boolean toggle(final String token) {
if (StringUtils.isEmpty(token)) {
throw JavaScriptEngine.reportRuntimeError("DOMTokenList: toggle() does not support empty tokens");
throw JavaScriptEngine.syntaxError("DOMTokenList: toggle() does not support empty tokens");
}
if (StringUtils.containsAny(token, WHITESPACE_CHARS)) {
throw JavaScriptEngine.reportRuntimeError(
"DOMTokenList: toggle() does not support whitespace chars in tokens");
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"DOMTokenList: toggle() does not support whitespace chars in tokens",
DOMException.INVALID_CHARACTER_ERR));
}

final List<String> parts = split(getValue());
Expand Down

0 comments on commit 682e763

Please sign in to comment.