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.intl;
16  
17  import static org.htmlunit.BrowserVersionFeatures.JS_INTL_V8_BREAK_ITERATOR;
18  
19  import org.htmlunit.BrowserVersion;
20  import org.htmlunit.corejs.javascript.FunctionObject;
21  import org.htmlunit.corejs.javascript.ScriptableObject;
22  import org.htmlunit.javascript.HtmlUnitScriptable;
23  import org.htmlunit.javascript.JavaScriptEngine;
24  import org.htmlunit.javascript.RecursiveFunctionObject;
25  import org.htmlunit.javascript.configuration.AbstractJavaScriptConfiguration;
26  import org.htmlunit.javascript.configuration.ClassConfiguration;
27  
28  /**
29   * A JavaScript object for {@code Intl}.
30   *
31   * @author Ahmed Ashour
32   */
33  public class Intl extends HtmlUnitScriptable {
34  
35      /**
36       * Define needed properties.
37       * @param browserVersion the browser version
38       */
39      public void defineProperties(final BrowserVersion browserVersion) {
40          define(Collator.class, browserVersion);
41          define(DateTimeFormat.class, browserVersion);
42          define(NumberFormat.class, browserVersion);
43          if (browserVersion.hasFeature(JS_INTL_V8_BREAK_ITERATOR)) {
44              define(V8BreakIterator.class, browserVersion);
45          }
46      }
47  
48      private void define(final Class<? extends HtmlUnitScriptable> c, final BrowserVersion browserVersion) {
49          try {
50              final ClassConfiguration config = AbstractJavaScriptConfiguration.getClassConfiguration(c, browserVersion);
51              final HtmlUnitScriptable prototype = JavaScriptEngine.configureClass(config, this);
52              final FunctionObject functionObject =
53                      new RecursiveFunctionObject(config.getJsConstructor().getKey(),
54                              config.getJsConstructor().getValue(), this, browserVersion);
55              functionObject.addAsConstructor(this, prototype, ScriptableObject.DONTENUM);
56          }
57          catch (final Exception e) {
58              throw JavaScriptEngine.throwAsScriptRuntimeEx(e);
59          }
60      }
61  }