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