View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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.html;
16  
17  import java.util.Map;
18  
19  import org.htmlunit.SgmlPage;
20  import org.htmlunit.css.ComputedCssStyleDeclaration;
21  import org.htmlunit.javascript.host.event.Event;
22  
23  /**
24   * Wrapper for the HTML element "body".
25   *
26   * @author Mike Bowler
27   * @author David K. Taylor
28   * @author Christian Sell
29   * @author Ahmed Ashour
30   * @author Frank Danek
31   * @author Ronald Brill
32   */
33  public class HtmlBody extends HtmlElement {
34  
35      /** The HTML tag represented by this element. */
36      public static final String TAG_NAME = "body";
37  
38      /** Whether or not this body is temporary (created because the <code>body</code> tag has not yet been parsed). */
39      private final boolean temporary_;
40  
41      /**
42       * Creates a new instance.
43       *
44       * @param qualifiedName the qualified name of the element type to instantiate
45       * @param page the page that contains this element
46       * @param attributes the initial attributes
47       * @param temporary whether or not this body is temporary (created because the <code>body</code>
48       *        tag does not exist or has not yet been parsed)
49       */
50      public HtmlBody(final String qualifiedName, final SgmlPage page,
51              final Map<String, DomAttr> attributes, final boolean temporary) {
52  
53          super(qualifiedName, page, attributes);
54  
55          temporary_ = temporary;
56      }
57  
58      /**
59       * Returns the value of the attribute {@code onload}. Refer to the
60       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
61       * documentation for details on the use of this attribute.
62       *
63       * @return the value of the attribute {@code onload} or an empty string if that attribute isn't defined
64       */
65      public final String getOnLoadAttribute() {
66          return getAttributeDirect("onload");
67      }
68  
69      /**
70       * Returns the value of the attribute {@code onunload}. Refer to the
71       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
72       * documentation for details on the use of this attribute.
73       *
74       * @return the value of the attribute {@code onunload} or an empty string if that attribute isn't defined
75       */
76      public final String getOnUnloadAttribute() {
77          return getAttributeDirect("onunload");
78      }
79  
80      /**
81       * Returns the value of the attribute {@code background}. Refer to the
82       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
83       * documentation for details on the use of this attribute.
84       *
85       * @return the value of the attribute {@code background} or an empty string if that attribute isn't defined
86       */
87      public final String getBackgroundAttribute() {
88          return getAttributeDirect("background");
89      }
90  
91      /**
92       * Returns the value of the attribute {@code bgcolor}. Refer to the
93       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
94       * documentation for details on the use of this attribute.
95       *
96       * @return the value of the attribute {@code bgcolor} or an empty string if that attribute isn't defined
97       */
98      public final String getBgcolorAttribute() {
99          return getAttributeDirect("bgcolor");
100     }
101 
102     /**
103      * Returns the value of the attribute {@code text}. Refer to the
104      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
105      * documentation for details on the use of this attribute.
106      *
107      * @return the value of the attribute {@code text} or an empty string if that attribute isn't defined
108      */
109     public final String getTextAttribute() {
110         return getAttributeDirect("text");
111     }
112 
113     /**
114      * Returns the value of the attribute {@code link}. Refer to the
115      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
116      * documentation for details on the use of this attribute.
117      *
118      * @return the value of the attribute {@code link} or an empty string if that attribute isn't defined
119      */
120     public final String getLinkAttribute() {
121         return getAttributeDirect("link");
122     }
123 
124     /**
125      * Returns the value of the attribute {@code vlink}. Refer to the
126      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
127      * documentation for details on the use of this attribute.
128      *
129      * @return the value of the attribute {@code vlink} or an empty string if that attribute isn't defined
130      */
131     public final String getVlinkAttribute() {
132         return getAttributeDirect("vlink");
133     }
134 
135     /**
136      * Returns the value of the attribute {@code alink}. Refer to the
137      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
138      * documentation for details on the use of this attribute.
139      *
140      * @return the value of the attribute {@code alink} or an empty string if that attribute isn't defined
141      */
142     public final String getAlinkAttribute() {
143         return getAttributeDirect("alink");
144     }
145 
146     /**
147      * Returns {@code true} if this body is temporary (created because the <code>body</code> tag
148      * has not yet been parsed).
149      *
150      * @return {@code true} if this body is temporary (created because the <code>body</code> tag
151      *         has not yet been parsed)
152      */
153     public final boolean isTemporary() {
154         return temporary_;
155     }
156 
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     public boolean handles(final Event event) {
162         if (Event.TYPE_BLUR.equals(event.getType()) || Event.TYPE_FOCUS.equals(event.getType())) {
163             return true;
164         }
165         return super.handles(event);
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     @Override
172     @SuppressWarnings("PMD.AvoidDuplicateLiterals")
173     public void setDefaults(final ComputedCssStyleDeclaration style) {
174         style.setDefaultLocalStyleAttribute("margin-left", "8px");
175         style.setDefaultLocalStyleAttribute("margin-right", "8px");
176         style.setDefaultLocalStyleAttribute("margin-top", "8px");
177         style.setDefaultLocalStyleAttribute("margin-bottom", "8px");
178     }
179 }