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.html;
16  
17  import static org.htmlunit.BrowserVersionFeatures.JS_AREA_WITHOUT_HREF_FOCUSABLE;
18  import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
19  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
20  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
21  
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  
25  import org.htmlunit.html.DomElement;
26  import org.htmlunit.html.HtmlArea;
27  import org.htmlunit.html.HtmlElement;
28  import org.htmlunit.html.HtmlPage;
29  import org.htmlunit.javascript.configuration.JsxClass;
30  import org.htmlunit.javascript.configuration.JsxConstructor;
31  import org.htmlunit.javascript.configuration.JsxGetter;
32  import org.htmlunit.javascript.configuration.JsxSetter;
33  import org.htmlunit.javascript.host.dom.DOMTokenList;
34  import org.htmlunit.util.StringUtils;
35  import org.htmlunit.util.UrlUtils;
36  
37  /**
38   * The JavaScript object {@code HTMLAreaElement}.
39   *
40   * @author Ahmed Ashour
41   * @author Ronald Brill
42   * @author Frank Danek
43   * @author Lai Quang Duong
44   *
45   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLAreaElement">MDN Documentation</a>
46   */
47  @JsxClass(domClass = HtmlArea.class)
48  public class HTMLAreaElement extends HTMLElement {
49  
50      /**
51       * JavaScript constructor.
52       */
53      @Override
54      @JsxConstructor
55      public void jsConstructor() {
56          super.jsConstructor();
57      }
58  
59      /**
60       * Called, for instance, for implicit conversion to a string.
61       * @see org.htmlunit.javascript.HtmlUnitScriptable#getDefaultValue(java.lang.Class)
62       * @param hint the type hint
63       * @return the default value
64       */
65      @Override
66      public Object getDefaultValue(final Class<?> hint) {
67          final HtmlElement element = getDomNodeOrNull();
68          if (element == null) {
69              return super.getDefaultValue(null);
70          }
71          return HTMLAnchorElement.getDefaultValue(element);
72      }
73  
74      /**
75       * Returns the value of the {@code alt} property.
76       * @return the value of the {@code alt} property
77       */
78      @JsxGetter
79      public String getAlt() {
80          return getDomNodeOrDie().getAttributeDirect("alt");
81      }
82  
83      /**
84       * Sets the value of the {@code alt} property.
85       * @param alt the {@code alt} property value
86       */
87      @JsxSetter
88      public void setAlt(final String alt) {
89          getDomNodeOrDie().setAttribute("alt", alt);
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      protected boolean isEndTagForbidden() {
97          return true;
98      }
99  
100     /**
101      * Returns the {@code type} attribute.
102      * @return the {@code type} attribute
103      */
104     @JsxGetter({FF, FF_ESR})
105     public String getType() {
106         return getDomNodeOrDie().getAttributeDirect(DomElement.TYPE_ATTRIBUTE);
107     }
108 
109     /**
110      * Sets the {@code type} attribute.
111      * @param type the {@code type} attribute value
112      */
113     @JsxSetter({FF, FF_ESR})
114     public void setType(final String type) {
115         getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, type);
116     }
117 
118     /**
119      * Returns the value of the {@code rel} property.
120      * @return the value of the {@code rel} property
121      */
122     @JsxGetter
123     public String getRel() {
124         return getDomNodeOrDie().getAttributeDirect("rel");
125     }
126 
127     /**
128      * Sets the value of the {@code rel} property.
129      * @param rel the {@code rel} property value
130      */
131     @JsxSetter
132     public void setRel(final String rel) {
133         getDomNodeOrDie().setAttribute("rel", rel);
134     }
135 
136     /**
137      * Returns the {@code relList} attribute.
138      * @return the {@code relList} attribute
139      */
140     @JsxGetter
141     public DOMTokenList getRelList() {
142         return new DOMTokenList(this, "rel");
143     }
144 
145     /**
146      * Sets the focus to this element.
147      */
148     @Override
149     public void focus() {
150         // in reality this depends also on the visibility of the area itself
151         final HtmlArea area = (HtmlArea) getDomNodeOrDie();
152         final String hrefAttr = area.getHrefAttribute();
153 
154         if (ATTRIBUTE_NOT_DEFINED != hrefAttr
155                 || getBrowserVersion().hasFeature(JS_AREA_WITHOUT_HREF_FOCUSABLE)) {
156             area.focus();
157         }
158     }
159 
160     /**
161      * Returns the {@code coords} attribute.
162      * @return the {@code coords} attribute
163      */
164     @JsxGetter
165     public String getCoords() {
166         return getDomNodeOrDie().getAttributeDirect("coords");
167     }
168 
169     /**
170      * Sets the {@code coords} attribute.
171      * @param coords the {@code coords} attribute value
172      */
173     @JsxSetter
174     public void setCoords(final String coords) {
175         getDomNodeOrDie().setAttribute("coords", coords);
176     }
177 
178     /**
179      * Returns the {@code href} property.
180      * @return the {@code href} property
181      */
182     @JsxGetter
183     public String getHref() {
184         final HtmlArea area = (HtmlArea) getDomNodeOrDie();
185         final String hrefAttr = area.getHrefAttribute();
186 
187         if (ATTRIBUTE_NOT_DEFINED == hrefAttr) {
188             return "";
189         }
190 
191         try {
192             return getUrl().toString();
193         }
194         catch (final MalformedURLException e) {
195             return hrefAttr;
196         }
197     }
198 
199     /**
200      * Sets the {@code href} property.
201      * @param href the {@code href} value
202      */
203     @JsxSetter
204     public void setHref(final String href) {
205         getDomNodeOrDie().setAttribute("href", href);
206     }
207 
208     /**
209      * Returns the {@code protocol} property.
210      * @return the {@code protocol} property
211      */
212     @JsxGetter
213     public String getProtocol() {
214         try {
215             return getUrl().getProtocol() + ":";
216         }
217         catch (final MalformedURLException e) {
218             return ":";
219         }
220     }
221 
222     /**
223      * Sets the {@code protocol} property.
224      * @param protocol the {@code protocol} value
225      * @throws Exception if an error occurs
226      */
227     @JsxSetter
228     public void setProtocol(final String protocol) throws Exception {
229         final URL result = HTMLHyperlinkElementUtils.setProtocol(getUrl(), protocol);
230         if (result != null) {
231             setUrl(result);
232         }
233     }
234 
235     /**
236      * Returns the {@code hostname} property.
237      * @return the {@code hostname} property
238      */
239     @JsxGetter
240     public String getHostname() {
241         try {
242             return HTMLHyperlinkElementUtils.getHostname(getUrl());
243         }
244         catch (final MalformedURLException e) {
245             return "";
246         }
247     }
248 
249     /**
250      * Sets the {@code hostname} property.
251      * @param hostname the {@code hostname} value
252      * @throws Exception if an error occurs
253      */
254     @JsxSetter
255     public void setHostname(final String hostname) throws Exception {
256         if (!StringUtils.isEmptyOrNull(hostname)) {
257             setUrl(UrlUtils.getUrlWithNewHost(getUrl(), hostname));
258         }
259     }
260 
261     /**
262      * Returns the {@code host} property.
263      * @return the {@code host} property
264      */
265     @JsxGetter
266     public String getHost() {
267         try {
268             final URL url = getUrl();
269             final int port = url.getPort();
270             final String host = url.getHost();
271 
272             if (port == -1 || HTMLHyperlinkElementUtils.isDefaultPort(url.getProtocol(), port)) {
273                 return host;
274             }
275             return host + ":" + port;
276         }
277         catch (final MalformedURLException e) {
278             return "";
279         }
280     }
281 
282     /**
283      * Sets the {@code host} property.
284      * @param host the {@code host} value
285      * @throws Exception if an error occurs
286      */
287     @JsxSetter
288     public void setHost(final String host) throws Exception {
289         setUrl(HTMLHyperlinkElementUtils.setHost(getUrl(), host));
290     }
291 
292     /**
293      * Returns the {@code port} property.
294      * @return the {@code port} property
295      */
296     @JsxGetter
297     public String getPort() {
298         try {
299             final URL url = getUrl();
300             final int port = url.getPort();
301             if (port == -1 || HTMLHyperlinkElementUtils.isDefaultPort(url.getProtocol(), port)) {
302                 return "";
303             }
304             return Integer.toString(port);
305         }
306         catch (final MalformedURLException e) {
307             return "";
308         }
309     }
310 
311     /**
312      * Sets the {@code port} property.
313      * @param port the {@code port} value
314      * @throws Exception if an error occurs
315      */
316     @JsxSetter
317     public void setPort(final String port) throws Exception {
318         final URL url = getUrl();
319         final int newPort = Integer.parseInt(port);
320         if (HTMLHyperlinkElementUtils.isDefaultPort(url.getProtocol(), newPort)) {
321             setUrl(UrlUtils.getUrlWithNewPort(url, -1));
322         }
323         else {
324             setUrl(UrlUtils.getUrlWithNewPort(url, newPort));
325         }
326     }
327 
328     /**
329      * Returns the {@code pathname} property.
330      * @return the {@code pathname} property
331      */
332     @JsxGetter
333     public String getPathname() {
334         try {
335             return getUrl().getPath();
336         }
337         catch (final MalformedURLException e) {
338             return "";
339         }
340     }
341 
342     /**
343      * Sets the {@code pathname} property.
344      * @param pathname the {@code pathname} value
345      * @throws Exception if an error occurs
346      */
347     @JsxSetter
348     public void setPathname(final String pathname) throws Exception {
349         setUrl(HTMLHyperlinkElementUtils.setPathname(getUrl(), pathname));
350     }
351 
352     /**
353      * Returns the {@code search} property.
354      * @return the {@code search} property
355      */
356     @JsxGetter
357     public String getSearch() {
358         try {
359             return HTMLHyperlinkElementUtils.getSearch(getUrl());
360         }
361         catch (final MalformedURLException e) {
362             return "";
363         }
364     }
365 
366     /**
367      * Sets the {@code search} property.
368      * @param search the {@code search} value
369      * @throws Exception if an error occurs
370      */
371     @JsxSetter
372     public void setSearch(final String search) throws Exception {
373         setUrl(HTMLHyperlinkElementUtils.setSearch(getUrl(), search));
374     }
375 
376     /**
377      * Returns the {@code hash} property.
378      * @return the {@code hash} property
379      */
380     @JsxGetter
381     public String getHash() {
382         try {
383             return HTMLHyperlinkElementUtils.getHash(getUrl());
384         }
385         catch (final MalformedURLException e) {
386             return "";
387         }
388     }
389 
390     /**
391      * Sets the {@code hash} property.
392      * @param hash the {@code hash} value
393      * @throws Exception if an error occurs
394      */
395     @JsxSetter
396     public void setHash(final String hash) throws Exception {
397         setUrl(HTMLHyperlinkElementUtils.setHash(getUrl(), hash));
398     }
399 
400     /**
401      * Returns the {@code origin} property.
402      * @return the {@code origin} property
403      */
404     @JsxGetter
405     public String getOrigin() {
406         if (!getDomNodeOrDie().hasAttribute("href")) {
407             return "";
408         }
409 
410         try {
411             return getUrl().getProtocol() + "://" + getHost();
412         }
413         catch (final Exception e) {
414             return "";
415         }
416     }
417 
418     /**
419      * Returns the {@code username} property.
420      * @return the {@code username} property
421      */
422     @JsxGetter
423     public String getUsername() {
424         try {
425             return HTMLHyperlinkElementUtils.getUsername(getUrl());
426         }
427         catch (final MalformedURLException e) {
428             return "";
429         }
430     }
431 
432     /**
433      * Sets the {@code username} property.
434      * @param username the {@code username} value
435      */
436     @JsxSetter
437     public void setUsername(final String username) {
438         try {
439             final HtmlArea area = (HtmlArea) getDomNodeOrDie();
440             final String href = area.getHrefAttribute();
441             if (ATTRIBUTE_NOT_DEFINED == href) {
442                 return;
443             }
444 
445             final URL url = ((HtmlPage) area.getPage()).getFullyQualifiedUrl(href);
446             setUrl(UrlUtils.getUrlWithNewUserName(url, username));
447         }
448         catch (final MalformedURLException ignored) {
449             // ignore
450         }
451     }
452 
453     /**
454      * Returns the {@code password} property.
455      * @return the {@code password} property
456      */
457     @JsxGetter
458     public String getPassword() {
459         try {
460             return HTMLHyperlinkElementUtils.getPassword(getUrl());
461         }
462         catch (final MalformedURLException e) {
463             return "";
464         }
465     }
466 
467     /**
468      * Sets the {@code password} property.
469      * @param password the {@code password} value
470      */
471     @JsxSetter
472     public void setPassword(final String password) {
473         try {
474             final HtmlArea area = (HtmlArea) getDomNodeOrDie();
475             final String href = area.getHrefAttribute();
476             if (ATTRIBUTE_NOT_DEFINED == href) {
477                 return;
478             }
479 
480             final URL url = ((HtmlPage) area.getPage()).getFullyQualifiedUrl(href);
481             setUrl(UrlUtils.getUrlWithNewUserPassword(url, password));
482         }
483         catch (final MalformedURLException ignored) {
484             // ignore
485         }
486     }
487 
488     /**
489      * Returns this area's current URL.
490      * @return this area's current URL
491      * @throws MalformedURLException if an error occurs
492      */
493     private URL getUrl() throws MalformedURLException {
494         final HtmlArea area = (HtmlArea) getDomNodeOrDie();
495         final String href = area.getHrefAttribute();
496         if (ATTRIBUTE_NOT_DEFINED == href) {
497             throw new MalformedURLException("no href attribute");
498         }
499         return ((HtmlPage) area.getPage()).getFullyQualifiedUrl(href);
500     }
501 
502     /**
503      * Sets the {@code href} attribute of this area to the specified URL.
504      * @param url the new value of the {@code href} attribute
505      */
506     private void setUrl(final URL url) {
507         getDomNodeOrDie().setAttribute("href", url.toString());
508     }
509 }