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.html;
16  
17  import static org.htmlunit.BrowserVersionFeatures.JS_AREA_WITHOUT_HREF_FOCUSABLE;
18  import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
19  
20  import org.htmlunit.html.HtmlArea;
21  import org.htmlunit.html.HtmlElement;
22  import org.htmlunit.javascript.configuration.JsxClass;
23  import org.htmlunit.javascript.configuration.JsxConstructor;
24  import org.htmlunit.javascript.configuration.JsxGetter;
25  import org.htmlunit.javascript.configuration.JsxSetter;
26  import org.htmlunit.javascript.host.dom.DOMTokenList;
27  
28  /**
29   * The JavaScript object {@code HTMLAreaElement}.
30   *
31   * @author Ahmed Ashour
32   * @author Ronald Brill
33   * @author Frank Danek
34   */
35  @JsxClass(domClass = HtmlArea.class)
36  public class HTMLAreaElement extends HTMLElement {
37  
38      /**
39       * JavaScript constructor.
40       */
41      @Override
42      @JsxConstructor
43      public void jsConstructor() {
44          super.jsConstructor();
45      }
46  
47      /**
48       * Calls for instance for implicit conversion to string.
49       * @see org.htmlunit.javascript.HtmlUnitScriptable#getDefaultValue(java.lang.Class)
50       * @param hint the type hint
51       * @return the default value
52       */
53      @Override
54      public Object getDefaultValue(final Class<?> hint) {
55          final HtmlElement element = getDomNodeOrNull();
56          if (element == null) {
57              return super.getDefaultValue(null);
58          }
59          return HTMLAnchorElement.getDefaultValue(element);
60      }
61  
62      /**
63       * Returns the value of the {@code alt} property.
64       * @return the value of the {@code alt} property
65       */
66      @JsxGetter
67      public String getAlt() {
68          return getDomNodeOrDie().getAttributeDirect("alt");
69      }
70  
71      /**
72       * Returns the value of the {@code alt} property.
73       * @param alt the value
74       */
75      @JsxSetter
76      public void setAlt(final String alt) {
77          getDomNodeOrDie().setAttribute("alt", alt);
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      protected boolean isEndTagForbidden() {
85          return true;
86      }
87  
88      /**
89       * Returns the value of the {@code rel} property.
90       * @return the value of the {@code rel} property
91       */
92      @JsxGetter
93      public String getRel() {
94          return getDomNodeOrDie().getAttributeDirect("rel");
95      }
96  
97      /**
98       * Returns the value of the {@code rel} property.
99       * @param rel the value
100      */
101     @JsxSetter
102     public void setRel(final String rel) {
103         getDomNodeOrDie().setAttribute("rel", rel);
104     }
105 
106     /**
107      * Returns the {@code relList} attribute.
108      * @return the {@code relList} attribute
109      */
110     @JsxGetter
111     public DOMTokenList getRelList() {
112         return new DOMTokenList(this, "rel");
113     }
114 
115     /**
116      * Sets the focus to this element.
117      */
118     @Override
119     public void focus() {
120         // in reality this depends also on the visibility of the area itself
121         final HtmlArea area = (HtmlArea) getDomNodeOrDie();
122         final String hrefAttr = area.getHrefAttribute();
123 
124         if (ATTRIBUTE_NOT_DEFINED != hrefAttr
125                 || getBrowserVersion().hasFeature(JS_AREA_WITHOUT_HREF_FOCUSABLE)) {
126             area.focus();
127         }
128     }
129 
130     /**
131      * Returns the {@code coords} attribute.
132      * @return the {@code coords} attribute
133      */
134     @JsxGetter
135     public String getCoords() {
136         return getDomNodeOrDie().getAttributeDirect("coords");
137     }
138 
139     /**
140      * Sets the {@code coords} attribute.
141      * @param coords {@code coords} attribute
142      */
143     @JsxSetter
144     public void setCoords(final String coords) {
145         getDomNodeOrDie().setAttribute("coords", coords);
146     }
147 
148 }