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