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