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 "core/html/HTMLTableElement.h"
28 #include "CSSPropertyNames.h"
29 #include "CSSValueKeywords.h"
30 #include "HTMLNames.h"
31 #include "bindings/v8/ExceptionState.h"
32 #include "bindings/v8/ExceptionStatePlaceholder.h"
33 #include "core/css/CSSImageValue.h"
34 #include "core/css/CSSValuePool.h"
35 #include "core/css/StylePropertySet.h"
36 #include "core/dom/Attribute.h"
37 #include "core/dom/ExceptionCode.h"
38 #include "core/html/HTMLTableCaptionElement.h"
39 #include "core/html/HTMLTableRowElement.h"
40 #include "core/html/HTMLTableRowsCollection.h"
41 #include "core/html/HTMLTableSectionElement.h"
42 #include "core/html/parser/HTMLParserIdioms.h"
43 #include "core/rendering/RenderTable.h"
44 #include "wtf/StdLibExtras.h"
48 using namespace HTMLNames;
50 HTMLTableElement::HTMLTableElement(Document& document)
51 : HTMLElement(tableTag, document)
53 , m_borderColorAttr(false)
55 , m_rulesAttr(UnsetRules)
58 ScriptWrappable::init(this);
61 PassRefPtr<HTMLTableElement> HTMLTableElement::create(Document& document)
63 return adoptRef(new HTMLTableElement(document));
66 HTMLTableCaptionElement* HTMLTableElement::caption() const
68 for (Node* child = firstChild(); child; child = child->nextSibling()) {
69 if (child->hasTagName(captionTag))
70 return toHTMLTableCaptionElement(child);
75 void HTMLTableElement::setCaption(PassRefPtr<HTMLTableCaptionElement> newCaption, ExceptionState& exceptionState)
78 insertBefore(newCaption, firstChild(), exceptionState);
81 HTMLTableSectionElement* HTMLTableElement::tHead() const
83 for (Node* child = firstChild(); child; child = child->nextSibling()) {
84 if (child->hasTagName(theadTag))
85 return toHTMLTableSectionElement(child);
90 void HTMLTableElement::setTHead(PassRefPtr<HTMLTableSectionElement> newHead, ExceptionState& exceptionState)
95 for (child = firstChild(); child; child = child->nextSibling())
96 if (child->isElementNode() && !child->hasTagName(captionTag) && !child->hasTagName(colgroupTag))
99 insertBefore(newHead, child, exceptionState);
102 HTMLTableSectionElement* HTMLTableElement::tFoot() const
104 for (Node* child = firstChild(); child; child = child->nextSibling()) {
105 if (child->hasTagName(tfootTag))
106 return toHTMLTableSectionElement(child);
111 void HTMLTableElement::setTFoot(PassRefPtr<HTMLTableSectionElement> newFoot, ExceptionState& exceptionState)
116 for (child = firstChild(); child; child = child->nextSibling())
117 if (child->isElementNode() && !child->hasTagName(captionTag) && !child->hasTagName(colgroupTag) && !child->hasTagName(theadTag))
120 insertBefore(newFoot, child, exceptionState);
123 PassRefPtr<HTMLElement> HTMLTableElement::createTHead()
125 if (HTMLTableSectionElement* existingHead = tHead())
127 RefPtr<HTMLTableSectionElement> head = HTMLTableSectionElement::create(theadTag, document());
128 setTHead(head, IGNORE_EXCEPTION);
129 return head.release();
132 void HTMLTableElement::deleteTHead()
134 removeChild(tHead(), IGNORE_EXCEPTION);
137 PassRefPtr<HTMLElement> HTMLTableElement::createTFoot()
139 if (HTMLTableSectionElement* existingFoot = tFoot())
141 RefPtr<HTMLTableSectionElement> foot = HTMLTableSectionElement::create(tfootTag, document());
142 setTFoot(foot, IGNORE_EXCEPTION);
143 return foot.release();
146 void HTMLTableElement::deleteTFoot()
148 removeChild(tFoot(), IGNORE_EXCEPTION);
151 PassRefPtr<HTMLElement> HTMLTableElement::createTBody()
153 RefPtr<HTMLTableSectionElement> body = HTMLTableSectionElement::create(tbodyTag, document());
154 Node* referenceElement = lastBody() ? lastBody()->nextSibling() : 0;
156 insertBefore(body, referenceElement);
157 return body.release();
160 PassRefPtr<HTMLElement> HTMLTableElement::createCaption()
162 if (HTMLTableCaptionElement* existingCaption = caption())
163 return existingCaption;
164 RefPtr<HTMLTableCaptionElement> caption = HTMLTableCaptionElement::create(document());
165 setCaption(caption, IGNORE_EXCEPTION);
166 return caption.release();
169 void HTMLTableElement::deleteCaption()
171 removeChild(caption(), IGNORE_EXCEPTION);
174 HTMLTableSectionElement* HTMLTableElement::lastBody() const
176 for (Node* child = lastChild(); child; child = child->previousSibling()) {
177 if (child->hasTagName(tbodyTag))
178 return toHTMLTableSectionElement(child);
183 PassRefPtr<HTMLElement> HTMLTableElement::insertRow(int index, ExceptionState& exceptionState)
186 exceptionState.throwDOMException(IndexSizeError, "The index provided (" + String::number(index) + ") is less than -1.");
190 RefPtr<Node> protectFromMutationEvents(this);
192 RefPtr<HTMLTableRowElement> lastRow = 0;
193 RefPtr<HTMLTableRowElement> row = 0;
195 lastRow = HTMLTableRowsCollection::lastRow(this);
197 for (int i = 0; i <= index; ++i) {
198 row = HTMLTableRowsCollection::rowAfter(this, lastRow.get());
201 exceptionState.throwDOMException(IndexSizeError, "The index provided (" + String::number(index) + ") is greater than the number of rows in the table (" + String::number(i) + ").");
210 RefPtr<ContainerNode> parent;
212 parent = row ? row->parentNode() : lastRow->parentNode();
216 RefPtr<HTMLTableSectionElement> newBody = HTMLTableSectionElement::create(tbodyTag, document());
217 RefPtr<HTMLTableRowElement> newRow = HTMLTableRowElement::create(document());
218 newBody->appendChild(newRow, exceptionState);
219 appendChild(newBody.release(), exceptionState);
220 return newRow.release();
224 RefPtr<HTMLTableRowElement> newRow = HTMLTableRowElement::create(document());
225 parent->insertBefore(newRow, row.get(), exceptionState);
226 return newRow.release();
229 void HTMLTableElement::deleteRow(int index, ExceptionState& exceptionState)
232 exceptionState.throwDOMException(IndexSizeError, "The index provided (" + String::number(index) + ") is less than -1.");
236 HTMLTableRowElement* row = 0;
239 row = HTMLTableRowsCollection::lastRow(this);
241 for (i = 0; i <= index; ++i) {
242 row = HTMLTableRowsCollection::rowAfter(this, row);
248 exceptionState.throwDOMException(IndexSizeError, "The index provided (" + String::number(index) + ") is greater than the number of rows in the table (" + String::number(i) + ").");
251 row->remove(exceptionState);
254 static inline bool isTableCellAncestor(Node* n)
256 return n->hasTagName(theadTag) || n->hasTagName(tbodyTag) ||
257 n->hasTagName(tfootTag) || n->hasTagName(trTag) ||
258 n->hasTagName(thTag);
261 static bool setTableCellsChanged(Node* n)
264 bool cellChanged = false;
266 if (n->hasTagName(tdTag))
268 else if (isTableCellAncestor(n)) {
269 for (Node* child = n->firstChild(); child; child = child->nextSibling())
270 cellChanged |= setTableCellsChanged(child);
274 n->setNeedsStyleRecalc(SubtreeStyleChange);
279 static bool getBordersFromFrameAttributeValue(const AtomicString& value, bool& borderTop, bool& borderRight, bool& borderBottom, bool& borderLeft)
283 borderBottom = false;
286 if (equalIgnoringCase(value, "above"))
288 else if (equalIgnoringCase(value, "below"))
290 else if (equalIgnoringCase(value, "hsides"))
291 borderTop = borderBottom = true;
292 else if (equalIgnoringCase(value, "vsides"))
293 borderLeft = borderRight = true;
294 else if (equalIgnoringCase(value, "lhs"))
296 else if (equalIgnoringCase(value, "rhs"))
298 else if (equalIgnoringCase(value, "box") || equalIgnoringCase(value, "border"))
299 borderTop = borderBottom = borderLeft = borderRight = true;
300 else if (!equalIgnoringCase(value, "void"))
305 void HTMLTableElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
307 if (name == widthAttr)
308 addHTMLLengthToStyle(style, CSSPropertyWidth, value);
309 else if (name == heightAttr)
310 addHTMLLengthToStyle(style, CSSPropertyHeight, value);
311 else if (name == borderAttr)
312 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderWidth, parseBorderWidthAttribute(value), CSSPrimitiveValue::CSS_PX);
313 else if (name == bordercolorAttr) {
314 if (!value.isEmpty())
315 addHTMLColorToStyle(style, CSSPropertyBorderColor, value);
316 } else if (name == bgcolorAttr)
317 addHTMLColorToStyle(style, CSSPropertyBackgroundColor, value);
318 else if (name == backgroundAttr) {
319 String url = stripLeadingAndTrailingHTMLSpaces(value);
321 style->setProperty(CSSProperty(CSSPropertyBackgroundImage, CSSImageValue::create(document().completeURL(url))));
322 } else if (name == valignAttr) {
323 if (!value.isEmpty())
324 addPropertyToPresentationAttributeStyle(style, CSSPropertyVerticalAlign, value);
325 } else if (name == cellspacingAttr) {
326 if (!value.isEmpty())
327 addHTMLLengthToStyle(style, CSSPropertyBorderSpacing, value);
328 } else if (name == vspaceAttr) {
329 addHTMLLengthToStyle(style, CSSPropertyMarginTop, value);
330 addHTMLLengthToStyle(style, CSSPropertyMarginBottom, value);
331 } else if (name == hspaceAttr) {
332 addHTMLLengthToStyle(style, CSSPropertyMarginLeft, value);
333 addHTMLLengthToStyle(style, CSSPropertyMarginRight, value);
334 } else if (name == alignAttr) {
335 if (!value.isEmpty()) {
336 if (equalIgnoringCase(value, "center")) {
337 addPropertyToPresentationAttributeStyle(style, CSSPropertyWebkitMarginStart, CSSValueAuto);
338 addPropertyToPresentationAttributeStyle(style, CSSPropertyWebkitMarginEnd, CSSValueAuto);
340 addPropertyToPresentationAttributeStyle(style, CSSPropertyFloat, value);
342 } else if (name == rulesAttr) {
343 // The presence of a valid rules attribute causes border collapsing to be enabled.
344 if (m_rulesAttr != UnsetRules)
345 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderCollapse, CSSValueCollapse);
346 } else if (name == frameAttr) {
351 if (getBordersFromFrameAttributeValue(value, borderTop, borderRight, borderBottom, borderLeft)) {
352 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderWidth, CSSValueThin);
353 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderTopStyle, borderTop ? CSSValueSolid : CSSValueHidden);
354 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderBottomStyle, borderBottom ? CSSValueSolid : CSSValueHidden);
355 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderLeftStyle, borderLeft ? CSSValueSolid : CSSValueHidden);
356 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderRightStyle, borderRight ? CSSValueSolid : CSSValueHidden);
359 HTMLElement::collectStyleForPresentationAttribute(name, value, style);
362 bool HTMLTableElement::isPresentationAttribute(const QualifiedName& name) const
364 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)
366 return HTMLElement::isPresentationAttribute(name);
369 void HTMLTableElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
371 CellBorders bordersBefore = cellBorders();
372 unsigned short oldPadding = m_padding;
374 if (name == borderAttr) {
375 // FIXME: This attribute is a mess.
376 m_borderAttr = parseBorderWidthAttribute(value);
377 } else if (name == bordercolorAttr) {
378 m_borderColorAttr = !value.isEmpty();
379 } else if (name == frameAttr) {
380 // FIXME: This attribute is a mess.
385 m_frameAttr = getBordersFromFrameAttributeValue(value, borderTop, borderRight, borderBottom, borderLeft);
386 } else if (name == rulesAttr) {
387 m_rulesAttr = UnsetRules;
388 if (equalIgnoringCase(value, "none"))
389 m_rulesAttr = NoneRules;
390 else if (equalIgnoringCase(value, "groups"))
391 m_rulesAttr = GroupsRules;
392 else if (equalIgnoringCase(value, "rows"))
393 m_rulesAttr = RowsRules;
394 else if (equalIgnoringCase(value, "cols"))
395 m_rulesAttr = ColsRules;
396 else if (equalIgnoringCase(value, "all"))
397 m_rulesAttr = AllRules;
398 } else if (name == cellpaddingAttr) {
399 if (!value.isEmpty())
400 m_padding = max(0, value.toInt());
403 } else if (name == colsAttr) {
406 HTMLElement::parseAttribute(name, value);
408 if (bordersBefore != cellBorders() || oldPadding != m_padding) {
409 m_sharedCellStyle = 0;
410 bool cellChanged = false;
411 for (Node* child = firstChild(); child; child = child->nextSibling())
412 cellChanged |= setTableCellsChanged(child);
414 setNeedsStyleRecalc(SubtreeStyleChange);
418 static PassRefPtr<StylePropertySet> createBorderStyle(CSSValueID value)
420 RefPtr<MutableStylePropertySet> style = MutableStylePropertySet::create();
421 style->setProperty(CSSPropertyBorderTopStyle, value);
422 style->setProperty(CSSPropertyBorderBottomStyle, value);
423 style->setProperty(CSSPropertyBorderLeftStyle, value);
424 style->setProperty(CSSPropertyBorderRightStyle, value);
425 return style.release();
428 const StylePropertySet* HTMLTableElement::additionalPresentationAttributeStyle()
433 if (!m_borderAttr && !m_borderColorAttr) {
434 // Setting the border to 'hidden' allows it to win over any border
435 // set on the table's cells during border-conflict resolution.
436 if (m_rulesAttr != UnsetRules) {
437 DEFINE_STATIC_REF(StylePropertySet, solidBorderStyle, (createBorderStyle(CSSValueHidden)));
438 return solidBorderStyle;
443 if (m_borderColorAttr) {
444 DEFINE_STATIC_REF(StylePropertySet, solidBorderStyle, (createBorderStyle(CSSValueSolid)));
445 return solidBorderStyle;
447 DEFINE_STATIC_REF(StylePropertySet, outsetBorderStyle, (createBorderStyle(CSSValueOutset)));
448 return outsetBorderStyle;
451 HTMLTableElement::CellBorders HTMLTableElement::cellBorders() const
453 switch (m_rulesAttr) {
460 return SolidBordersColsOnly;
462 return SolidBordersRowsOnly;
466 if (m_borderColorAttr)
470 ASSERT_NOT_REACHED();
474 PassRefPtr<StylePropertySet> HTMLTableElement::createSharedCellStyle()
476 RefPtr<MutableStylePropertySet> style = MutableStylePropertySet::create();
478 switch (cellBorders()) {
479 case SolidBordersColsOnly:
480 style->setProperty(CSSPropertyBorderLeftWidth, CSSValueThin);
481 style->setProperty(CSSPropertyBorderRightWidth, CSSValueThin);
482 style->setProperty(CSSPropertyBorderLeftStyle, CSSValueSolid);
483 style->setProperty(CSSPropertyBorderRightStyle, CSSValueSolid);
484 style->setProperty(CSSPropertyBorderColor, cssValuePool().createInheritedValue());
486 case SolidBordersRowsOnly:
487 style->setProperty(CSSPropertyBorderTopWidth, CSSValueThin);
488 style->setProperty(CSSPropertyBorderBottomWidth, CSSValueThin);
489 style->setProperty(CSSPropertyBorderTopStyle, CSSValueSolid);
490 style->setProperty(CSSPropertyBorderBottomStyle, CSSValueSolid);
491 style->setProperty(CSSPropertyBorderColor, cssValuePool().createInheritedValue());
494 style->setProperty(CSSPropertyBorderWidth, cssValuePool().createValue(1, CSSPrimitiveValue::CSS_PX));
495 style->setProperty(CSSPropertyBorderStyle, cssValuePool().createIdentifierValue(CSSValueSolid));
496 style->setProperty(CSSPropertyBorderColor, cssValuePool().createInheritedValue());
499 style->setProperty(CSSPropertyBorderWidth, cssValuePool().createValue(1, CSSPrimitiveValue::CSS_PX));
500 style->setProperty(CSSPropertyBorderStyle, cssValuePool().createIdentifierValue(CSSValueInset));
501 style->setProperty(CSSPropertyBorderColor, cssValuePool().createInheritedValue());
504 // If 'rules=none' then allow any borders set at cell level to take effect.
509 style->setProperty(CSSPropertyPadding, cssValuePool().createValue(m_padding, CSSPrimitiveValue::CSS_PX));
511 return style.release();
514 const StylePropertySet* HTMLTableElement::additionalCellStyle()
516 if (!m_sharedCellStyle)
517 m_sharedCellStyle = createSharedCellStyle();
518 return m_sharedCellStyle.get();
521 static PassRefPtr<StylePropertySet> createGroupBorderStyle(int rows)
523 RefPtr<MutableStylePropertySet> style = MutableStylePropertySet::create();
525 style->setProperty(CSSPropertyBorderTopWidth, CSSValueThin);
526 style->setProperty(CSSPropertyBorderBottomWidth, CSSValueThin);
527 style->setProperty(CSSPropertyBorderTopStyle, CSSValueSolid);
528 style->setProperty(CSSPropertyBorderBottomStyle, CSSValueSolid);
530 style->setProperty(CSSPropertyBorderLeftWidth, CSSValueThin);
531 style->setProperty(CSSPropertyBorderRightWidth, CSSValueThin);
532 style->setProperty(CSSPropertyBorderLeftStyle, CSSValueSolid);
533 style->setProperty(CSSPropertyBorderRightStyle, CSSValueSolid);
535 return style.release();
538 const StylePropertySet* HTMLTableElement::additionalGroupStyle(bool rows)
540 if (m_rulesAttr != GroupsRules)
544 DEFINE_STATIC_REF(StylePropertySet, rowBorderStyle, (createGroupBorderStyle(true)));
545 return rowBorderStyle;
547 DEFINE_STATIC_REF(StylePropertySet, columnBorderStyle, (createGroupBorderStyle(false)));
548 return columnBorderStyle;
551 bool HTMLTableElement::isURLAttribute(const Attribute& attribute) const
553 return attribute.name() == backgroundAttr || HTMLElement::isURLAttribute(attribute);
556 PassRefPtr<HTMLCollection> HTMLTableElement::rows()
558 return ensureCachedHTMLCollection(TableRows);
561 PassRefPtr<HTMLCollection> HTMLTableElement::tBodies()
563 return ensureCachedHTMLCollection(TableTBodies);
566 const AtomicString& HTMLTableElement::rules() const
568 return getAttribute(rulesAttr);
571 const AtomicString& HTMLTableElement::summary() const
573 return getAttribute(summaryAttr);