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.WebClient; 45 import com.gargoylesoftware.htmlunit.html.HtmlImage; 46 import com.gargoylesoftware.htmlunit.html.HtmlPage; 47 import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine; 48 49 50 59 public class Image extends HTMLElement { 60 61 private static final long serialVersionUID = 5630843390548382869L; 62 private String src_; 63 64 65 68 public Image() { 69 } 70 71 72 76 public void jsConstructor() { 77 } 78 79 85 public void jsxSet_src(final String src) { 86 src_ = src; 87 final HtmlImage htmlImageElement = (HtmlImage) getHtmlElementOrNull(); 88 if (htmlImageElement != null) { 89 htmlImageElement.setAttributeValue( "src", src ); 90 } 91 } 92 93 100 public String jsxGet_src() { 101 final HtmlImage htmlImageElement = (HtmlImage) getHtmlElementOrNull(); 102 if (htmlImageElement != null) { 103 final String srcValue = htmlImageElement.getSrcAttribute(); 104 try { 105 return htmlImageElement.getPage().getFullyQualifiedUrl(srcValue).toExternalForm(); 106 } 107 catch (final MalformedURLException e) { 108 throw Context.reportRuntimeError("Unable to create fully qualified URL for src attribute of image: " 109 + e.getMessage()); 110 } 111 } 112 else { 113 final WebClient webClient = JavaScriptEngine.getWebClientForCurrentThread(); 118 final HtmlPage currentPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); 119 try { 120 return currentPage.getFullyQualifiedUrl(src_).toExternalForm(); 121 } 122 catch (final MalformedURLException e) { 123 throw Context.reportRuntimeError("Unable to create fully qualified URL for src attribute of image: " 124 + e.getMessage()); 125 } 126 } 127 } 128 } 129 | Popular Tags |