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