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