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