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 * Returns the column span of this cell.
52 *
53 * @return the value of the {@code colspan} attribute, or {@code 1} if the
54 * attribute was not specified or is invalid
55 */
56 public int getColumnSpan() {
57 final String spanString = StringUtils.replaceChars(getAttributeDirect("colspan"), "\r\n\t ", null);
58 if (spanString == null || spanString.isEmpty()) {
59 return 1;
60 }
61 try {
62 final int span = (int) Double.parseDouble(spanString);
63 if (span < 1) {
64 return 1;
65 }
66 return Math.min(span, 1_000);
67 }
68 catch (final NumberFormatException e) {
69 return 1;
70 }
71 }
72
73 /**
74 * Returns the row span of this cell.
75 *
76 * @return the value of the {@code rowspan} attribute, or {@code 1} if the
77 * attribute was not specified or is invalid
78 */
79 public int getRowSpan() {
80 final String spanString = StringUtils.replaceChars(getAttributeDirect("rowspan"), "\r\n\t ", null);
81 if (spanString == null || spanString.isEmpty()) {
82 return 1;
83 }
84 try {
85 final int span = (int) Double.parseDouble(spanString);
86 if (span < 0) {
87 return 1;
88 }
89 if (span < 1) {
90 return 0;
91 }
92
93 return Math.min(span, 65_534);
94 }
95 catch (final NumberFormatException e) {
96 return 1;
97 }
98 }
99
100 /**
101 * Returns the table row containing this cell.
102 * @return the table row containing this cell
103 */
104 public HtmlTableRow getEnclosingRow() {
105 return (HtmlTableRow) getEnclosingElement("tr");
106 }
107
108 /**
109 * {@inheritDoc}
110 */
111 @Override
112 public DisplayStyle getDefaultStyleDisplay() {
113 return DisplayStyle.TABLE_CELL;
114 }
115 }