Skip to content

Commit

Permalink
Window.atob()/btoa() throws an InvalidCharacterError
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Feb 2, 2025
1 parent 17eb7c8 commit 889f24e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<body>
<release version="4.10.0" date="February xx, 2025" description="Bugfixes">
<action type="fix" dev="rbri">
Window.atob()/btoa() throws an InvalidCharacterError.
</action>
<action type="fix" dev="rbri">
DomNode.appendChild() throws a HierarchyRequstError.
</action>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/htmlunit/javascript/host/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void alert(final Object message) {
@JsxFunction
@Override
public String btoa(final String stringToEncode) {
return WindowOrWorkerGlobalScopeMixin.btoa(stringToEncode);
return WindowOrWorkerGlobalScopeMixin.btoa(stringToEncode, this);
}

/**
Expand All @@ -301,7 +301,7 @@ public String btoa(final String stringToEncode) {
@JsxFunction
@Override
public String atob(final String encodedData) {
return WindowOrWorkerGlobalScopeMixin.atob(encodedData);
return WindowOrWorkerGlobalScopeMixin.atob(encodedData, this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import org.htmlunit.Page;
import org.htmlunit.WebWindow;
import org.htmlunit.corejs.javascript.Context;
import org.htmlunit.corejs.javascript.EvaluatorException;
import org.htmlunit.corejs.javascript.Function;
import org.htmlunit.corejs.javascript.FunctionObject;
import org.htmlunit.corejs.javascript.Scriptable;
import org.htmlunit.javascript.HtmlUnitScriptable;
import org.htmlunit.javascript.JavaScriptEngine;
import org.htmlunit.javascript.background.BackgroundJavaScriptFactory;
import org.htmlunit.javascript.background.JavaScriptJob;
Expand Down Expand Up @@ -53,13 +53,18 @@ private WindowOrWorkerGlobalScopeMixin() {
/**
* Decodes a string of data which has been encoded using base-64 encoding.
* @param encodedData the encoded string
* @param scriptable the HtmlUnitScriptable scope
* @return the decoded value
*/
public static String atob(final String encodedData) {
public static String atob(final String encodedData, final HtmlUnitScriptable scriptable) {
final int l = encodedData.length();
for (int i = 0; i < l; i++) {
if (encodedData.charAt(i) > 255) {
throw new EvaluatorException("Function atob supports only latin1 characters");
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));
}
}
final byte[] bytes = encodedData.getBytes(StandardCharsets.ISO_8859_1);
Expand All @@ -69,13 +74,18 @@ public static String atob(final String encodedData) {
/**
* Creates a base-64 encoded ASCII string from a string of binary data.
* @param stringToEncode string to encode
* @param scriptable the HtmlUnitScriptable scope
* @return the encoded string
*/
public static String btoa(final String stringToEncode) {
public static String btoa(final String stringToEncode, final HtmlUnitScriptable scriptable) {
final int l = stringToEncode.length();
for (int i = 0; i < l; i++) {
if (stringToEncode.charAt(i) > 255) {
throw new EvaluatorException("Function btoa supports only latin1 characters");
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));
}
}
final byte[] bytes = stringToEncode.getBytes(StandardCharsets.ISO_8859_1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void jsConstructor() {
@JsxFunction
@Override
public String btoa(final String stringToEncode) {
return WindowOrWorkerGlobalScopeMixin.btoa(stringToEncode);
return WindowOrWorkerGlobalScopeMixin.btoa(stringToEncode, this);
}

/**
Expand All @@ -65,6 +65,6 @@ public String btoa(final String stringToEncode) {
@JsxFunction
@Override
public String atob(final String encodedData) {
return WindowOrWorkerGlobalScopeMixin.atob(encodedData);
return WindowOrWorkerGlobalScopeMixin.atob(encodedData, this);
}
}

0 comments on commit 889f24e

Please sign in to comment.