2 * Copyright (C) 1997 Martin Jones (mjones@kde.org)
3 * (C) 1997 Torben Weis (weis@kde.org)
4 * (C) 1998 Waldo Bastian (bastian@kde.org)
5 * (C) 1999 Lars Knoll (knoll@kde.org)
6 * (C) 1999 Antti Koivisto (koivisto@kde.org)
7 * Copyright (C) 2003, 2004, 2005, 2006, 2010 Apple Inc. All rights reserved.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 #include "core/html/HTMLTableCellElement.h"
28 #include "core/CSSPropertyNames.h"
29 #include "core/CSSValueKeywords.h"
30 #include "core/HTMLNames.h"
31 #include "core/dom/Attribute.h"
32 #include "core/dom/ElementTraversal.h"
33 #include "core/html/HTMLTableElement.h"
34 #include "core/rendering/RenderTableCell.h"
41 // Clamp rowspan and colspan at 8k.
42 // Firefox used a limit of 8190 for rowspan but they changed it to 65,534.
43 // (FIXME: We should consider increasing this limit (crbug.com/78577).
44 // Firefox uses a limit of 1,000 for colspan and resets the value to 1
45 // but we don't discriminate between rowspan / colspan as it is artificial.
46 static const int maxColRowSpan = 8190;
48 using namespace HTMLNames;
50 inline HTMLTableCellElement::HTMLTableCellElement(const QualifiedName& tagName, Document& document)
51 : HTMLTablePartElement(tagName, document)
53 ScriptWrappable::init(this);
56 DEFINE_ELEMENT_FACTORY_WITH_TAGNAME(HTMLTableCellElement)
58 int HTMLTableCellElement::colSpan() const
60 const AtomicString& colSpanValue = fastGetAttribute(colspanAttr);
61 return max(1, min(colSpanValue.toInt(), maxColRowSpan));
64 int HTMLTableCellElement::rowSpan() const
66 const AtomicString& rowSpanValue = fastGetAttribute(rowspanAttr);
67 return max(1, min(rowSpanValue.toInt(), maxColRowSpan));
70 int HTMLTableCellElement::cellIndex() const
72 if (!isHTMLTableRowElement(parentElement()))
76 for (const HTMLTableCellElement* element = Traversal<HTMLTableCellElement>::previousSibling(*this); element; element = Traversal<HTMLTableCellElement>::previousSibling(*element))
82 bool HTMLTableCellElement::isPresentationAttribute(const QualifiedName& name) const
84 if (name == nowrapAttr || name == widthAttr || name == heightAttr)
86 return HTMLTablePartElement::isPresentationAttribute(name);
89 void HTMLTableCellElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
91 if (name == nowrapAttr)
92 addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValueWebkitNowrap);
93 else if (name == widthAttr) {
94 if (!value.isEmpty()) {
95 int widthInt = value.toInt();
96 if (widthInt > 0) // width="0" is ignored for compatibility with WinIE.
97 addHTMLLengthToStyle(style, CSSPropertyWidth, value);
99 } else if (name == heightAttr) {
100 if (!value.isEmpty()) {
101 int heightInt = value.toInt();
102 if (heightInt > 0) // height="0" is ignored for compatibility with WinIE.
103 addHTMLLengthToStyle(style, CSSPropertyHeight, value);
106 HTMLTablePartElement::collectStyleForPresentationAttribute(name, value, style);
109 void HTMLTableCellElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
111 if (name == rowspanAttr) {
112 if (renderer() && renderer()->isTableCell())
113 toRenderTableCell(renderer())->colSpanOrRowSpanChanged();
114 } else if (name == colspanAttr) {
115 if (renderer() && renderer()->isTableCell())
116 toRenderTableCell(renderer())->colSpanOrRowSpanChanged();
118 HTMLTablePartElement::parseAttribute(name, value);
121 const StylePropertySet* HTMLTableCellElement::additionalPresentationAttributeStyle()
123 if (HTMLTableElement* table = findParentTable())
124 return table->additionalCellStyle();
128 bool HTMLTableCellElement::isURLAttribute(const Attribute& attribute) const
130 return attribute.name() == backgroundAttr || HTMLTablePartElement::isURLAttribute(attribute);
133 bool HTMLTableCellElement::hasLegalLinkAttribute(const QualifiedName& name) const
135 return (hasTagName(tdTag) && name == backgroundAttr) || HTMLTablePartElement::hasLegalLinkAttribute(name);
138 const QualifiedName& HTMLTableCellElement::subResourceAttributeName() const
140 return hasTagName(tdTag) ? backgroundAttr : HTMLTablePartElement::subResourceAttributeName();
143 const AtomicString& HTMLTableCellElement::abbr() const
145 return fastGetAttribute(abbrAttr);
148 const AtomicString& HTMLTableCellElement::axis() const
150 return fastGetAttribute(axisAttr);
153 void HTMLTableCellElement::setColSpan(int n)
155 setIntegralAttribute(colspanAttr, n);
158 const AtomicString& HTMLTableCellElement::headers() const
160 return fastGetAttribute(headersAttr);
163 void HTMLTableCellElement::setRowSpan(int n)
165 setIntegralAttribute(rowspanAttr, n);
168 const AtomicString& HTMLTableCellElement::scope() const
170 return fastGetAttribute(scopeAttr);
173 HTMLTableCellElement* HTMLTableCellElement::cellAbove() const
175 RenderObject* cellRenderer = renderer();
178 if (!cellRenderer->isTableCell())
181 RenderTableCell* tableCellRenderer = toRenderTableCell(cellRenderer);
182 RenderTableCell* cellAboveRenderer = tableCellRenderer->table()->cellAbove(tableCellRenderer);
183 if (!cellAboveRenderer)
186 return toHTMLTableCellElement(cellAboveRenderer->node());