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