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 java.io.IOException;
18  
19  import org.htmlunit.SgmlPage;
20  import org.htmlunit.html.DomElement;
21  import org.htmlunit.html.DomNode;
22  import org.htmlunit.html.HtmlElement;
23  import org.htmlunit.html.HtmlImage;
24  import org.htmlunit.javascript.JavaScriptEngine;
25  import org.htmlunit.javascript.configuration.JsxClass;
26  import org.htmlunit.javascript.configuration.JsxConstructor;
27  import org.htmlunit.javascript.configuration.JsxGetter;
28  import org.htmlunit.javascript.configuration.JsxSetter;
29  
30  /**
31   * The JavaScript object {@code HTMLImageElement}.
32   *
33   * @author Mike Bowler
34   * @author George Murnock
35   * @author Chris Erskine
36   * @author Marc Guillemot
37   * @author Ahmed Ashour
38   * @author Ronald Brill
39   */
40  @JsxClass(domClass = HtmlImage.class)
41  public class HTMLImageElement extends HTMLElement {
42  
43      private boolean endTagForbidden_ = true;
44  
45      /**
46       * JavaScript constructor.
47       */
48      @Override
49      @JsxConstructor
50      public void jsConstructor() {
51          throw JavaScriptEngine.typeError("Invalid constructor.");
52      }
53  
54      /**
55       * JavaScript constructor.
56       */
57      public void jsConstructorImage() {
58          final SgmlPage page = (SgmlPage) getWindow().getWebWindow().getEnclosedPage();
59          final DomElement fake =
60                  page.getWebClient().getPageCreator().getHtmlParser()
61                      .getFactory(HtmlImage.TAG_NAME)
62                      .createElement(page, HtmlImage.TAG_NAME, null);
63          setDomNode(fake);
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public void setDomNode(final DomNode domNode) {
71          super.setDomNode(domNode);
72          if ("image".equalsIgnoreCase(domNode.getLocalName())) {
73              endTagForbidden_ = false;
74          }
75      }
76  
77      /**
78       * Sets the {@code src} attribute.
79       * @param src the {@code src} attribute value
80       */
81      @JsxSetter
82      public void setSrc(final String src) {
83          final HtmlElement img = getDomNodeOrDie();
84          img.setAttribute(DomElement.SRC_ATTRIBUTE, src);
85      }
86  
87      /**
88       * Returns the value of the {@code src} attribute.
89       * @return the value of the {@code src} attribute
90       */
91      @JsxGetter
92      public String getSrc() {
93          final HtmlImage img = (HtmlImage) getDomNodeOrDie();
94          return img.getSrc();
95      }
96  
97      /**
98       * Sets the {@code onload} event handler for this element.
99       * @param onload the {@code onload} event handler for this element
100      */
101     @Override
102     public void setOnload(final Object onload) {
103         super.setOnload(onload);
104 
105         // maybe the onload handler was not called so far
106         final HtmlImage img = (HtmlImage) getDomNodeOrDie();
107         img.doOnLoad();
108     }
109 
110     /**
111      * Returns the value of the {@code alt} property.
112      * @return the value of the {@code alt} property
113      */
114     @JsxGetter
115     public String getAlt() {
116         return getDomNodeOrDie().getAttributeDirect("alt");
117     }
118 
119     /**
120      * Sets the value of the {@code alt} property.
121      * @param alt the value
122      */
123     @JsxSetter
124     public void setAlt(final String alt) {
125         getDomNodeOrDie().setAttribute("alt", alt);
126     }
127 
128     /**
129      * Gets the {@code border} attribute.
130      * @return the {@code border} attribute
131      */
132     @JsxGetter
133     public String getBorder() {
134         return getDomNodeOrDie().getAttributeDirect("border");
135     }
136 
137     /**
138      * Sets the {@code border} attribute.
139      * @param border the {@code border} attribute
140      */
141     @JsxSetter
142     public void setBorder(final String border) {
143         getDomNodeOrDie().setAttribute("border", border);
144     }
145 
146     /**
147      * Returns the value of the {@code align} property.
148      * @return the value of the {@code align} property
149      */
150     @JsxGetter
151     public String getAlign() {
152         return getDomNodeOrDie().getAttributeDirect("align");
153     }
154 
155     /**
156      * Sets the value of the {@code align} property.
157      * @param align the value of the {@code align} property
158      */
159     @JsxSetter
160     public void setAlign(final String align) {
161         getDomNodeOrDie().setAttribute("align", align);
162     }
163 
164     /**
165      * Returns the value of the {@code width} property.
166      * @return the value of the {@code width} property
167      */
168     @JsxGetter
169     public int getWidth() {
170         final HtmlImage img = (HtmlImage) getDomNodeOrDie();
171         return img.getWidthOrDefault();
172     }
173 
174     /**
175      * Sets the value of the {@code width} property.
176      * @param width the value of the {@code width} property
177      */
178     @JsxSetter
179     public void setWidth(final String width) {
180         getDomNodeOrDie().setAttribute("width", width);
181     }
182 
183     /**
184      * Returns the value of the {@code height} property.
185      * @return the value of the {@code height} property
186      */
187     @JsxGetter
188     public int getHeight() {
189         final HtmlImage img = (HtmlImage) getDomNodeOrDie();
190         return img.getHeightOrDefault();
191     }
192 
193     /**
194      * Sets the value of the {@code height} property.
195      * @param height the value of the {@code height} property
196      */
197     @JsxSetter
198     public void setHeight(final String height) {
199         getDomNodeOrDie().setAttribute("height", height);
200     }
201 
202     /**
203      * {@inheritDoc}
204      */
205     @Override
206     protected boolean isEndTagForbidden() {
207         return endTagForbidden_;
208     }
209 
210     /**
211      * Support for the image.complete() property.
212      * @return the value of the {@code complete} property
213      */
214     @JsxGetter
215     public boolean isComplete() {
216         return ((HtmlImage) getDomNodeOrDie()).isComplete();
217     }
218 
219     /**
220      * Returns the value of the {@code naturalWidth} property.
221      * @return the value of the {@code naturalWidth} property
222      */
223     @JsxGetter
224     public int getNaturalWidth() {
225         final HtmlImage img = (HtmlImage) getDomNodeOrDie();
226         try {
227             return img.getWidth();
228         }
229         catch (final IOException e) {
230             return 0;
231         }
232     }
233 
234     /**
235      * Returns the value of the {@code naturalHeight} property.
236      * @return the value of the {@code naturalHeight} property
237      */
238     @JsxGetter
239     public int getNaturalHeight() {
240         final HtmlImage img = (HtmlImage) getDomNodeOrDie();
241         try {
242             return img.getHeight();
243         }
244         catch (final IOException e) {
245             return 0;
246         }
247     }
248 
249     /**
250      * Returns the {@code name} attribute.
251      * @return the {@code name} attribute
252      */
253     @JsxGetter
254     @Override
255     public String getName() {
256         return getDomNodeOrDie().getAttributeDirect(DomElement.NAME_ATTRIBUTE);
257     }
258 
259     /**
260      * Sets the {@code name} attribute.
261      * @param name the {@code name} attribute
262      */
263     @JsxSetter
264     @Override
265     public void setName(final String name) {
266         getDomNodeOrDie().setAttribute(DomElement.NAME_ATTRIBUTE, name);
267     }
268 
269 }