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 Apple Computer, Inc.
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 "CSSPropertyNames.h"
29 #include "CSSValueKeywords.h"
30 #include "HTMLNames.h"
31 #include "HTMLTableElement.h"
32 #include "MappedAttribute.h"
33 #include "RenderTableCell.h"
34 #ifdef ANDROID_LAYOUT
35 #include "Document.h"
36 #include "Frame.h"
37 #include "Settings.h"
38 #endif
39
40 using std::max;
41 using std::min;
42
43 namespace WebCore {
44
45 // Clamp rowspan at 8k to match Firefox.
46 static const int maxRowspan = 8190;
47
48 using namespace HTMLNames;
49
HTMLTableCellElement(const QualifiedName & tagName,Document * doc)50 HTMLTableCellElement::HTMLTableCellElement(const QualifiedName& tagName, Document *doc)
51 : HTMLTablePartElement(tagName, doc)
52 , _row(-1)
53 , _col(-1)
54 , rSpan(1)
55 , cSpan(1)
56 , rowHeight(0)
57 , m_solid(false)
58 {
59 }
60
~HTMLTableCellElement()61 HTMLTableCellElement::~HTMLTableCellElement()
62 {
63 }
64
cellIndex() const65 int HTMLTableCellElement::cellIndex() const
66 {
67 int index = 0;
68 for (const Node * node = previousSibling(); node; node = node->previousSibling()) {
69 if (node->hasTagName(tdTag) || node->hasTagName(thTag))
70 index++;
71 }
72
73 return index;
74 }
75
mapToEntry(const QualifiedName & attrName,MappedAttributeEntry & result) const76 bool HTMLTableCellElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
77 {
78 if (attrName == nowrapAttr) {
79 result = eUniversal;
80 return false;
81 }
82
83 if (attrName == widthAttr ||
84 attrName == heightAttr) {
85 result = eCell; // Because of the quirky behavior of ignoring 0 values, cells are special.
86 return false;
87 }
88
89 return HTMLTablePartElement::mapToEntry(attrName, result);
90 }
91
parseMappedAttribute(MappedAttribute * attr)92 void HTMLTableCellElement::parseMappedAttribute(MappedAttribute *attr)
93 {
94 if (attr->name() == rowspanAttr) {
95 rSpan = !attr->isNull() ? attr->value().toInt() : 1;
96 rSpan = max(1, min(rSpan, maxRowspan));
97 if (renderer() && renderer()->isTableCell())
98 toRenderTableCell(renderer())->updateFromElement();
99 } else if (attr->name() == colspanAttr) {
100 cSpan = !attr->isNull() ? attr->value().toInt() : 1;
101 cSpan = max(1, cSpan);
102 if (renderer() && renderer()->isTableCell())
103 toRenderTableCell(renderer())->updateFromElement();
104 } else if (attr->name() == nowrapAttr) {
105 #ifdef ANDROID_LAYOUT
106 if (!(document()->frame()) || document()->frame()->settings()->layoutAlgorithm() != Settings::kLayoutSSR)
107 #endif
108 if (!attr->isNull())
109 addCSSProperty(attr, CSSPropertyWhiteSpace, CSSValueWebkitNowrap);
110 } else if (attr->name() == widthAttr) {
111 if (!attr->value().isEmpty()) {
112 int widthInt = attr->value().toInt();
113 if (widthInt > 0) // width="0" is ignored for compatibility with WinIE.
114 addCSSLength(attr, CSSPropertyWidth, attr->value());
115 }
116 } else if (attr->name() == heightAttr) {
117 if (!attr->value().isEmpty()) {
118 int heightInt = attr->value().toInt();
119 if (heightInt > 0) // height="0" is ignored for compatibility with WinIE.
120 addCSSLength(attr, CSSPropertyHeight, attr->value());
121 }
122 } else
123 HTMLTablePartElement::parseMappedAttribute(attr);
124 }
125
126 // used by table cells to share style decls created by the enclosing table.
additionalAttributeStyleDecls(Vector<CSSMutableStyleDeclaration * > & results)127 void HTMLTableCellElement::additionalAttributeStyleDecls(Vector<CSSMutableStyleDeclaration*>& results)
128 {
129 Node* p = parentNode();
130 while (p && !p->hasTagName(tableTag))
131 p = p->parentNode();
132 if (!p)
133 return;
134 static_cast<HTMLTableElement*>(p)->addSharedCellDecls(results);
135 }
136
isURLAttribute(Attribute * attr) const137 bool HTMLTableCellElement::isURLAttribute(Attribute *attr) const
138 {
139 return attr->name() == backgroundAttr;
140 }
141
abbr() const142 String HTMLTableCellElement::abbr() const
143 {
144 return getAttribute(abbrAttr);
145 }
146
setAbbr(const String & value)147 void HTMLTableCellElement::setAbbr(const String &value)
148 {
149 setAttribute(abbrAttr, value);
150 }
151
align() const152 String HTMLTableCellElement::align() const
153 {
154 return getAttribute(alignAttr);
155 }
156
setAlign(const String & value)157 void HTMLTableCellElement::setAlign(const String &value)
158 {
159 setAttribute(alignAttr, value);
160 }
161
axis() const162 String HTMLTableCellElement::axis() const
163 {
164 return getAttribute(axisAttr);
165 }
166
setAxis(const String & value)167 void HTMLTableCellElement::setAxis(const String &value)
168 {
169 setAttribute(axisAttr, value);
170 }
171
bgColor() const172 String HTMLTableCellElement::bgColor() const
173 {
174 return getAttribute(bgcolorAttr);
175 }
176
setBgColor(const String & value)177 void HTMLTableCellElement::setBgColor(const String &value)
178 {
179 setAttribute(bgcolorAttr, value);
180 }
181
ch() const182 String HTMLTableCellElement::ch() const
183 {
184 return getAttribute(charAttr);
185 }
186
setCh(const String & value)187 void HTMLTableCellElement::setCh(const String &value)
188 {
189 setAttribute(charAttr, value);
190 }
191
chOff() const192 String HTMLTableCellElement::chOff() const
193 {
194 return getAttribute(charoffAttr);
195 }
196
setChOff(const String & value)197 void HTMLTableCellElement::setChOff(const String &value)
198 {
199 setAttribute(charoffAttr, value);
200 }
201
setColSpan(int n)202 void HTMLTableCellElement::setColSpan(int n)
203 {
204 setAttribute(colspanAttr, String::number(n));
205 }
206
headers() const207 String HTMLTableCellElement::headers() const
208 {
209 return getAttribute(headersAttr);
210 }
211
setHeaders(const String & value)212 void HTMLTableCellElement::setHeaders(const String &value)
213 {
214 setAttribute(headersAttr, value);
215 }
216
height() const217 String HTMLTableCellElement::height() const
218 {
219 return getAttribute(heightAttr);
220 }
221
setHeight(const String & value)222 void HTMLTableCellElement::setHeight(const String &value)
223 {
224 setAttribute(heightAttr, value);
225 }
226
noWrap() const227 bool HTMLTableCellElement::noWrap() const
228 {
229 return !getAttribute(nowrapAttr).isNull();
230 }
231
setNoWrap(bool b)232 void HTMLTableCellElement::setNoWrap(bool b)
233 {
234 setAttribute(nowrapAttr, b ? "" : 0);
235 }
236
setRowSpan(int n)237 void HTMLTableCellElement::setRowSpan(int n)
238 {
239 setAttribute(rowspanAttr, String::number(n));
240 }
241
scope() const242 String HTMLTableCellElement::scope() const
243 {
244 return getAttribute(scopeAttr);
245 }
246
setScope(const String & value)247 void HTMLTableCellElement::setScope(const String &value)
248 {
249 setAttribute(scopeAttr, value);
250 }
251
vAlign() const252 String HTMLTableCellElement::vAlign() const
253 {
254 return getAttribute(valignAttr);
255 }
256
setVAlign(const String & value)257 void HTMLTableCellElement::setVAlign(const String &value)
258 {
259 setAttribute(valignAttr, value);
260 }
261
width() const262 String HTMLTableCellElement::width() const
263 {
264 return getAttribute(widthAttr);
265 }
266
setWidth(const String & value)267 void HTMLTableCellElement::setWidth(const String &value)
268 {
269 setAttribute(widthAttr, value);
270 }
271
addSubresourceAttributeURLs(ListHashSet<KURL> & urls) const272 void HTMLTableCellElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
273 {
274 HTMLTablePartElement::addSubresourceAttributeURLs(urls);
275
276 addSubresourceURL(urls, document()->completeURL(getAttribute(backgroundAttr)));
277 }
278
279 }
280