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.util.StringUtils;
21  
22  /**
23   * An abstract cell that provides the implementation for HtmlTableDataCell and HtmlTableHeaderCell.
24   *
25   * @see HtmlTableDataCell
26   * @see HtmlTableHeaderCell
27   *
28   * @author Mike Bowler
29   * @author David K. Taylor
30   * @author Christian Sell
31   * @author Ahmed Ashour
32   * @author Frank Danek
33   * @author Lai Quang Duong
34   * @author Ronald Brill
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              return Math.min(span, 1_000);
64          }
65          catch (final NumberFormatException e) {
66              return 1;
67          }
68      }
69  
70      /**
71       * @return the value of the rowspan attribute, or <code>1</code> if the attribute wasn't specified
72       */
73      public int getRowSpan() {
74          final String spanString = StringUtils.replaceChars(getAttributeDirect("rowspan"), "\r\n\t ", null);
75          if (spanString == null || spanString.isEmpty()) {
76              return 1;
77          }
78          try {
79              final int span = (int) Double.parseDouble(spanString);
80              if (span < 0) {
81                  return 1;
82              }
83              if (span < 1) {
84                  return 0;
85              }
86  
87              return Math.min(span, 65_534);
88          }
89          catch (final NumberFormatException e) {
90              return 1;
91          }
92      }
93  
94      /**
95       * Returns the table row containing this cell.
96       * @return the table row containing this cell
97       */
98      public HtmlTableRow getEnclosingRow() {
99          return (HtmlTableRow) getEnclosingElement("tr");
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public DisplayStyle getDefaultStyleDisplay() {
107         return DisplayStyle.TABLE_CELL;
108     }
109 }