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