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