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 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 "CSSPropertyNames.h"
30 #include "CSSStyleSheet.h"
31 #include "CSSValueKeywords.h"
32 #include "ExceptionCode.h"
33 #include "HTMLNames.h"
34 #include "HTMLParserIdioms.h"
35 #include "HTMLTableCaptionElement.h"
36 #include "HTMLTableRowElement.h"
37 #include "HTMLTableRowsCollection.h"
38 #include "HTMLTableSectionElement.h"
39 #include "RenderTable.h"
44 using namespace HTMLNames;
46 HTMLTableElement::HTMLTableElement(const QualifiedName& tagName, Document* document)
47 : HTMLElement(tagName, document)
49 , m_borderColorAttr(false)
51 , m_rulesAttr(UnsetRules)
54 ASSERT(hasTagName(tableTag));
57 PassRefPtr<HTMLTableElement> HTMLTableElement::create(Document* document)
59 return adoptRef(new HTMLTableElement(tableTag, document));
62 PassRefPtr<HTMLTableElement> HTMLTableElement::create(const QualifiedName& tagName, Document* document)
64 return adoptRef(new HTMLTableElement(tagName, document));
67 HTMLTableCaptionElement* HTMLTableElement::caption() const
69 for (Node* child = firstChild(); child; child = child->nextSibling()) {
70 if (child->hasTagName(captionTag))
71 return static_cast<HTMLTableCaptionElement*>(child);
76 void HTMLTableElement::setCaption(PassRefPtr<HTMLTableCaptionElement> newCaption, ExceptionCode& ec)
79 insertBefore(newCaption, firstChild(), ec);
82 HTMLTableSectionElement* HTMLTableElement::tHead() const
84 for (Node* child = firstChild(); child; child = child->nextSibling()) {
85 if (child->hasTagName(theadTag))
86 return static_cast<HTMLTableSectionElement*>(child);
91 void HTMLTableElement::setTHead(PassRefPtr<HTMLTableSectionElement> newHead, ExceptionCode& ec)
96 for (child = firstChild(); child; child = child->nextSibling())
97 if (child->isElementNode() && !child->hasTagName(captionTag) && !child->hasTagName(colgroupTag))
100 insertBefore(newHead, child, ec);
103 HTMLTableSectionElement* HTMLTableElement::tFoot() const
105 for (Node* child = firstChild(); child; child = child->nextSibling()) {
106 if (child->hasTagName(tfootTag))
107 return static_cast<HTMLTableSectionElement*>(child);
112 void HTMLTableElement::setTFoot(PassRefPtr<HTMLTableSectionElement> newFoot, ExceptionCode& ec)
117 for (child = firstChild(); child; child = child->nextSibling())
118 if (child->isElementNode() && !child->hasTagName(captionTag) && !child->hasTagName(colgroupTag) && !child->hasTagName(theadTag))
121 insertBefore(newFoot, child, ec);
124 PassRefPtr<HTMLElement> HTMLTableElement::createTHead()
126 if (HTMLTableSectionElement* existingHead = tHead())
128 RefPtr<HTMLTableSectionElement> head = HTMLTableSectionElement::create(theadTag, document());
131 return head.release();
134 void HTMLTableElement::deleteTHead()
137 removeChild(tHead(), ec);
140 PassRefPtr<HTMLElement> HTMLTableElement::createTFoot()
142 if (HTMLTableSectionElement* existingFoot = tFoot())
144 RefPtr<HTMLTableSectionElement> foot = HTMLTableSectionElement::create(tfootTag, document());
147 return foot.release();
150 void HTMLTableElement::deleteTFoot()
153 removeChild(tFoot(), ec);
156 PassRefPtr<HTMLElement> HTMLTableElement::createCaption()
158 if (HTMLTableCaptionElement* existingCaption = caption())
159 return existingCaption;
160 RefPtr<HTMLTableCaptionElement> caption = HTMLTableCaptionElement::create(captionTag, document());
162 setCaption(caption, ec);
163 return caption.release();
166 void HTMLTableElement::deleteCaption()
169 removeChild(caption(), ec);
172 HTMLTableSectionElement* HTMLTableElement::lastBody() const
174 for (Node* child = lastChild(); child; child = child->previousSibling()) {
175 if (child->hasTagName(tbodyTag))
176 return static_cast<HTMLTableSectionElement*>(child);
181 PassRefPtr<HTMLElement> HTMLTableElement::insertRow(int index, ExceptionCode& ec)
188 HTMLTableRowElement* lastRow = 0;
189 HTMLTableRowElement* row = 0;
191 lastRow = HTMLTableRowsCollection::lastRow(this);
193 for (int i = 0; i <= index; ++i) {
194 row = HTMLTableRowsCollection::rowAfter(this, lastRow);
206 ContainerNode* parent;
208 parent = row ? row->parentNode() : lastRow->parentNode();
212 RefPtr<HTMLTableSectionElement> newBody = HTMLTableSectionElement::create(tbodyTag, document());
213 RefPtr<HTMLTableRowElement> newRow = HTMLTableRowElement::create(document());
214 newBody->appendChild(newRow, ec);
215 appendChild(newBody.release(), ec);
216 return newRow.release();
220 RefPtr<HTMLTableRowElement> newRow = HTMLTableRowElement::create(document());
221 parent->insertBefore(newRow, row, ec);
222 return newRow.release();
225 void HTMLTableElement::deleteRow(int index, ExceptionCode& ec)
227 HTMLTableRowElement* row = 0;
229 row = HTMLTableRowsCollection::lastRow(this);
231 for (int i = 0; i <= index; ++i) {
232 row = HTMLTableRowsCollection::rowAfter(this, row);
244 bool HTMLTableElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
246 if (attrName == backgroundAttr) {
247 result = (MappedAttributeEntry)(eLastEntry + document()->docID());
251 if (attrName == widthAttr ||
252 attrName == heightAttr ||
253 attrName == bgcolorAttr ||
254 attrName == cellspacingAttr ||
255 attrName == vspaceAttr ||
256 attrName == hspaceAttr ||
257 attrName == valignAttr) {
262 if (attrName == bordercolorAttr || attrName == frameAttr || attrName == rulesAttr) {
267 if (attrName == borderAttr) {
272 if (attrName == alignAttr) {
277 return HTMLElement::mapToEntry(attrName, result);
280 static inline bool isTableCellAncestor(Node* n)
282 return n->hasTagName(theadTag) || n->hasTagName(tbodyTag) ||
283 n->hasTagName(tfootTag) || n->hasTagName(trTag) ||
284 n->hasTagName(thTag);
287 static bool setTableCellsChanged(Node* n)
290 bool cellChanged = false;
292 if (n->hasTagName(tdTag))
294 else if (isTableCellAncestor(n)) {
295 for (Node* child = n->firstChild(); child; child = child->nextSibling())
296 cellChanged |= setTableCellsChanged(child);
300 n->setNeedsStyleRecalc();
305 void HTMLTableElement::parseMappedAttribute(Attribute* attr)
307 CellBorders bordersBefore = cellBorders();
308 unsigned short oldPadding = m_padding;
310 if (attr->name() == widthAttr)
311 addCSSLength(attr, CSSPropertyWidth, attr->value());
312 else if (attr->name() == heightAttr)
313 addCSSLength(attr, CSSPropertyHeight, attr->value());
314 else if (attr->name() == borderAttr) {
317 RefPtr<CSSValue> val = attr->decl()->getPropertyCSSValue(CSSPropertyBorderLeftWidth);
318 if (val && val->isPrimitiveValue()) {
319 CSSPrimitiveValue* primVal = static_cast<CSSPrimitiveValue*>(val.get());
320 m_borderAttr = primVal->getDoubleValue(CSSPrimitiveValue::CSS_NUMBER);
322 } else if (!attr->isNull()) {
327 border = attr->value().toInt();
328 m_borderAttr = border;
329 addCSSLength(attr, CSSPropertyBorderWidth, String::number(border));
331 } else if (attr->name() == bgcolorAttr)
332 addCSSColor(attr, CSSPropertyBackgroundColor, attr->value());
333 else if (attr->name() == bordercolorAttr) {
334 m_borderColorAttr = attr->decl();
335 if (!attr->decl() && !attr->isEmpty()) {
336 addCSSColor(attr, CSSPropertyBorderColor, attr->value());
337 m_borderColorAttr = true;
339 } else if (attr->name() == backgroundAttr) {
340 String url = stripLeadingAndTrailingHTMLSpaces(attr->value());
342 addCSSImageProperty(attr, CSSPropertyBackgroundImage, document()->completeURL(url).string());
343 } else if (attr->name() == frameAttr) {
344 // Cache the value of "frame" so that the table can examine it later.
347 // Whether or not to hide the top/right/bottom/left borders.
349 const int cRight = 1;
350 const int cBottom = 2;
352 bool borders[4] = { false, false, false, false };
354 // void, above, below, hsides, vsides, lhs, rhs, box, border
355 if (equalIgnoringCase(attr->value(), "void"))
357 else if (equalIgnoringCase(attr->value(), "above")) {
359 borders[cTop] = true;
360 } else if (equalIgnoringCase(attr->value(), "below")) {
362 borders[cBottom] = true;
363 } else if (equalIgnoringCase(attr->value(), "hsides")) {
365 borders[cTop] = borders[cBottom] = true;
366 } else if (equalIgnoringCase(attr->value(), "vsides")) {
368 borders[cLeft] = borders[cRight] = true;
369 } else if (equalIgnoringCase(attr->value(), "lhs")) {
371 borders[cLeft] = true;
372 } else if (equalIgnoringCase(attr->value(), "rhs")) {
374 borders[cRight] = true;
375 } else if (equalIgnoringCase(attr->value(), "box") ||
376 equalIgnoringCase(attr->value(), "border")) {
378 borders[cTop] = borders[cBottom] = borders[cLeft] = borders[cRight] = true;
381 // Now map in the border styles of solid and hidden respectively.
383 addCSSProperty(attr, CSSPropertyBorderTopWidth, CSSValueThin);
384 addCSSProperty(attr, CSSPropertyBorderBottomWidth, CSSValueThin);
385 addCSSProperty(attr, CSSPropertyBorderLeftWidth, CSSValueThin);
386 addCSSProperty(attr, CSSPropertyBorderRightWidth, CSSValueThin);
387 addCSSProperty(attr, CSSPropertyBorderTopStyle, borders[cTop] ? CSSValueSolid : CSSValueHidden);
388 addCSSProperty(attr, CSSPropertyBorderBottomStyle, borders[cBottom] ? CSSValueSolid : CSSValueHidden);
389 addCSSProperty(attr, CSSPropertyBorderLeftStyle, borders[cLeft] ? CSSValueSolid : CSSValueHidden);
390 addCSSProperty(attr, CSSPropertyBorderRightStyle, borders[cRight] ? CSSValueSolid : CSSValueHidden);
392 } else if (attr->name() == rulesAttr) {
393 m_rulesAttr = UnsetRules;
394 if (equalIgnoringCase(attr->value(), "none"))
395 m_rulesAttr = NoneRules;
396 else if (equalIgnoringCase(attr->value(), "groups"))
397 m_rulesAttr = GroupsRules;
398 else if (equalIgnoringCase(attr->value(), "rows"))
399 m_rulesAttr = RowsRules;
400 if (equalIgnoringCase(attr->value(), "cols"))
401 m_rulesAttr = ColsRules;
402 if (equalIgnoringCase(attr->value(), "all"))
403 m_rulesAttr = AllRules;
405 // The presence of a valid rules attribute causes border collapsing to be enabled.
406 if (m_rulesAttr != UnsetRules)
407 addCSSProperty(attr, CSSPropertyBorderCollapse, CSSValueCollapse);
408 } else if (attr->name() == cellspacingAttr) {
409 if (!attr->value().isEmpty())
410 addCSSLength(attr, CSSPropertyBorderSpacing, attr->value());
411 } else if (attr->name() == cellpaddingAttr) {
412 if (!attr->value().isEmpty())
413 m_padding = max(0, attr->value().toInt());
416 } else if (attr->name() == colsAttr) {
418 } else if (attr->name() == vspaceAttr) {
419 addCSSLength(attr, CSSPropertyMarginTop, attr->value());
420 addCSSLength(attr, CSSPropertyMarginBottom, attr->value());
421 } else if (attr->name() == hspaceAttr) {
422 addCSSLength(attr, CSSPropertyMarginLeft, attr->value());
423 addCSSLength(attr, CSSPropertyMarginRight, attr->value());
424 } else if (attr->name() == alignAttr) {
425 if (!attr->value().isEmpty()) {
426 if (equalIgnoringCase(attr->value(), "center")) {
427 addCSSProperty(attr, CSSPropertyWebkitMarginStart, CSSValueAuto);
428 addCSSProperty(attr, CSSPropertyWebkitMarginEnd, CSSValueAuto);
430 addCSSProperty(attr, CSSPropertyFloat, attr->value());
432 } else if (attr->name() == valignAttr) {
433 if (!attr->value().isEmpty())
434 addCSSProperty(attr, CSSPropertyVerticalAlign, attr->value());
436 HTMLElement::parseMappedAttribute(attr);
438 if (bordersBefore != cellBorders() || oldPadding != m_padding) {
439 if (oldPadding != m_padding)
441 bool cellChanged = false;
442 for (Node* child = firstChild(); child; child = child->nextSibling())
443 cellChanged |= setTableCellsChanged(child);
445 setNeedsStyleRecalc();
449 void HTMLTableElement::additionalAttributeStyleDecls(Vector<CSSMutableStyleDeclaration*>& results)
451 if ((!m_borderAttr && !m_borderColorAttr) || m_frameAttr)
454 AtomicString borderValue = m_borderColorAttr ? "solid" : "outset";
455 CSSMappedAttributeDeclaration* decl = getMappedAttributeDecl(ePersistent, tableborderAttr, borderValue);
457 decl = CSSMappedAttributeDeclaration::create().releaseRef(); // This single ref pins us in the table until the document dies.
458 decl->setParent(document()->elementSheet());
460 decl->setStrictParsing(false); // Mapped attributes are just always quirky.
462 int v = m_borderColorAttr ? CSSValueSolid : CSSValueOutset;
463 decl->setProperty(CSSPropertyBorderTopStyle, v, false);
464 decl->setProperty(CSSPropertyBorderBottomStyle, v, false);
465 decl->setProperty(CSSPropertyBorderLeftStyle, v, false);
466 decl->setProperty(CSSPropertyBorderRightStyle, v, false);
468 setMappedAttributeDecl(ePersistent, tableborderAttr, borderValue, decl);
471 decl->setMappedState(ePersistent, tableborderAttr, borderValue);
475 results.append(decl);
478 HTMLTableElement::CellBorders HTMLTableElement::cellBorders() const
480 switch (m_rulesAttr) {
487 return SolidBordersColsOnly;
489 return SolidBordersRowsOnly;
493 if (m_borderColorAttr)
497 ASSERT_NOT_REACHED();
501 void HTMLTableElement::addSharedCellDecls(Vector<CSSMutableStyleDeclaration*>& results)
503 addSharedCellBordersDecl(results);
504 addSharedCellPaddingDecl(results);
507 void HTMLTableElement::addSharedCellBordersDecl(Vector<CSSMutableStyleDeclaration*>& results)
509 CellBorders borders = cellBorders();
511 static const AtomicString* cellBorderNames[] = { new AtomicString("none"), new AtomicString("solid"), new AtomicString("inset"), new AtomicString("solid-cols"), new AtomicString("solid-rows") };
512 const AtomicString& cellborderValue = *cellBorderNames[borders];
513 CSSMappedAttributeDeclaration* decl = getMappedAttributeDecl(ePersistent, cellborderAttr, cellborderValue);
515 decl = CSSMappedAttributeDeclaration::create().releaseRef(); // This single ref pins us in the table until the document dies.
516 decl->setParent(document()->elementSheet());
518 decl->setStrictParsing(false); // Mapped attributes are just always quirky.
521 case SolidBordersColsOnly:
522 decl->setProperty(CSSPropertyBorderLeftWidth, CSSValueThin, false);
523 decl->setProperty(CSSPropertyBorderRightWidth, CSSValueThin, false);
524 decl->setProperty(CSSPropertyBorderLeftStyle, CSSValueSolid, false);
525 decl->setProperty(CSSPropertyBorderRightStyle, CSSValueSolid, false);
526 decl->setProperty(CSSPropertyBorderColor, "inherit", false);
528 case SolidBordersRowsOnly:
529 decl->setProperty(CSSPropertyBorderTopWidth, CSSValueThin, false);
530 decl->setProperty(CSSPropertyBorderBottomWidth, CSSValueThin, false);
531 decl->setProperty(CSSPropertyBorderTopStyle, CSSValueSolid, false);
532 decl->setProperty(CSSPropertyBorderBottomStyle, CSSValueSolid, false);
533 decl->setProperty(CSSPropertyBorderColor, "inherit", false);
536 decl->setProperty(CSSPropertyBorderWidth, "1px", false);
537 decl->setProperty(CSSPropertyBorderTopStyle, CSSValueSolid, false);
538 decl->setProperty(CSSPropertyBorderBottomStyle, CSSValueSolid, false);
539 decl->setProperty(CSSPropertyBorderLeftStyle, CSSValueSolid, false);
540 decl->setProperty(CSSPropertyBorderRightStyle, CSSValueSolid, false);
541 decl->setProperty(CSSPropertyBorderColor, "inherit", false);
544 decl->setProperty(CSSPropertyBorderWidth, "1px", false);
545 decl->setProperty(CSSPropertyBorderTopStyle, CSSValueInset, false);
546 decl->setProperty(CSSPropertyBorderBottomStyle, CSSValueInset, false);
547 decl->setProperty(CSSPropertyBorderLeftStyle, CSSValueInset, false);
548 decl->setProperty(CSSPropertyBorderRightStyle, CSSValueInset, false);
549 decl->setProperty(CSSPropertyBorderColor, "inherit", false);
552 decl->setProperty(CSSPropertyBorderWidth, "0", false);
556 setMappedAttributeDecl(ePersistent, cellborderAttr, *cellBorderNames[borders], decl);
559 decl->setMappedState(ePersistent, cellborderAttr, cellborderValue);
562 results.append(decl);
565 void HTMLTableElement::addSharedCellPaddingDecl(Vector<CSSMutableStyleDeclaration*>& results)
570 if (!m_paddingDecl) {
571 String paddingValue = String::number(m_padding);
572 m_paddingDecl = getMappedAttributeDecl(eUniversal, cellpaddingAttr, paddingValue);
573 if (!m_paddingDecl) {
574 m_paddingDecl = CSSMappedAttributeDeclaration::create();
575 m_paddingDecl->setParent(document()->elementSheet());
576 m_paddingDecl->setNode(this);
577 m_paddingDecl->setStrictParsing(false); // Mapped attributes are just always quirky.
579 m_paddingDecl->setProperty(CSSPropertyPaddingTop, paddingValue, false);
580 m_paddingDecl->setProperty(CSSPropertyPaddingRight, paddingValue, false);
581 m_paddingDecl->setProperty(CSSPropertyPaddingBottom, paddingValue, false);
582 m_paddingDecl->setProperty(CSSPropertyPaddingLeft, paddingValue, false);
584 setMappedAttributeDecl(eUniversal, cellpaddingAttr, paddingValue, m_paddingDecl.get());
585 m_paddingDecl->setParent(0);
586 m_paddingDecl->setNode(0);
587 m_paddingDecl->setMappedState(eUniversal, cellpaddingAttr, paddingValue);
590 results.append(m_paddingDecl.get());
593 void HTMLTableElement::addSharedGroupDecls(bool rows, Vector<CSSMutableStyleDeclaration*>& results)
595 if (m_rulesAttr != GroupsRules)
598 AtomicString rulesValue = rows ? "rowgroups" : "colgroups";
599 CSSMappedAttributeDeclaration* decl = getMappedAttributeDecl(ePersistent, rulesAttr, rulesValue);
601 decl = CSSMappedAttributeDeclaration::create().releaseRef(); // This single ref pins us in the table until the document dies.
602 decl->setParent(document()->elementSheet());
604 decl->setStrictParsing(false); // Mapped attributes are just always quirky.
607 decl->setProperty(CSSPropertyBorderTopWidth, CSSValueThin, false);
608 decl->setProperty(CSSPropertyBorderBottomWidth, CSSValueThin, false);
609 decl->setProperty(CSSPropertyBorderTopStyle, CSSValueSolid, false);
610 decl->setProperty(CSSPropertyBorderBottomStyle, CSSValueSolid, false);
612 decl->setProperty(CSSPropertyBorderLeftWidth, CSSValueThin, false);
613 decl->setProperty(CSSPropertyBorderRightWidth, CSSValueThin, false);
614 decl->setProperty(CSSPropertyBorderLeftStyle, CSSValueSolid, false);
615 decl->setProperty(CSSPropertyBorderRightStyle, CSSValueSolid, false);
618 setMappedAttributeDecl(ePersistent, rulesAttr, rulesValue, decl);
621 decl->setMappedState(ePersistent, rulesAttr, rulesValue);
624 results.append(decl);
627 CollectionCache* HTMLTableElement::collectionCache() const
629 if (!m_collectionCache)
630 m_collectionCache = adoptPtr(new CollectionCache());
632 return m_collectionCache.get();
635 void HTMLTableElement::attach()
638 HTMLElement::attach();
641 bool HTMLTableElement::isURLAttribute(Attribute *attr) const
643 return attr->name() == backgroundAttr;
646 PassRefPtr<HTMLCollection> HTMLTableElement::rows()
648 return HTMLTableRowsCollection::create(this);
651 PassRefPtr<HTMLCollection> HTMLTableElement::tBodies()
653 return HTMLCollection::create(this, TableTBodies);
656 String HTMLTableElement::rules() const
658 return getAttribute(rulesAttr);
661 String HTMLTableElement::summary() const
663 return getAttribute(summaryAttr);
666 void HTMLTableElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
668 HTMLElement::addSubresourceAttributeURLs(urls);
670 addSubresourceURL(urls, document()->completeURL(getAttribute(backgroundAttr)));