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 org.htmlunit.html.DomAttr;
18 import org.htmlunit.html.DomElement;
19 import org.htmlunit.javascript.HtmlUnitScriptable;
20 import org.htmlunit.javascript.configuration.JsxClass;
21 import org.htmlunit.javascript.configuration.JsxConstructor;
22 import org.htmlunit.javascript.configuration.JsxGetter;
23 import org.htmlunit.javascript.configuration.JsxSetter;
24
25 /**
26 * A JavaScript object for {@code Attr}.
27 *
28 * @see <a href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-63764602">W3C DOM Level 2</a>
29 * @see <a href="http://msdn.microsoft.com/en-us/library/ms535187.aspx">MSDN documentation</a>
30 * @author Daniel Gredler
31 * @author Chris Erskine
32 * @author Ahmed Ashour
33 * @author Sudhan Moghe
34 * @author Ronald Brill
35 * @author Frank Danek
36 */
37 @JsxClass(domClass = DomAttr.class)
38 public class Attr extends Node {
39
40 /**
41 * JavaScript constructor.
42 */
43 @Override
44 @JsxConstructor
45 public void jsConstructor() {
46 super.jsConstructor();
47 }
48
49 /**
50 * Detaches this attribute from the parent HTML element after caching the attribute value.
51 */
52 public void detachFromParent() {
53 final DomAttr domNode = getDomNodeOrDie();
54 final DomElement parent = (DomElement) domNode.getParentNode();
55 if (parent != null) {
56 domNode.setValue(parent.getAttribute(getName()));
57 }
58 domNode.remove();
59 }
60
61 /**
62 * Returns the name of the attribute.
63 * @return the name of the attribute
64 */
65 @JsxGetter
66 public String getName() {
67 return getDomNodeOrDie().getName();
68 }
69
70 /**
71 * Returns the value of this attribute.
72 * @return the value of this attribute
73 */
74 @Override
75 public String getNodeValue() {
76 return getValue();
77 }
78
79 /**
80 * Returns the owner element.
81 * @return the owner element
82 */
83 @JsxGetter
84 public HtmlUnitScriptable getOwnerElement() {
85 final DomElement parent = getDomNodeOrDie().getOwnerElement();
86 if (parent != null) {
87 return parent.getScriptableObject();
88 }
89 return null;
90 }
91
92 /**
93 * {@inheritDoc}
94 * @return {@code null}
95 */
96 @Override
97 public Node getParentNode() {
98 return null;
99 }
100
101 /**
102 * Returns {@code true} if this attribute has been specified.
103 * @return {@code true} if this attribute has been specified
104 */
105 @JsxGetter
106 public boolean isSpecified() {
107 return getDomNodeOrDie().getSpecified();
108 }
109
110 /**
111 * Returns the value of this attribute.
112 * @return the value of this attribute
113 */
114 @JsxGetter
115 public String getValue() {
116 return getDomNodeOrDie().getValue();
117 }
118
119 /**
120 * Sets the value of this attribute.
121 * @param value the new value of this attribute
122 */
123 @JsxSetter
124 public void setValue(final String value) {
125 getDomNodeOrDie().setValue(value);
126 }
127
128 /**
129 * {@inheritDoc}
130 */
131 @Override
132 public Node getFirstChild() {
133 return getLastChild();
134 }
135
136 /**
137 * {@inheritDoc}
138 */
139 @Override
140 public Node getLastChild() {
141 return null;
142 }
143
144 /**
145 * {@inheritDoc}
146 */
147 @Override
148 public DomAttr getDomNodeOrDie() {
149 return (DomAttr) super.getDomNodeOrDie();
150 }
151
152 /**
153 * {@inheritDoc}
154 */
155 @Override
156 @JsxGetter
157 public String getPrefix() {
158 return super.getPrefix();
159 }
160
161 /**
162 * {@inheritDoc}
163 */
164 @Override
165 @JsxGetter
166 public String getLocalName() {
167 return super.getLocalName();
168 }
169
170 /**
171 * {@inheritDoc}
172 */
173 @Override
174 @JsxGetter
175 public String getNamespaceURI() {
176 return super.getNamespaceURI();
177 }
178
179 /**
180 * Returns the owner document.
181 * @return the document
182 */
183 @Override
184 public Node getRootNode() {
185 return this;
186 }
187 }