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 static org.htmlunit.BrowserVersionFeatures.JS_TABLE_SPAN_SET_ZERO_IF_INVALID;
18  
19  import java.util.Map;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.htmlunit.SgmlPage;
23  
24  /**
25   * An abstract cell that provides the implementation for HtmlTableDataCell and HtmlTableHeaderCell.
26   *
27   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
28   * @author David K. Taylor
29   * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
30   * @author Ahmed Ashour
31   * @author Frank Danek
32   * @author Lai Quang Duong
33   * @see HtmlTableDataCell
34   * @see HtmlTableHeaderCell
35   */
36  public abstract class HtmlTableCell extends HtmlElement {
37  
38      /**
39       * Creates an instance.
40       *
41       * @param qualifiedName the qualified name of the element type to instantiate
42       * @param page the page that this element is contained within
43       * @param attributes the initial attributes
44       */
45      protected HtmlTableCell(final String qualifiedName, final SgmlPage page,
46              final Map<String, DomAttr> attributes) {
47          super(qualifiedName, page, attributes);
48      }
49  
50      /**
51       * @return the value of the colspan attribute, or <code>1</code> if the attribute wasn't specified
52       */
53      public int getColumnSpan() {
54          final String spanString = StringUtils.replaceChars(getAttributeDirect("colspan"), "\r\n\t ", null);
55          if (spanString == null || spanString.isEmpty()) {
56              return 1;
57          }
58          try {
59              final int span = (int) Double.parseDouble(spanString);
60              if (span < 1) {
61                  return 1;
62              }
63              if (span > 1_000) {
64                  return 1_000;
65              }
66              return span;
67          }
68          catch (final NumberFormatException e) {
69              return 1;
70          }
71      }
72  
73      /**
74       * @return the value of the rowspan attribute, or <code>1</code> if the attribute wasn't specified
75       */
76      public int getRowSpan() {
77          final String spanString = StringUtils.replaceChars(getAttributeDirect("rowspan"), "\r\n\t ", null);
78          if (spanString == null || spanString.isEmpty()) {
79              return 1;
80          }
81          try {
82              final int span = (int) Double.parseDouble(spanString);
83              if (getPage().getWebClient().getBrowserVersion().hasFeature(JS_TABLE_SPAN_SET_ZERO_IF_INVALID)) {
84                  if (span < 0) {
85                      return 1;
86                  }
87                  if (span < 1) {
88                      return 0;
89                  }
90              }
91              else {
92                  if (span < 1) {
93                      return 1;
94                  }
95              }
96  
97              if (span > 65_534) {
98                  return 65_534;
99              }
100             return span;
101         }
102         catch (final NumberFormatException e) {
103             return 1;
104         }
105     }
106 
107     /**
108      * Returns the table row containing this cell.
109      * @return the table row containing this cell
110      */
111     public HtmlTableRow getEnclosingRow() {
112         return (HtmlTableRow) getEnclosingElement("tr");
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public DisplayStyle getDefaultStyleDisplay() {
120         return DisplayStyle.TABLE_CELL;
121     }
122 }