1
2
3
4
5
6
7
8
9
10
11
12
13
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
39
40
41
42
43
44
45
46
47 @JsxClass(domClass = HtmlArea.class)
48 public class HTMLAreaElement extends HTMLElement {
49
50
51
52
53 @Override
54 @JsxConstructor
55 public void jsConstructor() {
56 super.jsConstructor();
57 }
58
59
60
61
62
63
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
76
77
78 @JsxGetter
79 public String getAlt() {
80 return getDomNodeOrDie().getAttributeDirect("alt");
81 }
82
83
84
85
86
87 @JsxSetter
88 public void setAlt(final String alt) {
89 getDomNodeOrDie().setAttribute("alt", alt);
90 }
91
92
93
94
95 @Override
96 protected boolean isEndTagForbidden() {
97 return true;
98 }
99
100
101
102
103
104 @JsxGetter({FF, FF_ESR})
105 public String getType() {
106 return getDomNodeOrDie().getAttributeDirect(DomElement.TYPE_ATTRIBUTE);
107 }
108
109
110
111
112
113 @JsxSetter({FF, FF_ESR})
114 public void setType(final String type) {
115 getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, type);
116 }
117
118
119
120
121
122 @JsxGetter
123 public String getRel() {
124 return getDomNodeOrDie().getAttributeDirect("rel");
125 }
126
127
128
129
130
131 @JsxSetter
132 public void setRel(final String rel) {
133 getDomNodeOrDie().setAttribute("rel", rel);
134 }
135
136
137
138
139
140 @JsxGetter
141 public DOMTokenList getRelList() {
142 return new DOMTokenList(this, "rel");
143 }
144
145
146
147
148 @Override
149 public void focus() {
150
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
162
163
164 @JsxGetter
165 public String getCoords() {
166 return getDomNodeOrDie().getAttributeDirect("coords");
167 }
168
169
170
171
172
173 @JsxSetter
174 public void setCoords(final String coords) {
175 getDomNodeOrDie().setAttribute("coords", coords);
176 }
177
178
179
180
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
201
202
203 @JsxSetter
204 public void setHref(final String href) {
205 getDomNodeOrDie().setAttribute("href", href);
206 }
207
208
209
210
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
224
225
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
237
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
251
252
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
263
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
284
285
286
287 @JsxSetter
288 public void setHost(final String host) throws Exception {
289 setUrl(HTMLHyperlinkElementUtils.setHost(getUrl(), host));
290 }
291
292
293
294
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
313
314
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
330
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
344
345
346
347 @JsxSetter
348 public void setPathname(final String pathname) throws Exception {
349 setUrl(HTMLHyperlinkElementUtils.setPathname(getUrl(), pathname));
350 }
351
352
353
354
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
368
369
370
371 @JsxSetter
372 public void setSearch(final String search) throws Exception {
373 setUrl(HTMLHyperlinkElementUtils.setSearch(getUrl(), search));
374 }
375
376
377
378
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
392
393
394
395 @JsxSetter
396 public void setHash(final String hash) throws Exception {
397 setUrl(HTMLHyperlinkElementUtils.setHash(getUrl(), hash));
398 }
399
400
401
402
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
420
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
434
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
450 }
451 }
452
453
454
455
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
469
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
485 }
486 }
487
488
489
490
491
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
504
505
506 private void setUrl(final URL url) {
507 getDomNodeOrDie().setAttribute("href", url.toString());
508 }
509 }