Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Feb 4, 2025
1 parent 1bfac54 commit 54332dc
Show file tree
Hide file tree
Showing 21 changed files with 153 additions and 235 deletions.
16 changes: 9 additions & 7 deletions src/main/java/org/htmlunit/javascript/JavaScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -1191,21 +1191,23 @@ public static EcmaError constructError(final String error, final String message)
/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
*
* Encapsulates the given {@link DOMException} into a Rhino-compatible exception.
* Creates a {@link DOMException} and encapsulates it into a Rhino-compatible exception.
*
* @param scope the parent scope
* @param exception the exception to encapsulate
* @param message the exception message
* @param type the exception type
* @return the created exception
*/
public static RhinoException asJavaScriptException(final HtmlUnitScriptable scope, final DOMException exception) {
exception.setParentScope(scope);
exception.setPrototype(scope.getPrototype(exception.getClass()));
public static RhinoException asJavaScriptException(final HtmlUnitScriptable scope, final String message, final int type) {
final DOMException domException = new DOMException(message, type);
domException.setParentScope(scope);
domException.setPrototype(scope.getPrototype(DOMException.class));

final EcmaError helper = ScriptRuntime.syntaxError("helper");
final String fileName = helper.sourceName().replaceFirst("script in (.*) from .*", "$1");
exception.setLocation(fileName, helper.lineNumber());
domException.setLocation(fileName, helper.lineNumber());

return new JavaScriptException(exception, fileName, helper.lineNumber());
return new JavaScriptException(domException, fileName, helper.lineNumber());
}

/**
Expand Down
26 changes: 11 additions & 15 deletions src/main/java/org/htmlunit/javascript/host/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,9 @@ public NodeList querySelectorAll(final String selectors) {
catch (final CSSException e) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"An invalid or illegal selector was specified (selector: '"
+ selectors + "' error: " + e.getMessage() + ").",
DOMException.SYNTAX_ERR));
"An invalid or illegal selector was specified (selector: '"
+ selectors + "' error: " + e.getMessage() + ").",
DOMException.SYNTAX_ERR);
}
}

Expand All @@ -535,10 +534,9 @@ public Node querySelector(final String selectors) {
catch (final CSSException e) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"An invalid or illegal selector was specified (selector: '"
+ selectors + "' error: " + e.getMessage() + ").",
DOMException.SYNTAX_ERR));
"An invalid or illegal selector was specified (selector: '"
+ selectors + "' error: " + e.getMessage() + ").",
DOMException.SYNTAX_ERR);
}
}

Expand Down Expand Up @@ -940,9 +938,8 @@ public void setOuterHTML(final Object value) {
if (getBrowserVersion().hasFeature(JS_OUTER_HTML_THROWS_FOR_DETACHED)) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new org.htmlunit.javascript.host.dom.DOMException(
"outerHTML is readonly for detached nodes",
org.htmlunit.javascript.host.dom.DOMException.NO_MODIFICATION_ALLOWED_ERR));
"outerHTML is readonly for detached nodes",
DOMException.NO_MODIFICATION_ALLOWED_ERR);
}
return;
}
Expand Down Expand Up @@ -1537,10 +1534,9 @@ public static boolean matches(final Context context, final Scriptable scope,
catch (final CSSException e) {
throw JavaScriptEngine.asJavaScriptException(
(HtmlUnitScriptable) getTopLevelScope(thisObj),
new DOMException(
"An invalid or illegal selector was specified (selector: '"
+ selectorString + "' error: " + e.getMessage() + ").",
DOMException.SYNTAX_ERR));
"An invalid or illegal selector was specified (selector: '"
+ selectorString + "' error: " + e.getMessage() + ").",
DOMException.SYNTAX_ERR);
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/htmlunit/javascript/host/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -1986,10 +1986,9 @@ public static void postMessage(final Context context, final Scriptable scope,
catch (final Exception e) {
throw JavaScriptEngine.asJavaScriptException(
(HtmlUnitScriptable) getTopLevelScope(thisObj),
new DOMException(
"Failed to execute 'postMessage' on 'Window': Invalid target origin '"
+ targetOrigin + "' was specified (reason: " + e.getMessage() + ".",
DOMException.SYNTAX_ERR));
"Failed to execute 'postMessage' on 'Window': Invalid target origin '"
+ targetOrigin + "' was specified (reason: " + e.getMessage() + ".",
DOMException.SYNTAX_ERR);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ public static String atob(final String encodedData, final HtmlUnitScriptable scr
if (encodedData.charAt(i) > 255) {
throw JavaScriptEngine.asJavaScriptException(
scriptable,
new org.htmlunit.javascript.host.dom.DOMException(
"Function atob supports only latin1 characters",
org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR));
"Function atob supports only latin1 characters",
org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR);
}
}
final byte[] bytes = encodedData.getBytes(StandardCharsets.ISO_8859_1);
Expand All @@ -83,9 +82,8 @@ public static String btoa(final String stringToEncode, final HtmlUnitScriptable
if (stringToEncode.charAt(i) > 255) {
throw JavaScriptEngine.asJavaScriptException(
scriptable,
new org.htmlunit.javascript.host.dom.DOMException(
"Function btoa supports only latin1 characters",
org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR));
"Function btoa supports only latin1 characters",
org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR);
}
}
final byte[] bytes = stringToEncode.getBytes(StandardCharsets.ISO_8859_1);
Expand Down
25 changes: 10 additions & 15 deletions src/main/java/org/htmlunit/javascript/host/canvas/ImageData.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ public static ImageData jsConstructor(final Context cx, final Scriptable scope,
if (data.getArrayLength() % 4 != 0) {
throw JavaScriptEngine.asJavaScriptException(
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
new DOMException(
"ImageData ctor - data length mod 4 not zero",
DOMException.INVALID_STATE_ERR));
"ImageData ctor - data length mod 4 not zero",
DOMException.INVALID_STATE_ERR);
}

width = (int) JavaScriptEngine.toInteger(args[1]);
Expand All @@ -85,9 +84,8 @@ public static ImageData jsConstructor(final Context cx, final Scriptable scope,
if (data.getArrayLength() != 4 * width * height) {
throw JavaScriptEngine.asJavaScriptException(
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
new DOMException(
"ImageData ctor - width not correct",
DOMException.INDEX_SIZE_ERR));
"ImageData ctor - width not correct",
DOMException.INDEX_SIZE_ERR);
}
}
else {
Expand All @@ -97,9 +95,8 @@ public static ImageData jsConstructor(final Context cx, final Scriptable scope,
if (data.getArrayLength() != 4 * width * height) {
throw JavaScriptEngine.asJavaScriptException(
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
new DOMException(
"ImageData ctor - width/height not correct",
DOMException.INDEX_SIZE_ERR));
"ImageData ctor - width/height not correct",
DOMException.INDEX_SIZE_ERR);
}
}
else {
Expand All @@ -110,16 +107,14 @@ public static ImageData jsConstructor(final Context cx, final Scriptable scope,
if (width < 0) {
throw JavaScriptEngine.asJavaScriptException(
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
new DOMException(
"ImageData ctor - width negative",
DOMException.INDEX_SIZE_ERR));
"ImageData ctor - width negative",
DOMException.INDEX_SIZE_ERR);
}
if (height < 0) {
throw JavaScriptEngine.asJavaScriptException(
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
new DOMException(
"ImageData ctor - height negative",
DOMException.INDEX_SIZE_ERR));
"ImageData ctor - height negative",
DOMException.INDEX_SIZE_ERR);
}

final ImageData result = new ImageData(null, 0, 0, width, height);
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/htmlunit/javascript/host/crypto/Crypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ public NativeTypedArrayView<?> getRandomValues(final NativeTypedArrayView<?> arr
if (array.getByteLength() > 65_536) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(
"Error: Failed to execute 'getRandomValues' on 'Crypto': "
+ "The ArrayBufferView's byte length "
+ "(" + array.getByteLength() + ") exceeds the number of bytes "
+ "of entropy available via this API (65536).",
DOMException.QUOTA_EXCEEDED_ERR));
"Error: Failed to execute 'getRandomValues' on 'Crypto': "
+ "The ArrayBufferView's byte length "
+ "(" + array.getByteLength() + ") exceeds the number of bytes "
+ "of entropy available via this API (65536).",
DOMException.QUOTA_EXCEEDED_ERR);
}

for (int i = 0; i < array.getByteLength() / array.getBytesPerElement(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,13 @@ else if (JavaScriptEngine.isUndefined(position)) {
}
catch (final DOMException ex) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new org.htmlunit.javascript.host.dom.DOMException(
ex.getMessage(),
ex.code));
getWindow(), ex.getMessage(), ex.code);
}
}
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new org.htmlunit.javascript.host.dom.DOMException(
e.getMessage(),
org.htmlunit.javascript.host.dom.DOMException.SYNTAX_ERR));
e.getMessage(),
org.htmlunit.javascript.host.dom.DOMException.SYNTAX_ERR);
}
}

Expand All @@ -141,11 +137,7 @@ public void deleteRule(final int position) {
refreshCssRules();
}
catch (final DOMException e) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new org.htmlunit.javascript.host.dom.DOMException(
e.getMessage(),
e.code));
throw JavaScriptEngine.asJavaScriptException(getWindow(), e.getMessage(), e.code);
}
}

Expand Down
24 changes: 4 additions & 20 deletions src/main/java/org/htmlunit/javascript/host/css/CSSStyleSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,10 @@ public int insertRule(final String rule, final int position) {
return position;
}
catch (final DOMException ex) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new org.htmlunit.javascript.host.dom.DOMException(
ex.getMessage(),
ex.code));
throw JavaScriptEngine.asJavaScriptException(getWindow(), ex.getMessage(), ex.code);
}
}
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new org.htmlunit.javascript.host.dom.DOMException(
e.getMessage(),
e.code));
throw JavaScriptEngine.asJavaScriptException(getWindow(), e.getMessage(), e.code);
}
}

Expand Down Expand Up @@ -255,11 +247,7 @@ public void deleteRule(final int position) {
refreshCssRules();
}
catch (final DOMException e) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new org.htmlunit.javascript.host.dom.DOMException(
e.getMessage(),
e.code));
throw JavaScriptEngine.asJavaScriptException(getWindow(), e.getMessage(), e.code);
}
}

Expand Down Expand Up @@ -288,11 +276,7 @@ public int addRule(final String selector, final String rule) {
refreshCssRules();
}
catch (final DOMException ex) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new org.htmlunit.javascript.host.dom.DOMException(
ex.getMessage(),
ex.code));
throw JavaScriptEngine.asJavaScriptException(getWindow(), ex.getMessage(), ex.code);
}
}
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,16 @@ public void deleteData(final int offset, final int count) {
if (offset < 0) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new org.htmlunit.javascript.host.dom.DOMException(
"Provided offset: " + offset + " is less than zero.",
org.htmlunit.javascript.host.dom.DOMException.INDEX_SIZE_ERR));
"Provided offset: " + offset + " is less than zero.",
org.htmlunit.javascript.host.dom.DOMException.INDEX_SIZE_ERR);
}

final DomCharacterData domCharacterData = getDomCharacterDataOrDie();
if (offset > domCharacterData.getLength()) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new org.htmlunit.javascript.host.dom.DOMException(
"Provided offset: " + offset + " is greater than length.",
org.htmlunit.javascript.host.dom.DOMException.INDEX_SIZE_ERR));
"Provided offset: " + offset + " is greater than length.",
org.htmlunit.javascript.host.dom.DOMException.INDEX_SIZE_ERR);
}

domCharacterData.deleteData(offset, count);
Expand Down
Loading

0 comments on commit 54332dc

Please sign in to comment.