1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import java.net.MalformedURLException ; 41 42 import org.mozilla.javascript.Context; 43 44 import com.gargoylesoftware.htmlunit.html.HtmlAnchor; 45 import com.gargoylesoftware.htmlunit.html.HtmlElement; 46 47 48 58 public class Anchor extends FocusableHostElement { 59 60 private static final long serialVersionUID = -816365374422492967L; 61 62 65 public Anchor() { 66 } 67 68 69 73 public void jsConstructor() { 74 } 75 76 80 public void jsxSet_href( final String href ) { 81 getHtmlElementOrDie().setAttributeValue( "href", href ); 82 } 83 84 88 public String jsxGet_href() { 89 return getHtmlElementOrDie().getAttributeValue( "href" ); 90 } 91 92 98 public Object getDefaultValue(final Class hint) { 99 final HtmlAnchor link = (HtmlAnchor) getHtmlElementOrDie(); 100 final String href = link.getHrefAttribute(); 101 102 final String response; 103 if (href == HtmlElement.ATTRIBUTE_NOT_DEFINED) { 104 response = ""; } 106 else { 107 final int indexAnchor = href.indexOf('#'); 108 final String beforeAnchor; 109 final String anchorPart; 110 if (indexAnchor == -1) { 111 beforeAnchor = href; 112 anchorPart = ""; 113 } 114 else { 115 beforeAnchor = href.substring(0, indexAnchor); 116 anchorPart = href.substring(indexAnchor); 117 } 118 119 try { 120 response = link.getPage().getFullyQualifiedUrl(beforeAnchor).toExternalForm() + anchorPart; 121 } 122 catch (final MalformedURLException e) { 123 throw Context.reportRuntimeError("Problem reading url: " + e); 124 } 125 } 126 127 return response; 128 } 129 130 } 131 | Popular Tags |