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.intl;
16  
17  import org.htmlunit.corejs.javascript.Context;
18  import org.htmlunit.corejs.javascript.Function;
19  import org.htmlunit.corejs.javascript.FunctionObject;
20  import org.htmlunit.corejs.javascript.NativeArray;
21  import org.htmlunit.corejs.javascript.Scriptable;
22  import org.htmlunit.corejs.javascript.VarScope;
23  import org.htmlunit.javascript.HtmlUnitScriptable;
24  import org.htmlunit.javascript.JavaScriptEngine;
25  import org.htmlunit.javascript.configuration.JsxClass;
26  import org.htmlunit.javascript.configuration.JsxConstructor;
27  import org.htmlunit.javascript.configuration.JsxSymbolConstant;
28  
29  /**
30   * A JavaScript object for Intl.Collator.
31   *
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   */
35  @JsxClass
36  public class Collator extends HtmlUnitScriptable {
37  
38      /** Symbol.toStringTag support. */
39      @JsxSymbolConstant
40      public static final String TO_STRING_TAG = "Intl.Collator";
41  
42      /**
43       * JavaScript constructor.
44       * @param cx the current context
45       * @param scope the scope
46       * @param args the arguments to the WebSocket constructor
47       * @param ctorObj the function object
48       * @param inNewExpr Is new or not
49       * @return the java object to allow JavaScript to access
50       */
51      @JsxConstructor
52      public static Scriptable jsConstructor(final Context cx, final VarScope scope,
53              final Object[] args, final Function ctorObj, final boolean inNewExpr) {
54          final String[] locales;
55          if (args.length == 0) {
56              locales = new String[] {""};
57          }
58          else {
59              if (args[0] instanceof NativeArray array) {
60                  locales = new String[(int) array.getLength()];
61                  for (int i = 0; i < locales.length; i++) {
62                      locales[i] = JavaScriptEngine.toString(array.get(i));
63                  }
64              }
65              else {
66                  locales = new String[] {JavaScriptEngine.toString(args[0])};
67              }
68          }
69          final Collator format = new Collator(/*locales, window.getBrowserVersion()*/);
70          format.setParentScope(scope);
71          format.setPrototype(((FunctionObject) ctorObj).getClassPrototype());
72          return format;
73      }
74  
75      @Override
76      public Object getDefaultValue(final Class<?> hint) {
77          if (String.class.equals(hint) || hint == null) {
78              return "[object Intl.Collator]";
79          }
80          return super.getDefaultValue(hint);
81      }
82  }