View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host;
16  
17  import java.nio.charset.StandardCharsets;
18  
19  import org.htmlunit.corejs.javascript.ScriptableObject;
20  import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer;
21  import org.htmlunit.corejs.javascript.typedarrays.NativeUint8Array;
22  import org.htmlunit.javascript.HtmlUnitScriptable;
23  import org.htmlunit.javascript.JavaScriptEngine;
24  import org.htmlunit.javascript.configuration.JsxClass;
25  import org.htmlunit.javascript.configuration.JsxConstructor;
26  import org.htmlunit.javascript.configuration.JsxFunction;
27  import org.htmlunit.javascript.configuration.JsxGetter;
28  
29  /**
30   * JavaScript host object for {@code TextEncoder}.
31   *
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   *
35   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder">MDN Documentation</a>
36   */
37  @JsxClass
38  public class TextEncoder extends HtmlUnitScriptable {
39  
40      /**
41       * Creates an instance of this object.
42       */
43      @JsxConstructor
44      public void jsConstructor() {
45          // nothing to do
46      }
47  
48      /**
49       * Returns the encoding used by this encoder, which is always {@code "utf-8"}.
50       *
51       * @return {@code "utf-8"}
52       */
53      @JsxGetter
54      public String getEncoding() {
55          return "utf-8";
56      }
57  
58      /**
59       * Encodes the given string as a {@code Uint8Array} using UTF-8 encoding.
60       *
61       * @param toEncode the string to encode
62       * @return a {@code Uint8Array} containing the UTF-8 encoded bytes
63       */
64      @JsxFunction
65      public NativeUint8Array encode(final Object toEncode) {
66          if (JavaScriptEngine.isUndefined(toEncode)) {
67              final NativeUint8Array result = new NativeUint8Array(0);
68              result.setParentScope(getParentScope());
69              result.setPrototype(ScriptableObject.getClassPrototype(getParentScope(), result.getClassName()));
70              return result;
71          }
72  
73          final String txt;
74          if (toEncode == null) {
75              txt = "null";
76          }
77          else {
78              txt = JavaScriptEngine.toString(toEncode);
79          }
80  
81          final byte[] bytes = txt.getBytes(StandardCharsets.UTF_8);
82  
83          final NativeArrayBuffer arrayBuffer = new NativeArrayBuffer(bytes.length);
84          System.arraycopy(bytes, 0, arrayBuffer.getBuffer(), 0, bytes.length);
85  
86          final NativeUint8Array result = new NativeUint8Array(arrayBuffer, 0, bytes.length);
87          result.setParentScope(getParentScope());
88          result.setPrototype(ScriptableObject.getClassPrototype(getParentScope(), result.getClassName()));
89          return result;
90      }
91  }