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.dom;
16  
17  import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
18  
19  import org.htmlunit.corejs.javascript.Scriptable;
20  import org.htmlunit.corejs.javascript.ScriptableObject;
21  import org.htmlunit.html.HtmlElement;
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.host.Window;
27  import org.htmlunit.util.StringUtils;
28  
29  /**
30   * A JavaScript object for {@code DOMStringMap}.
31   *
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   */
35  @JsxClass
36  public final class DOMStringMap extends HtmlUnitScriptable {
37  
38      /**
39       * Creates an instance.
40       */
41      public DOMStringMap() {
42          super();
43      }
44  
45      /**
46       * JavaScript constructor.
47       */
48      @JsxConstructor
49      public void jsConstructor() {
50          // nothing to do
51      }
52  
53      /**
54       * Creates an instance.
55       * @param node the node which contains the underlying string
56       */
57      public DOMStringMap(final Node node) {
58          super();
59          setDomNode(node.getDomNodeOrDie(), false);
60          setParentScope(node.getParentScope());
61          setPrototype(getPrototype(getClass()));
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public Object get(final String name, final Scriptable start) {
69          final HtmlElement e = (HtmlElement) getDomNodeOrNull();
70          if (e != null) {
71              final String value = e.getAttribute("data-" + StringUtils.cssDeCamelize(name));
72              if (ATTRIBUTE_NOT_DEFINED != value) {
73                  return value;
74              }
75          }
76          return NOT_FOUND;
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public void put(final String name, final Scriptable start, final Object value) {
84          if (!(ScriptableObject.getTopLevelScope(this) instanceof Window) || getWindow().getWebWindow() == null) {
85              super.put(name, start, value);
86          }
87          else {
88              final HtmlElement e = (HtmlElement) getDomNodeOrNull();
89              if (e != null) {
90                  e.setAttribute("data-" + StringUtils.cssDeCamelize(name), JavaScriptEngine.toString(value));
91              }
92          }
93      }
94  }