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.dom;
16  
17  import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
18  
19  import org.htmlunit.corejs.javascript.Scriptable;
20  import org.htmlunit.html.HtmlElement;
21  import org.htmlunit.javascript.HtmlUnitScriptable;
22  import org.htmlunit.javascript.JavaScriptEngine;
23  import org.htmlunit.javascript.configuration.JsxClass;
24  import org.htmlunit.javascript.configuration.JsxConstructor;
25  
26  /**
27   * A JavaScript object for {@code DOMStringMap}.
28   *
29   * @author Ahmed Ashour
30   * @author Ronald Brill
31   *
32   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMStringMap">MDN Documentation</a>
33   */
34  @JsxClass
35  public final class DOMStringMap extends HtmlUnitScriptable {
36  
37      private static final String DATA_PREFIX = "data-";
38  
39      /**
40       * Creates an instance.
41       */
42      public DOMStringMap() {
43          super();
44      }
45  
46      /**
47       * JavaScript constructor.
48       */
49      @JsxConstructor
50      public void jsConstructor() {
51          // nothing to do
52      }
53  
54      /**
55       * Creates an instance.
56       * @param node the node which contains the underlying string
57       */
58      public DOMStringMap(final Node node) {
59          super();
60          setDomNode(node.getDomNodeOrDie(), false);
61          setParentScope(node.getParentScope());
62          setPrototype(getPrototype(getClass()));
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public Object get(final String name, final Scriptable start) {
70          final HtmlElement e = (HtmlElement) getDomNodeOrNull();
71          if (e != null) {
72              final String value = e.getAttribute(DATA_PREFIX + deCamelize(name));
73              if (ATTRIBUTE_NOT_DEFINED != value) {
74                  return value;
75              }
76          }
77          return super.get(name, start);
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public void put(final String name, final Scriptable start, final Object value) {
85          final HtmlElement e = (HtmlElement) getDomNodeOrNull();
86          if (e == null) {
87              super.put(name, start, value);
88          }
89          else {
90              e.setAttribute("data-" + deCamelize(name), JavaScriptEngine.toString(value));
91          }
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public void delete(final String name) {
99          final HtmlElement e = (HtmlElement) getDomNodeOrNull();
100         if (e == null) {
101             super.delete(name);
102         }
103         else {
104             e.removeAttribute("data-" + deCamelize(name));
105         }
106     }
107 
108     /**
109      * Transforms the specified string from camel-cased to dash separated.
110      *
111      * @param string the string to decamelize
112      * @return the transformed string
113      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset#name_conversion">
114      *      MDN - HTMLElement.dataset - Name conversion</a>
115      */
116     private static String deCamelize(final String string) {
117         if (string == null || string.isEmpty()) {
118             return string;
119         }
120 
121         final StringBuilder builder = new StringBuilder();
122         for (int i = 0; i < string.length(); i++) {
123             final char ch = string.charAt(i);
124             if (Character.isUpperCase(ch)) {
125                 builder.append('-').append(Character.toLowerCase(ch));
126             }
127             else {
128                 builder.append(ch);
129             }
130         }
131         return builder.toString();
132     }
133 
134 }