Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / html / HTMLTableCellElement.cpp
blobc2b7642a642a7728b7d6e57fe441721ca0fe17d6
1 /*
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.
25 #include "config.h"
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/html/parser/HTMLParserIdioms.h"
35 #include "core/layout/LayoutTableCell.h"
37 using namespace std;
38 using namespace std;
40 namespace blink {
42 // Clamp rowspan and colspan at 8k.
43 // Firefox used a limit of 8190 for rowspan but they changed it to 65,534.
44 // (FIXME: We should consider increasing this limit (crbug.com/78577).
45 // Firefox uses a limit of 1,000 for colspan and resets the value to 1
46 // but we don't discriminate between rowspan / colspan as it is artificial.
47 static const unsigned maxColRowSpan = 8190;
49 using namespace HTMLNames;
51 inline HTMLTableCellElement::HTMLTableCellElement(const QualifiedName& tagName, Document& document)
52 : HTMLTablePartElement(tagName, document)
56 DEFINE_ELEMENT_FACTORY_WITH_TAGNAME(HTMLTableCellElement)
58 unsigned HTMLTableCellElement::colSpan() const
60 const AtomicString& colSpanValue = fastGetAttribute(colspanAttr);
61 unsigned value = 0;
62 if (colSpanValue.isEmpty() || !parseHTMLNonNegativeInteger(colSpanValue, value))
63 return 1;
64 return max(1u, min(value, maxColRowSpan));
67 unsigned HTMLTableCellElement::rowSpan() const
69 const AtomicString& rowSpanValue = fastGetAttribute(rowspanAttr);
70 unsigned value = 0;
71 if (rowSpanValue.isEmpty() || !parseHTMLNonNegativeInteger(rowSpanValue, value))
72 return 1;
73 return max(1u, min(value, maxColRowSpan));
76 int HTMLTableCellElement::cellIndex() const
78 if (!isHTMLTableRowElement(parentElement()))
79 return -1;
81 int index = 0;
82 for (const HTMLTableCellElement* element = Traversal<HTMLTableCellElement>::previousSibling(*this); element; element = Traversal<HTMLTableCellElement>::previousSibling(*element))
83 ++index;
85 return index;
88 bool HTMLTableCellElement::isPresentationAttribute(const QualifiedName& name) const
90 if (name == nowrapAttr || name == widthAttr || name == heightAttr)
91 return true;
92 return HTMLTablePartElement::isPresentationAttribute(name);
95 void HTMLTableCellElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
97 if (name == nowrapAttr) {
98 addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValueWebkitNowrap);
99 } else if (name == widthAttr) {
100 if (!value.isEmpty()) {
101 int widthInt = value.toInt();
102 if (widthInt > 0) // width="0" is ignored for compatibility with WinIE.
103 addHTMLLengthToStyle(style, CSSPropertyWidth, value);
105 } else if (name == heightAttr) {
106 if (!value.isEmpty()) {
107 int heightInt = value.toInt();
108 if (heightInt > 0) // height="0" is ignored for compatibility with WinIE.
109 addHTMLLengthToStyle(style, CSSPropertyHeight, value);
111 } else {
112 HTMLTablePartElement::collectStyleForPresentationAttribute(name, value, style);
116 void HTMLTableCellElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
118 if (name == rowspanAttr) {
119 if (layoutObject() && layoutObject()->isTableCell())
120 toLayoutTableCell(layoutObject())->colSpanOrRowSpanChanged();
121 } else if (name == colspanAttr) {
122 if (layoutObject() && layoutObject()->isTableCell())
123 toLayoutTableCell(layoutObject())->colSpanOrRowSpanChanged();
124 } else {
125 HTMLTablePartElement::parseAttribute(name, value);
129 const StylePropertySet* HTMLTableCellElement::additionalPresentationAttributeStyle()
131 if (HTMLTableElement* table = findParentTable())
132 return table->additionalCellStyle();
133 return nullptr;
136 bool HTMLTableCellElement::isURLAttribute(const Attribute& attribute) const
138 return attribute.name() == backgroundAttr || HTMLTablePartElement::isURLAttribute(attribute);
141 bool HTMLTableCellElement::hasLegalLinkAttribute(const QualifiedName& name) const
143 return (hasTagName(tdTag) && name == backgroundAttr) || HTMLTablePartElement::hasLegalLinkAttribute(name);
146 const QualifiedName& HTMLTableCellElement::subResourceAttributeName() const
148 return hasTagName(tdTag) ? backgroundAttr : HTMLTablePartElement::subResourceAttributeName();
151 const AtomicString& HTMLTableCellElement::abbr() const
153 return fastGetAttribute(abbrAttr);
156 const AtomicString& HTMLTableCellElement::axis() const
158 return fastGetAttribute(axisAttr);
161 void HTMLTableCellElement::setColSpan(unsigned n)
163 setUnsignedIntegralAttribute(colspanAttr, n);
166 const AtomicString& HTMLTableCellElement::headers() const
168 return fastGetAttribute(headersAttr);
171 void HTMLTableCellElement::setRowSpan(unsigned n)
173 setUnsignedIntegralAttribute(rowspanAttr, n);
176 const AtomicString& HTMLTableCellElement::scope() const
178 return fastGetAttribute(scopeAttr);
181 HTMLTableCellElement* HTMLTableCellElement::cellAbove() const
183 LayoutObject* cellLayoutObject = layoutObject();
184 if (!cellLayoutObject)
185 return nullptr;
186 if (!cellLayoutObject->isTableCell())
187 return nullptr;
189 LayoutTableCell* tableCellLayoutObject = toLayoutTableCell(cellLayoutObject);
190 LayoutTableCell* cellAboveLayoutObject = tableCellLayoutObject->table()->cellAbove(tableCellLayoutObject);
191 if (!cellAboveLayoutObject)
192 return nullptr;
194 return toHTMLTableCellElement(cellAboveLayoutObject->node());
197 } // namespace blink