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