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 static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
18  import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
19  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
20  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
21  
22  import org.htmlunit.corejs.javascript.Function;
23  import org.htmlunit.html.HtmlBody;
24  import org.htmlunit.html.HtmlElement;
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  import org.htmlunit.javascript.host.event.Event;
30  import org.htmlunit.util.StringUtils;
31  
32  /**
33   * The JavaScript object {@code HTMLBodyElement}.
34   *
35   * @author Ahmed Ashour
36   * @author Marc Guillemot
37   * @author Daniel Gredler
38   * @author Ronald Brill
39   */
40  @JsxClass(domClass = HtmlBody.class)
41  public class HTMLBodyElement extends HTMLElement {
42  
43      /**
44       * JavaScript constructor.
45       */
46      @Override
47      @JsxConstructor
48      public void jsConstructor() {
49          super.jsConstructor();
50      }
51  
52      /**
53       * Creates the event handler from the attribute value. This has to be done no matter which browser
54       * is simulated to handle ill-formed HTML code with many body (possibly generated) elements.
55       * @param attributeName the attribute name
56       * @param value the value
57       */
58      public void createEventHandlerFromAttribute(final String attributeName, final String value) {
59          // when many body tags are found while parsing, attributes of
60          // different tags are added and should create an event handler when needed
61          if (StringUtils.toRootLowerCase(attributeName).startsWith("on")) {
62              createEventHandler(attributeName.substring(2), value);
63          }
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      protected boolean isEventHandlerOnWindow() {
71          return true;
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public HTMLElement getOffsetParent_js() {
79          return null;
80      }
81  
82      /**
83       * Returns the value of the {@code aLink} attribute.
84       * @return the value of the {@code aLink} attribute
85       * @see <a href="http://msdn.microsoft.com/en-us/library/ms533070.aspx">MSDN Documentation</a>
86       */
87      @JsxGetter
88      public String getALink() {
89          return getDomNodeOrDie().getAttribute("aLink");
90      }
91  
92      /**
93       * Sets the value of the {@code aLink} attribute.
94       * @param aLink the value of the {@code aLink} attribute
95       * @see <a href="http://msdn.microsoft.com/en-us/library/ms533070.aspx">MSDN Documentation</a>
96       */
97      @JsxSetter
98      public void setALink(final String aLink) {
99          setColorAttribute("aLink", aLink);
100     }
101 
102     /**
103      * Returns the value of the {@code background} attribute.
104      * @return the value of the {@code background} attribute
105      * @see <a href="http://msdn.microsoft.com/en-us/library/ms533498.aspx">MSDN Documentation</a>
106      */
107     @JsxGetter
108     public String getBackground() {
109         final HtmlElement node = getDomNodeOrDie();
110         return node.getAttributeDirect("background");
111     }
112 
113     /**
114      * Sets the value of the {@code background} attribute.
115      * @param background the value of the {@code background} attribute
116      * @see <a href="http://msdn.microsoft.com/en-us/library/ms533498.aspx">MSDN Documentation</a>
117      */
118     @JsxSetter
119     public void setBackground(final String background) {
120         getDomNodeOrDie().setAttribute("background", background);
121     }
122 
123     /**
124      * Returns the value of the {@code bgColor} attribute.
125      * @return the value of the {@code bgColor} attribute
126      * @see <a href="http://msdn.microsoft.com/en-us/library/ms533505.aspx">MSDN Documentation</a>
127      */
128     @JsxGetter
129     public String getBgColor() {
130         return getDomNodeOrDie().getAttribute("bgColor");
131     }
132 
133     /**
134      * Sets the value of the {@code bgColor} attribute.
135      * @param bgColor the value of the {@code bgColor} attribute
136      * @see <a href="http://msdn.microsoft.com/en-us/library/ms533505.aspx">MSDN Documentation</a>
137      */
138     @JsxSetter
139     public void setBgColor(final String bgColor) {
140         setColorAttribute("bgColor", bgColor);
141     }
142 
143     /**
144      * Returns the value of the {@code link} attribute.
145      * @return the value of the {@code link} attribute
146      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534119.aspx">MSDN Documentation</a>
147      */
148     @JsxGetter
149     public String getLink() {
150         return getDomNodeOrDie().getAttributeDirect("link");
151     }
152 
153     /**
154      * Sets the value of the {@code link} attribute.
155      * @param link the value of the {@code link} attribute
156      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534119.aspx">MSDN Documentation</a>
157      */
158     @JsxSetter
159     public void setLink(final String link) {
160         setColorAttribute("link", link);
161     }
162 
163     /**
164      * Returns the value of the {@code text} attribute.
165      * @return the value of the {@code text} attribute
166      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534677.aspx">MSDN Documentation</a>
167      */
168     @JsxGetter
169     public String getText() {
170         return getDomNodeOrDie().getAttributeDirect("text");
171     }
172 
173     /**
174      * Sets the value of the {@code text} attribute.
175      * @param text the value of the {@code text} attribute
176      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534677.aspx">MSDN Documentation</a>
177      */
178     @JsxSetter
179     public void setText(final String text) {
180         setColorAttribute("text", text);
181     }
182 
183     /**
184      * Returns the value of the {@code vLink} attribute.
185      * @return the value of the {@code vLink} attribute
186      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534677.aspx">MSDN Documentation</a>
187      */
188     @JsxGetter
189     public String getVLink() {
190         return getDomNodeOrDie().getAttribute("vLink");
191     }
192 
193     /**
194      * Sets the value of the {@code vLink} attribute.
195      * @param vLink the value of the {@code vLink} attribute
196      * @see <a href="http://msdn.microsoft.com/en-us/library/ms534677.aspx">MSDN Documentation</a>
197      */
198     @JsxSetter
199     public void setVLink(final String vLink) {
200         setColorAttribute("vLink", vLink);
201     }
202 
203     /**
204      * {@inheritDoc}
205      */
206     @Override
207     public int getClientWidth() {
208         return super.getClientWidth() + 16;
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     @Override
215     @JsxGetter({CHROME, EDGE})
216     public Function getOnload() {
217         return super.getOnload();
218     }
219 
220     /**
221      * {@inheritDoc}
222      */
223     @Override
224     @JsxSetter({CHROME, EDGE})
225     public void setOnload(final Object onload) {
226         super.setOnload(onload);
227     }
228 
229     /**
230      * {@inheritDoc}
231      */
232     @Override
233     @JsxSetter({CHROME, EDGE})
234     public void setOnblur(final Object handler) {
235         super.setOnblur(handler);
236     }
237 
238     /**
239      * {@inheritDoc}
240      */
241     @Override
242     @JsxGetter({CHROME, EDGE})
243     public Function getOnblur() {
244         return super.getOnblur();
245     }
246 
247     /**
248      * {@inheritDoc}
249      */
250     @Override
251     @JsxSetter({CHROME, EDGE})
252     public void setOnfocus(final Object handler) {
253         super.setOnfocus(handler);
254     }
255 
256     /**
257      * {@inheritDoc}
258      */
259     @Override
260     @JsxGetter({CHROME, EDGE})
261     public Function getOnfocus() {
262         return super.getOnfocus();
263     }
264 
265     /**
266      * {@inheritDoc}
267      */
268     @Override
269     @JsxSetter({CHROME, EDGE})
270     public void setOnerror(final Object handler) {
271         super.setOnerror(handler);
272     }
273 
274     /**
275      * {@inheritDoc}
276      */
277     @Override
278     @JsxGetter({CHROME, EDGE})
279     public Function getOnerror() {
280         return super.getOnerror();
281     }
282 
283     /**
284      * Returns the {@code onbeforeunload} event handler for this element.
285      * @return the {@code onbeforeunload} event handler for this element
286      */
287     @JsxGetter
288     public Function getOnbeforeunload() {
289         return getEventHandler(Event.TYPE_BEFORE_UNLOAD);
290     }
291 
292     /**
293      * Sets the {@code onbeforeunload} event handler for this element.
294      * @param onbeforeunload the {@code onbeforeunload} event handler for this element
295      */
296     @JsxSetter
297     public void setOnbeforeunload(final Object onbeforeunload) {
298         setEventHandler(Event.TYPE_BEFORE_UNLOAD, onbeforeunload);
299     }
300 
301     /**
302      * Returns the {@code ongamepadconnected} event handler.
303      * @return the {@code ongamepadconnected} event handler
304      */
305     @JsxGetter({FF, FF_ESR})
306     public Function getOngamepadconnected() {
307         return getEventHandler(Event.TYPE_GAMEPAD_CONNECTED);
308     }
309 
310     /**
311      * Sets the {@code ongamepadconnected} event handler.
312      * @param gamepadconnected the {@code ongamepadconnected} event handler
313      */
314     @JsxSetter({FF, FF_ESR})
315     public void setOngamepadconnected(final Object gamepadconnected) {
316         setEventHandler(Event.TYPE_GAMEPAD_CONNECTED, gamepadconnected);
317     }
318 
319     /**
320      * Returns the {@code ongamepaddisconnected} event handler.
321      * @return the {@code ongamepaddisconnected} event handler
322      */
323     @JsxGetter({FF, FF_ESR})
324     public Function getOngamepaddisconnected() {
325         return getEventHandler(Event.TYPE_GAMEPAD_DISCONNECTED);
326     }
327 
328     /**
329      * Sets the {@code ongamepaddisconnected} event handler.
330      * @param gamepaddisconnected the {@code ongamepaddisconnected} event handler
331      */
332     @JsxSetter({FF, FF_ESR})
333     public void setOngamepaddisconnected(final Object gamepaddisconnected) {
334         setEventHandler(Event.TYPE_GAMEPAD_DISCONNECTED, gamepaddisconnected);
335     }
336 
337     /**
338      * Returns the {@code onhashchange} event handler for this element.
339      * @return the {@code onhashchange} event handler for this element
340      */
341     @JsxGetter
342     public Function getOnhashchange() {
343         return getEventHandler(Event.TYPE_HASH_CHANGE);
344     }
345 
346     /**
347      * Sets the {@code onhashchange} event handler for this element.
348      * @param onhashchange the {@code onhashchange} event handler for this element
349      */
350     @JsxSetter
351     public void setOnhashchange(final Object onhashchange) {
352         setEventHandler(Event.TYPE_HASH_CHANGE, onhashchange);
353     }
354 
355     /**
356      * Returns the {@code onlanguagechange} event handler for this element.
357      * @return the {@code onlanguagechange} event handler for this element
358      */
359     @JsxGetter
360     public Function getOnlanguagechange() {
361         return getEventHandler(Event.TYPE_LANGUAGECHANGE);
362     }
363 
364     /**
365      * Sets the {@code onlanguagechange} event handler for this element.
366      * @param onlanguagechange the {@code onlanguagechange} event handler for this element
367      */
368     @JsxSetter
369     public void setOnlanguagechange(final Object onlanguagechange) {
370         setEventHandler(Event.TYPE_LANGUAGECHANGE, onlanguagechange);
371     }
372 
373     /**
374      * Returns the {@code onmessage} event handler for this element.
375      * @return the {@code onmessage} event handler for this element
376      */
377     @JsxGetter
378     public Function getOnmessage() {
379         return getEventHandler(Event.TYPE_MESSAGE);
380     }
381 
382     /**
383      * Sets the {@code onmessage} event handler for this element.
384      * @param onmessage the {@code onmessage} event handler for this element
385      */
386     @JsxSetter
387     public void setOnmessage(final Object onmessage) {
388         setEventHandler(Event.TYPE_MESSAGE, onmessage);
389     }
390 
391     /**
392      * Returns the {@code onoffline} event handler for this element.
393      * @return the {@code onoffline} event handler for this element
394      */
395     @JsxGetter
396     public Function getOnoffline() {
397         return getEventHandler(Event.TYPE_OFFLINE);
398     }
399 
400     /**
401      * Sets the {@code onoffline} event handler for this element.
402      * @param onoffline the {@code onoffline} event handler for this element
403      */
404     @JsxSetter
405     public void setOnoffline(final Object onoffline) {
406         setEventHandler(Event.TYPE_OFFLINE, onoffline);
407     }
408 
409     /**
410      * Returns the {@code ononline} event handler for this element.
411      * @return the {@code ononline} event handler for this element
412      */
413     @JsxGetter
414     public Function getOnonline() {
415         return getEventHandler(Event.TYPE_ONLINE);
416     }
417 
418     /**
419      * Sets the {@code ononline} event handler for this element.
420      * @param ononline the {@code ononline} event handler for this element
421      */
422     @JsxSetter
423     public void setOnonline(final Object ononline) {
424         setEventHandler(Event.TYPE_ONLINE, ononline);
425     }
426 
427     /**
428      * Returns the {@code onpagehide} event handler for this element.
429      * @return the {@code onpagehide} event handler for this element
430      */
431     @JsxGetter
432     public Function getOnpagehide() {
433         return getEventHandler(Event.TYPE_PAGEHIDE);
434     }
435 
436     /**
437      * Sets the {@code onpagehide} event handler for this element.
438      * @param onpagehide the {@code onpagehide} event handler for this element
439      */
440     @JsxSetter
441     public void setOnpagehide(final Object onpagehide) {
442         setEventHandler(Event.TYPE_PAGEHIDE, onpagehide);
443     }
444 
445     /**
446      * Returns the {@code onpageshow} event handler for this element.
447      * @return the {@code onpageshow} event handler for this element
448      */
449     @JsxGetter
450     public Function getOnpageshow() {
451         return getEventHandler(Event.TYPE_PAGESHOW);
452     }
453 
454     /**
455      * Sets the {@code onpageshow} event handler for this element.
456      * @param onpageshow the {@code onpageshow} event handler for this element
457      */
458     @JsxSetter
459     public void setOnpageshow(final Object onpageshow) {
460         setEventHandler(Event.TYPE_PAGESHOW, onpageshow);
461     }
462 
463     /**
464      * Returns the {@code onpopstate} event handler for this element.
465      * @return the {@code onpopstate} event handler for this element
466      */
467     @JsxGetter
468     public Function getOnpopstate() {
469         return getEventHandler(Event.TYPE_POPSTATE);
470     }
471 
472     /**
473      * Sets the {@code onpopstate} event handler for this element.
474      * @param onpopstate the {@code onpopstate} event handler for this element
475      */
476     @JsxSetter
477     public void setOnpopstate(final Object onpopstate) {
478         setEventHandler(Event.TYPE_POPSTATE, onpopstate);
479     }
480 
481     /**
482      * Returns the {@code onrejectionhandled} event handler for this element.
483      * @return the {@code onrejectionhandled} event handler for this element
484      */
485     @JsxGetter
486     public Function getOnrejectionhandled() {
487         return getEventHandler(Event.TYPE_REJECTIONHANDLED);
488     }
489 
490     /**
491      * Sets the {@code onrejectionhandled} event handler for this element.
492      * @param onrejectionhandled the {@code onrejectionhandled} event handler for this element
493      */
494     @JsxSetter
495     public void setOnrejectionhandled(final Object onrejectionhandled) {
496         setEventHandler(Event.TYPE_REJECTIONHANDLED, onrejectionhandled);
497     }
498 
499     /**
500      * Returns the {@code onstorage} event handler for this element.
501      * @return the {@code onstorage} event handler for this element
502      */
503     @JsxGetter
504     public Function getOnstorage() {
505         return getEventHandler(Event.TYPE_STORAGE);
506     }
507 
508     /**
509      * Sets the {@code onstorage} event handler for this element.
510      * @param onstorage the {@code onstorage} event handler for this element
511      */
512     @JsxSetter
513     public void setOnstorage(final Object onstorage) {
514         setEventHandler(Event.TYPE_STORAGE, onstorage);
515     }
516 
517     /**
518      * Returns the {@code onunhandledrejection} event handler for this element.
519      * @return the {@code onunhandledrejection} event handler for this element
520      */
521     @JsxGetter
522     public Function getOnunhandledrejection() {
523         return getEventHandler(Event.TYPE_UNHANDLEDREJECTION);
524     }
525 
526     /**
527      * Sets the {@code onunhandledrejection} event handler for this element.
528      * @param onunhandledrejection the {@code onunhandledrejection} event handler for this element
529      */
530     @JsxSetter
531     public void setOnunhandledrejection(final Object onunhandledrejection) {
532         setEventHandler(Event.TYPE_UNHANDLEDREJECTION, onunhandledrejection);
533     }
534 
535     /**
536      * Returns the {@code onunload} event handler for this element.
537      * @return the {@code onunload} event handler for this element
538      */
539     @JsxGetter
540     public Function getOnunload() {
541         return getEventHandler(Event.TYPE_UNLOAD);
542     }
543 
544     /**
545      * Sets the {@code onunload} event handler for this element.
546      * @param onunload the {@code onunload} event handler for this element
547      */
548     @JsxSetter
549     public void setOnunload(final Object onunload) {
550         setEventHandler(Event.TYPE_UNLOAD, onunload);
551     }
552 
553     /**
554      * Returns the {@code onafterprint} event handler for this element.
555      * @return the {@code onafterprint} event handler for this element
556      */
557     @JsxGetter
558     public Function getOnafterprint() {
559         return getEventHandler(Event.TYPE_AFTERPRINT);
560     }
561 
562     /**
563      * Sets the {@code onafterprint} event handler for this element.
564      * @param onafterprint the {@code onafterprint} event handler for this element
565      */
566     @JsxSetter
567     public void setOnafterprint(final Object onafterprint) {
568         setEventHandler(Event.TYPE_AFTERPRINT, onafterprint);
569     }
570 
571     /**
572      * Returns the {@code onbeforeprint} event handler for this element.
573      * @return the {@code onbeforeprint} event handler for this element
574      */
575     @JsxGetter
576     public Function getOnbeforeprint() {
577         return getEventHandler(Event.TYPE_BEFOREPRINT);
578     }
579 
580     /**
581      * Sets the {@code onbeforeprint} event handler for this element.
582      * @param onbeforeprint the {@code onbeforeprint} event handler for this element
583      */
584     @JsxSetter
585     public void setOnbeforeprint(final Object onbeforeprint) {
586         setEventHandler(Event.TYPE_BEFOREPRINT, onbeforeprint);
587     }
588 
589     /**
590      * Returns the {@code onmessageerror} event handler for this element.
591      * @return the {@code onmessageerror} event handler for this element
592      */
593     @JsxGetter
594     public Function getOnmessageerror() {
595         return getEventHandler(Event.TYPE_ONMESSAGEERROR);
596     }
597 
598     /**
599      * Sets the {@code onmessageerror} event handler for this element.
600      * @param onmessageerror the {@code onmessageerror} event handler for this element
601      */
602     @JsxSetter
603     public void setOnmessageerror(final Object onmessageerror) {
604         setEventHandler(Event.TYPE_ONMESSAGEERROR, onmessageerror);
605     }
606 
607     /**
608      * {@inheritDoc}
609      */
610     @Override
611     @JsxGetter({CHROME, EDGE})
612     public Function getOnresize() {
613         return super.getOnresize();
614     }
615 
616     /**
617      * {@inheritDoc}
618      */
619     @Override
620     @JsxSetter({CHROME, EDGE})
621     public void setOnresize(final Object onresize) {
622         super.setOnresize(onresize);
623     }
624 
625     /**
626      * {@inheritDoc}
627      */
628     @Override
629     @JsxGetter({CHROME, EDGE})
630     public Function getOnscroll() {
631         return super.getOnscroll();
632     }
633 
634     /**
635      * {@inheritDoc}
636      */
637     @Override
638     @JsxSetter({CHROME, EDGE})
639     public void setOnscroll(final Object onresize) {
640         super.setOnscroll(onresize);
641     }
642 }