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_ANCHOR_PATHNAME_DETECT_WIN_DRIVES_URL_REPLACE;
18 import static org.htmlunit.BrowserVersionFeatures.JS_ANCHOR_PATHNAME_PREFIX_WIN_DRIVES_URL;
19 import static org.htmlunit.BrowserVersionFeatures.JS_ANCHOR_PROTOCOL_COLON_UPPER_CASE_DRIVE_LETTERS;
20 import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
21
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Locale;
27
28 import org.htmlunit.BrowserVersion;
29 import org.htmlunit.HttpHeader;
30 import org.htmlunit.SgmlPage;
31 import org.htmlunit.html.DomElement;
32 import org.htmlunit.html.DomNode;
33 import org.htmlunit.html.HtmlAnchor;
34 import org.htmlunit.html.HtmlElement;
35 import org.htmlunit.html.HtmlPage;
36 import org.htmlunit.javascript.JavaScriptEngine;
37 import org.htmlunit.javascript.configuration.JsxClass;
38 import org.htmlunit.javascript.configuration.JsxConstructor;
39 import org.htmlunit.javascript.configuration.JsxGetter;
40 import org.htmlunit.javascript.configuration.JsxSetter;
41 import org.htmlunit.javascript.host.dom.DOMTokenList;
42 import org.htmlunit.util.StringUtils;
43 import org.htmlunit.util.UrlUtils;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 @JsxClass(domClass = HtmlAnchor.class)
62 public class HTMLAnchorElement extends HTMLElement {
63 private static final List<String> REFERRER_POLICIES = Arrays.asList(
64 "no-referrer", HttpHeader.ORIGIN_LC, "unsafe-url");
65
66
67
68
69 @Override
70 @JsxConstructor
71 public void jsConstructor() {
72 super.jsConstructor();
73 }
74
75
76
77
78
79 @JsxSetter
80 public void setHref(final String href) {
81 getDomNodeOrDie().setAttribute("href", href);
82 }
83
84
85
86
87
88 @JsxGetter
89 public String getHref() {
90 final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
91 final String hrefAttr = anchor.getHrefAttribute();
92
93 if (ATTRIBUTE_NOT_DEFINED == hrefAttr) {
94 return "";
95 }
96
97 try {
98 return getUrl().toString();
99 }
100 catch (final MalformedURLException e) {
101 return hrefAttr;
102 }
103 }
104
105
106
107
108 @Override
109 public void focus() {
110 final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
111 final String hrefAttr = anchor.getHrefAttribute();
112
113 if (ATTRIBUTE_NOT_DEFINED != hrefAttr) {
114 anchor.focus();
115 }
116 }
117
118
119
120
121
122 @JsxSetter
123 @Override
124 public void setName(final String name) {
125 getDomNodeOrDie().setAttribute(DomElement.NAME_ATTRIBUTE, name);
126 }
127
128
129
130
131
132 @JsxGetter
133 @Override
134 public String getName() {
135 return getDomNodeOrDie().getAttributeDirect(DomElement.NAME_ATTRIBUTE);
136 }
137
138
139
140
141
142 @JsxSetter
143 public void setTarget(final String target) {
144 getDomNodeOrDie().setAttribute("target", target);
145 }
146
147
148
149
150
151 @JsxGetter
152 public String getTarget() {
153 return getDomNodeOrDie().getAttributeDirect("target");
154 }
155
156
157
158
159
160
161 private URL getUrl() throws MalformedURLException {
162 final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
163 return ((HtmlPage) anchor.getPage()).getFullyQualifiedUrl(anchor.getHrefAttribute());
164 }
165
166
167
168
169
170 private void setUrl(final URL url) {
171 getDomNodeOrDie().setAttribute("href", url.toString());
172 }
173
174
175
176
177
178 @JsxSetter
179 public void setRel(final String rel) {
180 getDomNodeOrDie().setAttribute("rel", rel);
181 }
182
183
184
185
186
187 @JsxGetter
188 public String getRel() {
189 return ((HtmlAnchor) getDomNodeOrDie()).getRelAttribute();
190 }
191
192
193
194
195
196 @JsxGetter
197 public String getRev() {
198 return ((HtmlAnchor) getDomNodeOrDie()).getRevAttribute();
199 }
200
201
202
203
204
205 @JsxSetter
206 public void setRev(final String rel) {
207 getDomNodeOrDie().setAttribute("rev", rel);
208 }
209
210
211
212
213
214 @JsxGetter
215 public String getReferrerPolicy() {
216 String attrib = getDomNodeOrDie().getAttribute("referrerPolicy");
217 if (StringUtils.isEmptyOrNull(attrib)) {
218 return "";
219 }
220 attrib = attrib.toLowerCase(Locale.ROOT);
221 if (REFERRER_POLICIES.contains(attrib)) {
222 return attrib;
223 }
224 return "";
225 }
226
227
228
229
230
231 @JsxSetter
232 public void setReferrerPolicy(final String referrerPolicy) {
233 getDomNodeOrDie().setAttribute("referrerPolicy", referrerPolicy);
234 }
235
236
237
238
239
240
241
242 @JsxGetter
243 public String getSearch() {
244 try {
245 return HTMLHyperlinkElementUtils.getSearch(getUrl());
246 }
247 catch (final MalformedURLException e) {
248 return "";
249 }
250 }
251
252
253
254
255
256
257
258
259 @JsxSetter
260 public void setSearch(final String search) throws Exception {
261 setUrl(HTMLHyperlinkElementUtils.setSearch(getUrl(), search));
262 }
263
264
265
266
267
268
269 @JsxGetter
270 public String getHash() {
271 try {
272 return HTMLHyperlinkElementUtils.getHash(getUrl());
273 }
274 catch (final MalformedURLException e) {
275 return "";
276 }
277 }
278
279
280
281
282
283
284
285 @JsxSetter
286 public void setHash(final String hash) throws Exception {
287 setUrl(HTMLHyperlinkElementUtils.setHash(getUrl(), hash));
288 }
289
290
291
292
293
294
295 @JsxGetter
296 public String getHost() {
297 try {
298 final URL url = getUrl();
299 final int port = url.getPort();
300 final String host = url.getHost();
301
302 if (port == -1 || HTMLHyperlinkElementUtils.isDefaultPort(url.getProtocol(), port)) {
303 return host;
304 }
305 return host + ":" + port;
306 }
307 catch (final MalformedURLException e) {
308 return "";
309 }
310 }
311
312
313
314
315
316
317
318 @JsxSetter
319 public void setHost(final String host) throws Exception {
320 setUrl(HTMLHyperlinkElementUtils.setHost(getUrl(), host));
321 }
322
323
324
325
326
327
328 @JsxGetter
329 public String getHostname() {
330 try {
331 return HTMLHyperlinkElementUtils.getHostname(getUrl());
332 }
333 catch (final MalformedURLException e) {
334 return "";
335 }
336 }
337
338
339
340
341
342
343
344 @JsxSetter
345 public void setHostname(final String hostname) throws Exception {
346 if (org.htmlunit.util.StringUtils.isEmptyOrNull(hostname) || hostname.indexOf(' ') > -1) {
347 return;
348 }
349
350 final int idx = hostname.indexOf('#');
351 final String host = idx > -1 ? hostname.substring(0, idx) : hostname;
352 if (host.isEmpty()) {
353 return;
354 }
355
356 try {
357 setUrl(UrlUtils.getUrlWithNewHost(getUrl(), host));
358 }
359 catch (final MalformedURLException e) {
360
361 }
362 }
363
364
365
366
367
368
369 @JsxGetter
370 public String getPathname() {
371 final BrowserVersion browser = getBrowserVersion();
372 try {
373 if (browser.hasFeature(JS_ANCHOR_PATHNAME_DETECT_WIN_DRIVES_URL_REPLACE)) {
374 final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
375 String href = anchor.getHrefAttribute();
376 if (href.length() > 1 && Character.isLetter(href.charAt(0)) && ':' == href.charAt(1)) {
377 if (browser.hasFeature(JS_ANCHOR_PROTOCOL_COLON_UPPER_CASE_DRIVE_LETTERS)) {
378 href = org.apache.commons.lang3.StringUtils.capitalize(href);
379 }
380 if (browser.hasFeature(JS_ANCHOR_PATHNAME_PREFIX_WIN_DRIVES_URL)) {
381 href = "/" + href;
382 }
383 return href;
384 }
385 }
386 return getUrl().getPath();
387 }
388 catch (final MalformedURLException e) {
389 final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
390 if (anchor.getHrefAttribute().startsWith("http")) {
391 return "";
392 }
393 return "/";
394 }
395 }
396
397
398
399
400
401
402
403 @JsxSetter
404 public void setPathname(final String pathname) throws Exception {
405 setUrl(HTMLHyperlinkElementUtils.setPathname(getUrl(), pathname));
406 }
407
408
409
410
411
412
413 @JsxGetter
414 public String getPort() {
415 try {
416 final URL url = getUrl();
417 final int port = url.getPort();
418 if (port == -1 || HTMLHyperlinkElementUtils.isDefaultPort(url.getProtocol(), port)) {
419 return "";
420 }
421 return Integer.toString(port);
422 }
423 catch (final MalformedURLException e) {
424 return "";
425 }
426 }
427
428
429
430
431
432
433
434 @JsxSetter
435 public void setPort(final String port) throws Exception {
436 final URL url = getUrl();
437 final int newPort = Integer.parseInt(port);
438 if (HTMLHyperlinkElementUtils.isDefaultPort(url.getProtocol(), newPort)) {
439 setUrl(UrlUtils.getUrlWithNewPort(url, -1));
440 }
441 else {
442 setUrl(UrlUtils.getUrlWithNewPort(url, newPort));
443 }
444 }
445
446
447
448
449
450
451 @JsxGetter
452 public String getProtocol() {
453 final BrowserVersion browser = getBrowserVersion();
454 try {
455 if (browser.hasFeature(JS_ANCHOR_PATHNAME_DETECT_WIN_DRIVES_URL_REPLACE)) {
456 final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
457 final String href = anchor.getHrefAttribute().toLowerCase(Locale.ROOT);
458 if (href.length() > 1 && Character.isLetter(href.charAt(0)) && ':' == href.charAt(1)) {
459 return "file:";
460 }
461 }
462
463 return getUrl().getProtocol() + ":";
464 }
465 catch (final MalformedURLException e) {
466 final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
467 if (anchor.getHrefAttribute().startsWith("http")) {
468 return ":";
469 }
470 return StringUtils.substringBefore(anchor.getHrefAttribute(), "/");
471 }
472 }
473
474
475
476
477
478
479
480 @JsxSetter
481 public void setProtocol(final String protocol) throws Exception {
482 final URL result = HTMLHyperlinkElementUtils.setProtocol(getUrl(), protocol);
483 if (result != null) {
484 setUrl(result);
485 }
486 }
487
488
489
490
491
492
493
494 @Override
495 public Object getDefaultValue(final Class<?> hint) {
496 final HtmlElement element = getDomNodeOrNull();
497 if (element == null) {
498 return super.getDefaultValue(null);
499 }
500 return getDefaultValue(element);
501 }
502
503 static String getDefaultValue(final HtmlElement element) {
504 String href = element.getAttributeDirect("href");
505
506 if (ATTRIBUTE_NOT_DEFINED == href) {
507 return "";
508 }
509
510 href = href.trim();
511
512 final SgmlPage page = element.getPage();
513 if (page == null || !page.isHtmlPage()) {
514 return href;
515 }
516
517 try {
518 return HtmlAnchor.getTargetUrl(href, (HtmlPage) page).toExternalForm();
519 }
520 catch (final MalformedURLException e) {
521 return href;
522 }
523 }
524
525
526
527
528
529 @JsxGetter
530 public String getText() {
531 final DomNode htmlElement = getDomNodeOrDie();
532 return htmlElement.asNormalizedText();
533 }
534
535
536
537
538
539 @JsxSetter
540 public void setText(final String text) {
541 final DomNode htmlElement = getDomNodeOrDie();
542 htmlElement.setTextContent(text);
543 }
544
545
546
547
548
549 @JsxGetter
550 public String getCharset() {
551 return getDomNodeOrDie().getAttributeDirect("charset");
552 }
553
554
555
556
557
558 @JsxSetter
559 public void setCharset(final String charset) {
560 getDomNodeOrDie().setAttribute("charset", charset);
561 }
562
563
564
565
566
567 @JsxGetter
568 public String getCoords() {
569 return getDomNodeOrDie().getAttributeDirect("coords");
570 }
571
572
573
574
575
576 @JsxSetter
577 public void setCoords(final String coords) {
578 getDomNodeOrDie().setAttribute("coords", coords);
579 }
580
581
582
583
584
585 @JsxGetter
586 public String getHreflang() {
587 return getDomNodeOrDie().getAttributeDirect("hreflang");
588 }
589
590
591
592
593
594 @JsxSetter
595 public void setHreflang(final String hreflang) {
596 getDomNodeOrDie().setAttribute("hreflang", hreflang);
597 }
598
599
600
601
602
603 @JsxGetter
604 public String getOrigin() {
605 if (!getDomNodeOrDie().hasAttribute("href")) {
606 return "";
607 }
608
609 try {
610 return getUrl().getProtocol() + "://" + getHost();
611 }
612 catch (final Exception e) {
613 return "";
614 }
615 }
616
617
618
619
620
621 @JsxGetter
622 public String getUsername() {
623 try {
624 return HTMLHyperlinkElementUtils.getUsername(getUrl());
625 }
626 catch (final MalformedURLException e) {
627 return "";
628 }
629 }
630
631
632
633
634
635 @JsxSetter
636 public void setUsername(final String username) {
637 try {
638 final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
639 final String href = anchor.getHrefAttribute();
640 if (ATTRIBUTE_NOT_DEFINED == href) {
641 return;
642 }
643
644 final URL url = ((HtmlPage) anchor.getPage()).getFullyQualifiedUrl(href);
645 setUrl(UrlUtils.getUrlWithNewUserName(url, username));
646 }
647 catch (final MalformedURLException ignored) {
648
649 }
650 }
651
652
653
654
655
656 @JsxGetter
657 public String getPassword() {
658 try {
659 return HTMLHyperlinkElementUtils.getPassword(getUrl());
660 }
661 catch (final MalformedURLException e) {
662 return "";
663 }
664 }
665
666
667
668
669
670 @JsxSetter
671 public void setPassword(final String password) {
672 try {
673 final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
674 final String href = anchor.getHrefAttribute();
675 if (ATTRIBUTE_NOT_DEFINED == href) {
676 return;
677 }
678
679 final URL url = ((HtmlPage) anchor.getPage()).getFullyQualifiedUrl(href);
680 setUrl(UrlUtils.getUrlWithNewUserPassword(url, password));
681 }
682 catch (final MalformedURLException ignored) {
683
684 }
685 }
686
687
688
689
690
691 @JsxGetter
692 public String getDownload() {
693 return ((HtmlAnchor) getDomNodeOrDie()).getDownloadAttribute();
694 }
695
696
697
698
699
700 @JsxSetter
701 public void setDownload(final String download) {
702 getDomNodeOrDie().setAttribute("download", download);
703 }
704
705
706
707
708
709 @JsxGetter
710 public String getPing() {
711 return ((HtmlAnchor) getDomNodeOrDie()).getPingAttribute();
712 }
713
714
715
716
717
718 @JsxSetter
719 public void setPing(final String ping) {
720 getDomNodeOrDie().setAttribute("ping", ping);
721 }
722
723
724
725
726
727 @JsxGetter
728 public String getShape() {
729 return getDomNodeOrDie().getAttribute("shape");
730 }
731
732
733
734
735
736 @JsxSetter
737 public void setShape(final String shape) {
738 getDomNodeOrDie().setAttribute("shape", shape);
739 }
740
741
742
743
744
745 @JsxGetter
746 public String getType() {
747 return getDomNodeOrDie().getAttributeDirect(DomElement.TYPE_ATTRIBUTE);
748 }
749
750
751
752
753
754 @JsxSetter
755 public void setType(final String type) {
756 getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, type);
757 }
758
759
760
761
762
763 @JsxGetter
764 public DOMTokenList getRelList() {
765 return new DOMTokenList(this, "rel");
766 }
767
768
769
770
771
772 @JsxSetter
773 public void setRelList(final Object rel) {
774 setRel(JavaScriptEngine.toString(rel));
775 }
776 }