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   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator">MDN Documentation</a>
36   */
37  @JsxClass
38  public class Collator extends HtmlUnitScriptable {
39  
40      /** Symbol.toStringTag support. */
41      @JsxSymbolConstant
42      public static final String TO_STRING_TAG = "Intl.Collator";
43  
44      /**
45       * JavaScript constructor.
46       * @param cx the current context
47       * @param scope the scope
48       * @param args the arguments to the Collator constructor
49       * @param ctorObj the function object
50       * @param inNewExpr {@code true} if invoked with the {@code new} operator
51       * @return the Java object that JavaScript can access
52       */
53      @JsxConstructor
54      public static Scriptable jsConstructor(final Context cx, final VarScope scope,
55              final Object[] args, final Function ctorObj, final boolean inNewExpr) {
56          final String[] locales;
57          if (args.length == 0) {
58              locales = new String[] {""};
59          }
60          else {
61              if (args[0] instanceof NativeArray array) {
62                  locales = new String[(int) array.getLength()];
63                  for (int i = 0; i < locales.length; i++) {
64                      locales[i] = JavaScriptEngine.toString(array.get(i));
65                  }
66              }
67              else {
68                  locales = new String[] {JavaScriptEngine.toString(args[0])};
69              }
70          }
71          final Collator format = new Collator(/*locales, window.getBrowserVersion()*/);
72          format.setParentScope(scope);
73          format.setPrototype(((FunctionObject) ctorObj).getClassPrototype());
74          return format;
75      }
76  
77      @Override
78      public Object getDefaultValue(final Class<?> hint) {
79          if (String.class.equals(hint) || hint == null) {
80              return "[object Intl.Collator]";
81          }
82          return super.getDefaultValue(hint);
83      }
84  }