View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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    * http://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 com.gargoylesoftware.htmlunit.javascript.host.html;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_BODY_MARGINS_8;
18  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE;
22  
23  import java.util.Locale;
24  
25  import com.gargoylesoftware.htmlunit.html.HtmlBody;
26  import com.gargoylesoftware.htmlunit.html.HtmlElement;
27  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
28  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
29  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction;
30  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
31  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter;
32  import com.gargoylesoftware.htmlunit.javascript.host.css.ComputedCSSStyleDeclaration;
33  import com.gargoylesoftware.htmlunit.javascript.host.dom.TextRange;
34  
35  import net.sourceforge.htmlunit.corejs.javascript.Function;
36  
37  /**
38   * The JavaScript object {@code HTMLBodyElement}.
39   *
40   * @author Ahmed Ashour
41   * @author Marc Guillemot
42   * @author Daniel Gredler
43   */
44  @JsxClass(domClass = HtmlBody.class)
45  public class HTMLBodyElement extends HTMLElement {
46  
47      /**
48       * Creates an instance.
49       */
50      @JsxConstructor({CHROME, FF, EDGE})
51      public HTMLBodyElement() {
52      }
53  
54      /**
55       * Creates the event handler from the attribute value. This has to be done no matter which browser
56       * is simulated to handle ill-formed HTML code with many body (possibly generated) elements.
57       * @param attributeName the attribute name
58       * @param value the value
59       */
60      public void createEventHandlerFromAttribute(final String attributeName, final String value) {
61          // when many body tags are found while parsing, attributes of
62          // different tags are added and should create an event handler when needed
63          if (attributeName.toLowerCase(Locale.ROOT).startsWith("on")) {
64              createEventHandler(attributeName.substring(2), value);
65          }
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      protected boolean isEventHandlerOnWindow() {
73          return true;
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public void setDefaults(final ComputedCSSStyleDeclaration style) {
81          if (getBrowserVersion().hasFeature(JS_BODY_MARGINS_8)) {
82              style.setDefaultLocalStyleAttribute("margin", "8px");
83              style.setDefaultLocalStyleAttribute("padding", "0px");
84          }
85          else {
86              style.setDefaultLocalStyleAttribute("margin-left", "8px");
87              style.setDefaultLocalStyleAttribute("margin-right", "8px");
88              style.setDefaultLocalStyleAttribute("margin-top", "8px");
89              style.setDefaultLocalStyleAttribute("margin-bottom", "8px");
90          }
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public HTMLElement getOffsetParent_js() {
98          return null;
99      }
100 
101     /**
102      * Returns the value of the {@code aLink} attribute.
103      * @return the value of the {@code aLink} attribute
104      * @see <a href="http://msdn.microsoft.com/en-us/library/ms533070.aspx">MSDN Documentation</a>
105      */
106     @JsxGetter
107     public String getALink() {
108         return getDomNodeOrDie().getAttribute("aLink");
109     }
110 
111     /**
112      * Sets the value of the {@code aLink} attribute.
113      * @param aLink the value of the {@code aLink} attribute
114      * @see <a href="http://msdn.microsoft.com/en-us/library/ms533070.aspx">MSDN Documentation</a>
115      */
116     @JsxSetter
117     public void setALink(final String aLink) {
118         setColorAttribute("aLink", aLink);
119     }
120 
121     /**
122      * Returns the value of the {@code background} attribute.
123      * @return the value of the {@code background} attribute
124      * @see <a href="http://msdn.microsoft.com/en-us/library/ms533498.aspx">MSDN Documentation</a>
125      */
126     @JsxGetter
127     public String getBackground() {
128         final HtmlElement node = getDomNodeOrDie();
129         return node.getAttribute("background");
130     }
131 
132     /**
133      * Sets the value of the {@code background} attribute.
134      * @param background the value of the {@code background} attribute
135      * @see <a href="http://msdn.microsoft.com/en-us/library/ms533498.aspx">MSDN Documentation</a>
136      */
137     @JsxSetter
138     public void setBackground(final String background) {
139         getDomNodeOrDie().setAttribute("background", background);
140     }
141 
142     /**
143      * Returns the value of the {@code bgColor} attribute.
144      * @return the value of the {@code bgColor} attribute
145      * @see <a href="http://msdn.microsoft.com/en-us/library/ms533505.aspx">MSDN Documentation</a>
146      */
147     @JsxGetter
148     public String getBgColor() {
149         return getDomNodeOrDie().getAttribute("bgColor");
150     }
151 
152     /**
153      * Sets the value of the {@code bgColor} attribute.
154      * @param bgColor the value of the {@code bgColor} attribute
155      * @see <a href="http://msdn.microsoft.com/en-us/library/ms533505.aspx">MSDN Documentation</a>
156      */
157     @JsxSetter
158     public void setBgColor(final String bgColor) {
159         setColorAttribute("bgColor", bgColor);
160     }
161 
162     /**
163      * Returns the value of the {@code link} attribute.
164      * @return the value of the {@code link} attribute
165      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534119.aspx">MSDN Documentation</a>
166      */
167     @JsxGetter
168     public String getLink() {
169         return getDomNodeOrDie().getAttribute("link");
170     }
171 
172     /**
173      * Sets the value of the {@code link} attribute.
174      * @param link the value of the {@code link} attribute
175      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534119.aspx">MSDN Documentation</a>
176      */
177     @JsxSetter
178     public void setLink(final String link) {
179         setColorAttribute("link", link);
180     }
181 
182     /**
183      * Returns the value of the {@code text} attribute.
184      * @return the value of the {@code text} attribute
185      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534677.aspx">MSDN Documentation</a>
186      */
187     @JsxGetter
188     public String getText() {
189         return getDomNodeOrDie().getAttribute("text");
190     }
191 
192     /**
193      * Sets the value of the {@code text} attribute.
194      * @param text the value of the {@code text} attribute
195      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534677.aspx">MSDN Documentation</a>
196      */
197     @JsxSetter
198     public void setText(final String text) {
199         setColorAttribute("text", text);
200     }
201 
202     /**
203      * Returns the value of the {@code vLink} attribute.
204      * @return the value of the {@code vLink} attribute
205      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534677.aspx">MSDN Documentation</a>
206      */
207     @JsxGetter
208     public String getVLink() {
209         return getDomNodeOrDie().getAttribute("vLink");
210     }
211 
212     /**
213      * Sets the value of the {@code vLink} attribute.
214      * @param vLink the value of the {@code vLink} attribute
215      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534677.aspx">MSDN Documentation</a>
216      */
217     @JsxSetter
218     public void setVLink(final String vLink) {
219         setColorAttribute("vLink", vLink);
220     }
221 
222     /**
223      * {@inheritDoc}
224      */
225     @Override
226     public int getClientWidth() {
227         return super.getClientWidth() + 16;
228     }
229 
230     /**
231      * {@inheritDoc}
232      */
233     @Override
234     @JsxFunction(IE)
235     public TextRange createTextRange() {
236         return super.createTextRange();
237     }
238 
239     /**
240      * Returns the {@code onbeforeunload} event handler for this element.
241      * @return the {@code onbeforeunload} event handler for this element
242      */
243     @JsxGetter
244     public Function getOnbeforeunload() {
245         return getEventHandler("beforeunload");
246     }
247 
248     /**
249      * Sets the {@code onbeforeunload} event handler for this element.
250      * @param onbeforeunload the {@code onbeforeunload} event handler for this element
251      */
252     @JsxSetter
253     public void setOnbeforeunload(final Object onbeforeunload) {
254         setEventHandler("beforeunload", onbeforeunload);
255     }
256 
257     /**
258      * Returns the {@code onhashchange} event handler for this element.
259      * @return the {@code onhashchange} event handler for this element
260      */
261     @JsxGetter
262     public Function getOnhashchange() {
263         return getEventHandler("hashchange");
264     }
265 
266     /**
267      * Sets the {@code onhashchange} event handler for this element.
268      * @param onhashchange the {@code onhashchange} event handler for this element
269      */
270     @JsxSetter
271     public void setOnhashchange(final Object onhashchange) {
272         setEventHandler("hashchange", onhashchange);
273     }
274 
275     /**
276      * Returns the {@code onlanguagechange} event handler for this element.
277      * @return the {@code onlanguagechange} event handler for this element
278      */
279     @JsxGetter({CHROME, FF})
280     public Function getOnlanguagechange() {
281         return getEventHandler("languagechange");
282     }
283 
284     /**
285      * Sets the {@code onlanguagechange} event handler for this element.
286      * @param onlanguagechange the {@code onlanguagechange} event handler for this element
287      */
288     @JsxSetter({CHROME, FF})
289     public void setOnlanguagechange(final Object onlanguagechange) {
290         setEventHandler("languagechange", onlanguagechange);
291     }
292 
293     /**
294      * Returns the {@code onmessage} event handler for this element.
295      * @return the {@code onmessage} event handler for this element
296      */
297     @JsxGetter
298     public Function getOnmessage() {
299         return getEventHandler("message");
300     }
301 
302     /**
303      * Sets the {@code onmessage} event handler for this element.
304      * @param onmessage the {@code onmessage} event handler for this element
305      */
306     @JsxSetter
307     public void setOnmessage(final Object onmessage) {
308         setEventHandler("message", onmessage);
309     }
310 
311     /**
312      * Returns the {@code onoffline} event handler for this element.
313      * @return the {@code onoffline} event handler for this element
314      */
315     @JsxGetter
316     public Function getOnoffline() {
317         return getEventHandler("offline");
318     }
319 
320     /**
321      * Sets the {@code onoffline} event handler for this element.
322      * @param onoffline the {@code onoffline} event handler for this element
323      */
324     @JsxSetter
325     public void setOnoffline(final Object onoffline) {
326         setEventHandler("offline", onoffline);
327     }
328 
329     /**
330      * Returns the {@code ononline} event handler for this element.
331      * @return the {@code ononline} event handler for this element
332      */
333     @JsxGetter
334     public Function getOnonline() {
335         return getEventHandler("online");
336     }
337 
338     /**
339      * Sets the {@code ononline} event handler for this element.
340      * @param ononline the {@code ononline} event handler for this element
341      */
342     @JsxSetter
343     public void setOnonline(final Object ononline) {
344         setEventHandler("online", ononline);
345     }
346 
347     /**
348      * Returns the {@code onpagehide} event handler for this element.
349      * @return the {@code onpagehide} event handler for this element
350      */
351     @JsxGetter
352     public Function getOnpagehide() {
353         return getEventHandler("pagehide");
354     }
355 
356     /**
357      * Sets the {@code onpagehide} event handler for this element.
358      * @param onpagehide the {@code onpagehide} event handler for this element
359      */
360     @JsxSetter
361     public void setOnpagehide(final Object onpagehide) {
362         setEventHandler("pagehide", onpagehide);
363     }
364 
365     /**
366      * Returns the {@code onpageshow} event handler for this element.
367      * @return the {@code onpageshow} event handler for this element
368      */
369     @JsxGetter
370     public Function getOnpageshow() {
371         return getEventHandler("pageshow");
372     }
373 
374     /**
375      * Sets the {@code onpageshow} event handler for this element.
376      * @param onpageshow the {@code onpageshow} event handler for this element
377      */
378     @JsxSetter
379     public void setOnpageshow(final Object onpageshow) {
380         setEventHandler("pageshow", onpageshow);
381     }
382 
383     /**
384      * Returns the {@code onpopstate} event handler for this element.
385      * @return the {@code onpopstate} event handler for this element
386      */
387     @JsxGetter
388     public Function getOnpopstate() {
389         return getEventHandler("popstate");
390     }
391 
392     /**
393      * Sets the {@code onpopstate} event handler for this element.
394      * @param onpopstate the {@code onpopstate} event handler for this element
395      */
396     @JsxSetter
397     public void setOnpopstate(final Object onpopstate) {
398         setEventHandler("popstate", onpopstate);
399     }
400 
401     /**
402      * Returns the {@code onrejectionhandled} event handler for this element.
403      * @return the {@code onrejectionhandled} event handler for this element
404      */
405     @JsxGetter(CHROME)
406     public Function getOnrejectionhandled() {
407         return getEventHandler("rejectionhandled");
408     }
409 
410     /**
411      * Sets the {@code onrejectionhandled} event handler for this element.
412      * @param onrejectionhandled the {@code onrejectionhandled} event handler for this element
413      */
414     @JsxSetter(CHROME)
415     public void setOnrejectionhandled(final Object onrejectionhandled) {
416         setEventHandler("rejectionhandled", onrejectionhandled);
417     }
418 
419     /**
420      * Returns the {@code onstorage} event handler for this element.
421      * @return the {@code onstorage} event handler for this element
422      */
423     @JsxGetter
424     public Function getOnstorage() {
425         return getEventHandler("storage");
426     }
427 
428     /**
429      * Sets the {@code onstorage} event handler for this element.
430      * @param onstorage the {@code onstorage} event handler for this element
431      */
432     @JsxSetter
433     public void setOnstorage(final Object onstorage) {
434         setEventHandler("storage", onstorage);
435     }
436 
437     /**
438      * Returns the {@code onunhandledrejection} event handler for this element.
439      * @return the {@code onunhandledrejection} event handler for this element
440      */
441     @JsxGetter(CHROME)
442     public Function getOnunhandledrejection() {
443         return getEventHandler("unhandledrejection");
444     }
445 
446     /**
447      * Sets the {@code onunhandledrejection} event handler for this element.
448      * @param onunhandledrejection the {@code onunhandledrejection} event handler for this element
449      */
450     @JsxSetter(CHROME)
451     public void setOnunhandledrejection(final Object onunhandledrejection) {
452         setEventHandler("unhandledrejection", onunhandledrejection);
453     }
454 
455     /**
456      * Returns the {@code onunload} event handler for this element.
457      * @return the {@code onunload} event handler for this element
458      */
459     @JsxGetter
460     public Function getOnunload() {
461         return getEventHandler("unload");
462     }
463 
464     /**
465      * Sets the {@code onunload} event handler for this element.
466      * @param onunload the {@code onunload} event handler for this element
467      */
468     @JsxSetter
469     public void setOnunload(final Object onunload) {
470         setEventHandler("unload", onunload);
471     }
472 
473     /**
474      * Returns the {@code onafterprint} event handler for this element.
475      * @return the {@code onafterprint} event handler for this element
476      */
477     @JsxGetter({FF, IE})
478     public Function getOnafterprint() {
479         return getEventHandler("afterprint");
480     }
481 
482     /**
483      * Sets the {@code onafterprint} event handler for this element.
484      * @param onafterprint the {@code onafterprint} event handler for this element
485      */
486     @JsxSetter({FF, IE})
487     public void setOnafterprint(final Object onafterprint) {
488         setEventHandler("afterprint", onafterprint);
489     }
490 
491     /**
492      * Returns the {@code onbeforeprint} event handler for this element.
493      * @return the {@code onbeforeprint} event handler for this element
494      */
495     @JsxGetter({FF, IE})
496     public Function getOnbeforeprint() {
497         return getEventHandler("beforeprint");
498     }
499 
500     /**
501      * Sets the {@code onbeforeprint} event handler for this element.
502      * @param onbeforeprint the {@code onbeforeprint} event handler for this element
503      */
504     @JsxSetter({FF, IE})
505     public void setOnbeforeprint(final Object onbeforeprint) {
506         setEventHandler("beforeprint", onbeforeprint);
507     }
508 
509     /**
510      * {@inheritDoc}
511      */
512     @Override
513     @JsxGetter(IE)
514     public Function getOnresize() {
515         return super.getOnresize();
516     }
517 
518     /**
519      * {@inheritDoc}
520      */
521     @Override
522     @JsxSetter(IE)
523     public void setOnresize(final Object onresize) {
524         super.setOnresize(onresize);
525     }
526 
527 }