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, 2008, 2010, 2011 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 "HTMLTableElement.h"
28 #include "Attribute.h"
29 #include "CSSImageValue.h"
30 #include "CSSPropertyNames.h"
31 #include "CSSStyleSheet.h"
32 #include "CSSValueKeywords.h"
33 #include "CSSValuePool.h"
34 #include "ExceptionCode.h"
35 #include "HTMLNames.h"
36 #include "HTMLParserIdioms.h"
37 #include "HTMLTableCaptionElement.h"
38 #include "HTMLTableRowElement.h"
39 #include "HTMLTableRowsCollection.h"
40 #include "HTMLTableSectionElement.h"
41 #include "RenderTable.h"
46 using namespace HTMLNames;
48 HTMLTableElement::HTMLTableElement(const QualifiedName& tagName, Document* document)
49 : HTMLElement(tagName, document)
51 , m_borderColorAttr(false)
53 , m_rulesAttr(UnsetRules)
56 ASSERT(hasTagName(tableTag));
59 PassRefPtr<HTMLTableElement> HTMLTableElement::create(Document* document)
61 return adoptRef(new HTMLTableElement(tableTag, document));
64 PassRefPtr<HTMLTableElement> HTMLTableElement::create(const QualifiedName& tagName, Document* document)
66 return adoptRef(new HTMLTableElement(tagName, document));
69 HTMLTableCaptionElement* HTMLTableElement::caption() const
71 for (Node* child = firstChild(); child; child = child->nextSibling()) {
72 if (child->hasTagName(captionTag))
73 return static_cast<HTMLTableCaptionElement*>(child);
78 void HTMLTableElement::setCaption(PassRefPtr<HTMLTableCaptionElement> newCaption, ExceptionCode& ec)
81 insertBefore(newCaption, firstChild(), ec);
84 HTMLTableSectionElement* HTMLTableElement::tHead() const
86 for (Node* child = firstChild(); child; child = child->nextSibling()) {
87 if (child->hasTagName(theadTag))
88 return static_cast<HTMLTableSectionElement*>(child);
93 void HTMLTableElement::setTHead(PassRefPtr<HTMLTableSectionElement> newHead, ExceptionCode& ec)
98 for (child = firstChild(); child; child = child->nextSibling())
99 if (child->isElementNode() && !child->hasTagName(captionTag) && !child->hasTagName(colgroupTag))
102 insertBefore(newHead, child, ec);
105 HTMLTableSectionElement* HTMLTableElement::tFoot() const
107 for (Node* child = firstChild(); child; child = child->nextSibling()) {
108 if (child->hasTagName(tfootTag))
109 return static_cast<HTMLTableSectionElement*>(child);
114 void HTMLTableElement::setTFoot(PassRefPtr<HTMLTableSectionElement> newFoot, ExceptionCode& ec)
119 for (child = firstChild(); child; child = child->nextSibling())
120 if (child->isElementNode() && !child->hasTagName(captionTag) && !child->hasTagName(colgroupTag) && !child->hasTagName(theadTag))
123 insertBefore(newFoot, child, ec);
126 PassRefPtr<HTMLElement> HTMLTableElement::createTHead()
128 if (HTMLTableSectionElement* existingHead = tHead())
130 RefPtr<HTMLTableSectionElement> head = HTMLTableSectionElement::create(theadTag, document());
133 return head.release();
136 void HTMLTableElement::deleteTHead()
139 removeChild(tHead(), ec);
142 PassRefPtr<HTMLElement> HTMLTableElement::createTFoot()
144 if (HTMLTableSectionElement* existingFoot = tFoot())
146 RefPtr<HTMLTableSectionElement> foot = HTMLTableSectionElement::create(tfootTag, document());
149 return foot.release();
152 void HTMLTableElement::deleteTFoot()
155 removeChild(tFoot(), ec);
158 PassRefPtr<HTMLElement> HTMLTableElement::createTBody()
160 RefPtr<HTMLTableSectionElement> body = HTMLTableSectionElement::create(tbodyTag, document());
161 Node* referenceElement = lastBody() ? lastBody()->nextSibling() : 0;
162 insertBefore(body, referenceElement, ASSERT_NO_EXCEPTION);
163 return body.release();
166 PassRefPtr<HTMLElement> HTMLTableElement::createCaption()
168 if (HTMLTableCaptionElement* existingCaption = caption())
169 return existingCaption;
170 RefPtr<HTMLTableCaptionElement> caption = HTMLTableCaptionElement::create(captionTag, document());
172 setCaption(caption, ec);
173 return caption.release();
176 void HTMLTableElement::deleteCaption()
179 removeChild(caption(), ec);
182 HTMLTableSectionElement* HTMLTableElement::lastBody() const
184 for (Node* child = lastChild(); child; child = child->previousSibling()) {
185 if (child->hasTagName(tbodyTag))
186 return static_cast<HTMLTableSectionElement*>(child);
191 PassRefPtr<HTMLElement> HTMLTableElement::insertRow(int index, ExceptionCode& ec)
198 RefPtr<Node> protectFromMutationEvents(this);
200 RefPtr<HTMLTableRowElement> lastRow = 0;
201 RefPtr<HTMLTableRowElement> row = 0;
203 lastRow = HTMLTableRowsCollection::lastRow(this);
205 for (int i = 0; i <= index; ++i) {
206 row = HTMLTableRowsCollection::rowAfter(this, lastRow.get());
218 RefPtr<ContainerNode> parent;
220 parent = row ? row->parentNode() : lastRow->parentNode();
224 RefPtr<HTMLTableSectionElement> newBody = HTMLTableSectionElement::create(tbodyTag, document());
225 RefPtr<HTMLTableRowElement> newRow = HTMLTableRowElement::create(document());
226 newBody->appendChild(newRow, ec);
227 appendChild(newBody.release(), ec);
228 return newRow.release();
232 RefPtr<HTMLTableRowElement> newRow = HTMLTableRowElement::create(document());
233 parent->insertBefore(newRow, row.get(), ec);
234 return newRow.release();
237 void HTMLTableElement::deleteRow(int index, ExceptionCode& ec)
239 HTMLTableRowElement* row = 0;
241 row = HTMLTableRowsCollection::lastRow(this);
243 for (int i = 0; i <= index; ++i) {
244 row = HTMLTableRowsCollection::rowAfter(this, row);
256 static inline bool isTableCellAncestor(Node* n)
258 return n->hasTagName(theadTag) || n->hasTagName(tbodyTag) ||
259 n->hasTagName(tfootTag) || n->hasTagName(trTag) ||
260 n->hasTagName(thTag);
263 static bool setTableCellsChanged(Node* n)
266 bool cellChanged = false;
268 if (n->hasTagName(tdTag))
270 else if (isTableCellAncestor(n)) {
271 for (Node* child = n->firstChild(); child; child = child->nextSibling())
272 cellChanged |= setTableCellsChanged(child);
276 n->setNeedsStyleRecalc();
281 static bool getBordersFromFrameAttributeValue(const AtomicString& value, bool& borderTop, bool& borderRight, bool& borderBottom, bool& borderLeft)
285 borderBottom = false;
288 if (equalIgnoringCase(value, "above"))
290 else if (equalIgnoringCase(value, "below"))
292 else if (equalIgnoringCase(value, "hsides"))
293 borderTop = borderBottom = true;
294 else if (equalIgnoringCase(value, "vsides"))
295 borderLeft = borderRight = true;
296 else if (equalIgnoringCase(value, "lhs"))
298 else if (equalIgnoringCase(value, "rhs"))
300 else if (equalIgnoringCase(value, "box") || equalIgnoringCase(value, "border"))
301 borderTop = borderBottom = borderLeft = borderRight = true;
302 else if (!equalIgnoringCase(value, "void"))
307 void HTMLTableElement::collectStyleForAttribute(const Attribute& attribute, StylePropertySet* style)
309 if (attribute.name() == widthAttr)
310 addHTMLLengthToStyle(style, CSSPropertyWidth, attribute.value());
311 else if (attribute.name() == heightAttr)
312 addHTMLLengthToStyle(style, CSSPropertyHeight, attribute.value());
313 else if (attribute.name() == borderAttr) {
314 int borderWidth = attribute.isEmpty() ? 1 : attribute.value().toInt();
315 addPropertyToAttributeStyle(style, CSSPropertyBorderWidth, borderWidth, CSSPrimitiveValue::CSS_PX);
316 } else if (attribute.name() == bordercolorAttr) {
317 if (!attribute.isEmpty())
318 addHTMLColorToStyle(style, CSSPropertyBorderColor, attribute.value());
319 } else if (attribute.name() == bgcolorAttr)
320 addHTMLColorToStyle(style, CSSPropertyBackgroundColor, attribute.value());
321 else if (attribute.name() == backgroundAttr) {
322 String url = stripLeadingAndTrailingHTMLSpaces(attribute.value());
324 style->setProperty(CSSProperty(CSSPropertyBackgroundImage, CSSImageValue::create(document()->completeURL(url).string())));
325 } else if (attribute.name() == valignAttr) {
326 if (!attribute.isEmpty())
327 addPropertyToAttributeStyle(style, CSSPropertyVerticalAlign, attribute.value());
328 } else if (attribute.name() == cellspacingAttr) {
329 if (!attribute.isEmpty())
330 addHTMLLengthToStyle(style, CSSPropertyBorderSpacing, attribute.value());
331 } else if (attribute.name() == vspaceAttr) {
332 addHTMLLengthToStyle(style, CSSPropertyMarginTop, attribute.value());
333 addHTMLLengthToStyle(style, CSSPropertyMarginBottom, attribute.value());
334 } else if (attribute.name() == hspaceAttr) {
335 addHTMLLengthToStyle(style, CSSPropertyMarginLeft, attribute.value());
336 addHTMLLengthToStyle(style, CSSPropertyMarginRight, attribute.value());
337 } else if (attribute.name() == alignAttr) {
338 if (!attribute.value().isEmpty()) {
339 if (equalIgnoringCase(attribute.value(), "center")) {
340 addPropertyToAttributeStyle(style, CSSPropertyWebkitMarginStart, CSSValueAuto);
341 addPropertyToAttributeStyle(style, CSSPropertyWebkitMarginEnd, CSSValueAuto);
343 addPropertyToAttributeStyle(style, CSSPropertyFloat, attribute.value());
345 } else if (attribute.name() == rulesAttr) {
346 // The presence of a valid rules attribute causes border collapsing to be enabled.
347 if (m_rulesAttr != UnsetRules)
348 addPropertyToAttributeStyle(style, CSSPropertyBorderCollapse, CSSValueCollapse);
349 } else if (attribute.name() == frameAttr) {
354 if (getBordersFromFrameAttributeValue(attribute.value(), borderTop, borderRight, borderBottom, borderLeft)) {
355 addPropertyToAttributeStyle(style, CSSPropertyBorderWidth, CSSValueThin);
356 addPropertyToAttributeStyle(style, CSSPropertyBorderTopStyle, borderTop ? CSSValueSolid : CSSValueHidden);
357 addPropertyToAttributeStyle(style, CSSPropertyBorderBottomStyle, borderBottom ? CSSValueSolid : CSSValueHidden);
358 addPropertyToAttributeStyle(style, CSSPropertyBorderLeftStyle, borderLeft ? CSSValueSolid : CSSValueHidden);
359 addPropertyToAttributeStyle(style, CSSPropertyBorderRightStyle, borderRight ? CSSValueSolid : CSSValueHidden);
362 HTMLElement::collectStyleForAttribute(attribute, style);
365 bool HTMLTableElement::isPresentationAttribute(const QualifiedName& name) const
367 if (name == widthAttr || name == heightAttr || name == bgcolorAttr || name == backgroundAttr || name == valignAttr || name == vspaceAttr || name == hspaceAttr || name == alignAttr || name == cellspacingAttr || name == borderAttr || name == bordercolorAttr || name == frameAttr || name == rulesAttr)
369 return HTMLElement::isPresentationAttribute(name);
372 void HTMLTableElement::parseAttribute(const Attribute& attribute)
374 CellBorders bordersBefore = cellBorders();
375 unsigned short oldPadding = m_padding;
377 if (attribute.name() == borderAttr) {
378 // FIXME: This attribute is a mess.
380 if (!attribute.isNull()) {
381 int border = attribute.isEmpty() ? 1 : attribute.value().toInt();
382 m_borderAttr = border;
384 } else if (attribute.name() == bordercolorAttr) {
385 m_borderColorAttr = !attribute.isEmpty();
386 } else if (attribute.name() == frameAttr) {
387 // FIXME: This attribute is a mess.
392 m_frameAttr = getBordersFromFrameAttributeValue(attribute.value(), borderTop, borderRight, borderBottom, borderLeft);
393 } else if (attribute.name() == rulesAttr) {
394 m_rulesAttr = UnsetRules;
395 if (equalIgnoringCase(attribute.value(), "none"))
396 m_rulesAttr = NoneRules;
397 else if (equalIgnoringCase(attribute.value(), "groups"))
398 m_rulesAttr = GroupsRules;
399 else if (equalIgnoringCase(attribute.value(), "rows"))
400 m_rulesAttr = RowsRules;
401 if (equalIgnoringCase(attribute.value(), "cols"))
402 m_rulesAttr = ColsRules;
403 if (equalIgnoringCase(attribute.value(), "all"))
404 m_rulesAttr = AllRules;
405 } else if (attribute.name() == cellpaddingAttr) {
406 if (!attribute.value().isEmpty())
407 m_padding = max(0, attribute.value().toInt());
410 } else if (attribute.name() == colsAttr) {
413 HTMLElement::parseAttribute(attribute);
415 if (bordersBefore != cellBorders() || oldPadding != m_padding) {
416 m_sharedCellStyle = 0;
417 bool cellChanged = false;
418 for (Node* child = firstChild(); child; child = child->nextSibling())
419 cellChanged |= setTableCellsChanged(child);
421 setNeedsStyleRecalc();
425 static StylePropertySet* leakBorderStyle(int value)
427 RefPtr<StylePropertySet> style = StylePropertySet::create();
428 style->setProperty(CSSPropertyBorderTopStyle, value);
429 style->setProperty(CSSPropertyBorderBottomStyle, value);
430 style->setProperty(CSSPropertyBorderLeftStyle, value);
431 style->setProperty(CSSPropertyBorderRightStyle, value);
432 return style.release().leakRef();
435 const StylePropertySet* HTMLTableElement::additionalAttributeStyle()
440 if (!m_borderAttr && !m_borderColorAttr) {
441 // Setting the border to 'hidden' allows it to win over any border
442 // set on the table's cells during border-conflict resolution.
443 if (m_rulesAttr != UnsetRules) {
444 static StylePropertySet* solidBorderStyle = leakBorderStyle(CSSValueHidden);
445 return solidBorderStyle;
450 if (m_borderColorAttr) {
451 static StylePropertySet* solidBorderStyle = leakBorderStyle(CSSValueSolid);
452 return solidBorderStyle;
454 static StylePropertySet* outsetBorderStyle = leakBorderStyle(CSSValueOutset);
455 return outsetBorderStyle;
458 HTMLTableElement::CellBorders HTMLTableElement::cellBorders() const
460 switch (m_rulesAttr) {
467 return SolidBordersColsOnly;
469 return SolidBordersRowsOnly;
473 if (m_borderColorAttr)
477 ASSERT_NOT_REACHED();
481 PassRefPtr<StylePropertySet> HTMLTableElement::createSharedCellStyle()
483 RefPtr<StylePropertySet> style = StylePropertySet::create();
485 switch (cellBorders()) {
486 case SolidBordersColsOnly:
487 style->setProperty(CSSPropertyBorderLeftWidth, CSSValueThin);
488 style->setProperty(CSSPropertyBorderRightWidth, CSSValueThin);
489 style->setProperty(CSSPropertyBorderLeftStyle, CSSValueSolid);
490 style->setProperty(CSSPropertyBorderRightStyle, CSSValueSolid);
491 style->setProperty(CSSPropertyBorderColor, cssValuePool().createInheritedValue());
493 case SolidBordersRowsOnly:
494 style->setProperty(CSSPropertyBorderTopWidth, CSSValueThin);
495 style->setProperty(CSSPropertyBorderBottomWidth, CSSValueThin);
496 style->setProperty(CSSPropertyBorderTopStyle, CSSValueSolid);
497 style->setProperty(CSSPropertyBorderBottomStyle, CSSValueSolid);
498 style->setProperty(CSSPropertyBorderColor, cssValuePool().createInheritedValue());
501 style->setProperty(CSSPropertyBorderWidth, cssValuePool().createValue(1, CSSPrimitiveValue::CSS_PX));
502 style->setProperty(CSSPropertyBorderStyle, cssValuePool().createIdentifierValue(CSSValueSolid));
503 style->setProperty(CSSPropertyBorderColor, cssValuePool().createInheritedValue());
506 style->setProperty(CSSPropertyBorderWidth, cssValuePool().createValue(1, CSSPrimitiveValue::CSS_PX));
507 style->setProperty(CSSPropertyBorderStyle, cssValuePool().createIdentifierValue(CSSValueInset));
508 style->setProperty(CSSPropertyBorderColor, cssValuePool().createInheritedValue());
511 style->setProperty(CSSPropertyBorderWidth, cssValuePool().createValue(0, CSSPrimitiveValue::CSS_PX));
516 style->setProperty(CSSPropertyPadding, cssValuePool().createValue(m_padding, CSSPrimitiveValue::CSS_PX));
518 return style.release();
521 const StylePropertySet* HTMLTableElement::additionalCellStyle()
523 if (!m_sharedCellStyle)
524 m_sharedCellStyle = createSharedCellStyle();
525 return m_sharedCellStyle.get();
528 static StylePropertySet* leakGroupBorderStyle(int rows)
530 RefPtr<StylePropertySet> style = StylePropertySet::create();
532 style->setProperty(CSSPropertyBorderTopWidth, CSSValueThin);
533 style->setProperty(CSSPropertyBorderBottomWidth, CSSValueThin);
534 style->setProperty(CSSPropertyBorderTopStyle, CSSValueSolid);
535 style->setProperty(CSSPropertyBorderBottomStyle, CSSValueSolid);
537 style->setProperty(CSSPropertyBorderLeftWidth, CSSValueThin);
538 style->setProperty(CSSPropertyBorderRightWidth, CSSValueThin);
539 style->setProperty(CSSPropertyBorderLeftStyle, CSSValueSolid);
540 style->setProperty(CSSPropertyBorderRightStyle, CSSValueSolid);
542 return style.release().leakRef();
545 const StylePropertySet* HTMLTableElement::additionalGroupStyle(bool rows)
547 if (m_rulesAttr != GroupsRules)
551 static StylePropertySet* rowBorderStyle = leakGroupBorderStyle(true);
552 return rowBorderStyle;
554 static StylePropertySet* columnBorderStyle = leakGroupBorderStyle(false);
555 return columnBorderStyle;
558 bool HTMLTableElement::isURLAttribute(const Attribute& attribute) const
560 return attribute.name() == backgroundAttr || HTMLElement::isURLAttribute(attribute);
563 PassRefPtr<HTMLCollection> HTMLTableElement::rows()
565 return ensureCachedHTMLCollection(TableRows);
568 PassRefPtr<HTMLCollection> HTMLTableElement::tBodies()
570 return ensureCachedHTMLCollection(TableTBodies);
573 String HTMLTableElement::rules() const
575 return getAttribute(rulesAttr);
578 String HTMLTableElement::summary() const
580 return getAttribute(summaryAttr);
583 void HTMLTableElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
585 HTMLElement::addSubresourceAttributeURLs(urls);
587 addSubresourceURL(urls, document()->completeURL(getAttribute(backgroundAttr)));