View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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   * A JavaScript object for {@code TextEncoder}.
31   *
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   */
35  @JsxClass
36  public class TextEncoder extends HtmlUnitScriptable {
37  
38      /**
39       * JavaScript constructor.
40       */
41      @JsxConstructor
42      public void jsConstructor() {
43          // nothing to do
44      }
45  
46      /**
47       * @return always "utf-8"
48       */
49      @JsxGetter
50      public String getEncoding() {
51          return "utf-8";
52      }
53  
54      /**
55       * @param toEncode the string to encode
56       * @return returns a Uint8Array containing the text given encoded .
57       */
58      @JsxFunction
59      public NativeUint8Array encode(final Object toEncode) {
60          if (JavaScriptEngine.isUndefined(toEncode)) {
61              final NativeUint8Array result = new NativeUint8Array(0);
62              result.setParentScope(getParentScope());
63              result.setPrototype(ScriptableObject.getClassPrototype(getWindow(this), result.getClassName()));
64              return result;
65          }
66  
67          final String txt;
68          if (toEncode == null) {
69              txt = "null";
70          }
71          else {
72              txt = JavaScriptEngine.toString(toEncode);
73          }
74  
75          final byte[] bytes = txt.getBytes(StandardCharsets.UTF_8);
76  
77          final NativeArrayBuffer arrayBuffer = new NativeArrayBuffer(bytes.length);
78          System.arraycopy(bytes, 0, arrayBuffer.getBuffer(), 0, bytes.length);
79  
80          final NativeUint8Array result = new NativeUint8Array(arrayBuffer, 0, bytes.length);
81          result.setParentScope(getParentScope());
82          result.setPrototype(ScriptableObject.getClassPrototype(getWindow(this), result.getClassName()));
83          return result;
84      }
85  }