1
2
3
4
5
6
7
8
9
10
11
12
13
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
31
32
33
34
35 @JsxClass
36 public final class DOMStringMap extends HtmlUnitScriptable {
37
38
39
40
41 public DOMStringMap() {
42 super();
43 }
44
45
46
47
48 @JsxConstructor
49 public void jsConstructor() {
50
51 }
52
53
54
55
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
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
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 }