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.
8 *
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.
13 *
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.
18 *
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.
23 */
24
25#include "config.h"
26#include "HTMLTableCellElement.h"
27
28#include "Attribute.h"
29#include "CSSPropertyNames.h"
30#include "CSSValueKeywords.h"
31#include "HTMLNames.h"
32#include "HTMLTableElement.h"
33#include "RenderTableCell.h"
34
35using std::max;
36using std::min;
37
38namespace WebCore {
39
40// Clamp rowspan at 8k to match Firefox.
41static const int maxRowspan = 8190;
42
43using namespace HTMLNames;
44
45inline HTMLTableCellElement::HTMLTableCellElement(const QualifiedName& tagName, Document* document)
46    : HTMLTablePartElement(tagName, document)
47{
48}
49
50PassRefPtr<HTMLTableCellElement> HTMLTableCellElement::create(const QualifiedName& tagName, Document* document)
51{
52    return adoptRef(new HTMLTableCellElement(tagName, document));
53}
54
55int HTMLTableCellElement::colSpan() const
56{
57    const AtomicString& colSpanValue = fastGetAttribute(colspanAttr);
58    return max(1, colSpanValue.toInt());
59}
60
61int HTMLTableCellElement::rowSpan() const
62{
63    const AtomicString& rowSpanValue = fastGetAttribute(rowspanAttr);
64    return max(1, min(rowSpanValue.toInt(), maxRowspan));
65}
66
67int HTMLTableCellElement::cellIndex() const
68{
69    int index = 0;
70    if (!parentElement() || !parentElement()->hasTagName(trTag))
71        return -1;
72
73    for (const Node * node = previousSibling(); node; node = node->previousSibling()) {
74        if (node->hasTagName(tdTag) || node->hasTagName(thTag))
75            index++;
76    }
77
78    return index;
79}
80
81bool HTMLTableCellElement::isPresentationAttribute(const QualifiedName& name) const
82{
83    if (name == nowrapAttr || name == widthAttr || name == heightAttr)
84        return true;
85    return HTMLTablePartElement::isPresentationAttribute(name);
86}
87
88void HTMLTableCellElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
89{
90    if (name == nowrapAttr)
91        addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValueWebkitNowrap);
92    else if (name == widthAttr) {
93        if (!value.isEmpty()) {
94            int widthInt = value.toInt();
95            if (widthInt > 0) // width="0" is ignored for compatibility with WinIE.
96                addHTMLLengthToStyle(style, CSSPropertyWidth, value);
97        }
98    } else if (name == heightAttr) {
99        if (!value.isEmpty()) {
100            int heightInt = value.toInt();
101            if (heightInt > 0) // height="0" is ignored for compatibility with WinIE.
102                addHTMLLengthToStyle(style, CSSPropertyHeight, value);
103        }
104    } else
105        HTMLTablePartElement::collectStyleForPresentationAttribute(name, value, style);
106}
107
108void HTMLTableCellElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
109{
110    if (name == rowspanAttr) {
111        if (renderer() && renderer()->isTableCell())
112            toRenderTableCell(renderer())->colSpanOrRowSpanChanged();
113    } else if (name == colspanAttr) {
114        if (renderer() && renderer()->isTableCell())
115            toRenderTableCell(renderer())->colSpanOrRowSpanChanged();
116    } else
117        HTMLTablePartElement::parseAttribute(name, value);
118}
119
120const StylePropertySet* HTMLTableCellElement::additionalPresentationAttributeStyle()
121{
122    if (HTMLTableElement* table = findParentTable())
123        return table->additionalCellStyle();
124    return 0;
125}
126
127bool HTMLTableCellElement::isURLAttribute(const Attribute& attribute) const
128{
129    return attribute.name() == backgroundAttr || HTMLTablePartElement::isURLAttribute(attribute);
130}
131
132String HTMLTableCellElement::abbr() const
133{
134    return getAttribute(abbrAttr);
135}
136
137String HTMLTableCellElement::axis() const
138{
139    return getAttribute(axisAttr);
140}
141
142void HTMLTableCellElement::setColSpan(int n)
143{
144    setAttribute(colspanAttr, String::number(n));
145}
146
147String HTMLTableCellElement::headers() const
148{
149    return getAttribute(headersAttr);
150}
151
152void HTMLTableCellElement::setRowSpan(int n)
153{
154    setAttribute(rowspanAttr, String::number(n));
155}
156
157String HTMLTableCellElement::scope() const
158{
159    return getAttribute(scopeAttr);
160}
161
162void HTMLTableCellElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
163{
164    HTMLTablePartElement::addSubresourceAttributeURLs(urls);
165
166    addSubresourceURL(urls, document()->completeURL(getAttribute(backgroundAttr)));
167}
168
169HTMLTableCellElement* HTMLTableCellElement::cellAbove() const
170{
171    RenderObject* cellRenderer = renderer();
172    if (!cellRenderer)
173        return 0;
174    if (!cellRenderer->isTableCell())
175        return 0;
176
177    RenderTableCell* tableCellRenderer = toRenderTableCell(cellRenderer);
178    RenderTableCell* cellAboveRenderer = tableCellRenderer->table()->cellAbove(tableCellRenderer);
179    if (!cellAboveRenderer)
180        return 0;
181
182    return static_cast<HTMLTableCellElement*>(cellAboveRenderer->node());
183}
184
185#ifndef NDEBUG
186
187HTMLTableCellElement* toHTMLTableCellElement(Node* node)
188{
189    ASSERT_WITH_SECURITY_IMPLICATION(!node || node->hasTagName(HTMLNames::tdTag) || node->hasTagName(HTMLNames::thTag));
190    return static_cast<HTMLTableCellElement*>(node);
191}
192
193const HTMLTableCellElement* toHTMLTableCellElement(const Node* node)
194{
195    ASSERT_WITH_SECURITY_IMPLICATION(!node || node->hasTagName(HTMLNames::tdTag) || node->hasTagName(HTMLNames::thTag));
196    return static_cast<const HTMLTableCellElement*>(node);
197}
198
199#endif
200
201} // namespace WebCore
202