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