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, 2007, 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 "HTMLTableRowElement.h"
28 #include "ExceptionCode.h"
29 #include "HTMLCollection.h"
30 #include "HTMLNames.h"
31 #include "HTMLTableCellElement.h"
32 #include "HTMLTableElement.h"
33 #include "HTMLTableSectionElement.h"
39 using namespace HTMLNames;
41 HTMLTableRowElement::HTMLTableRowElement(const QualifiedName& tagName, Document& document)
42 : HTMLTablePartElement(tagName, document)
44 ASSERT(hasTagName(trTag));
47 PassRefPtr<HTMLTableRowElement> HTMLTableRowElement::create(Document& document)
49 return adoptRef(new HTMLTableRowElement(trTag, document));
52 PassRefPtr<HTMLTableRowElement> HTMLTableRowElement::create(const QualifiedName& tagName, Document& document)
54 return adoptRef(new HTMLTableRowElement(tagName, document));
57 int HTMLTableRowElement::rowIndex() const
59 ContainerNode* table = parentNode();
62 table = table->parentNode();
63 if (!table || !isHTMLTableElement(table))
66 // To match Firefox, the row indices work like this:
67 // Rows from the first <thead> are numbered before all <tbody> rows.
68 // Rows from the first <tfoot> are numbered after all <tbody> rows.
69 // Rows from other <thead> and <tfoot> elements don't get row indices at all.
73 if (HTMLTableSectionElement* head = toHTMLTableElement(table)->tHead()) {
74 for (Node *row = head->firstChild(); row; row = row->nextSibling()) {
77 if (row->hasTagName(trTag))
82 for (Node *node = table->firstChild(); node; node = node->nextSibling()) {
83 if (node->hasTagName(tbodyTag)) {
84 HTMLTableSectionElement* section = toHTMLTableSectionElement(node);
85 for (Node* row = section->firstChild(); row; row = row->nextSibling()) {
88 if (row->hasTagName(trTag))
94 if (HTMLTableSectionElement* foot = toHTMLTableElement(table)->tFoot()) {
95 for (Node *row = foot->firstChild(); row; row = row->nextSibling()) {
98 if (row->hasTagName(trTag))
103 // We get here for rows that are in <thead> or <tfoot> sections other than the main header and footer.
107 int HTMLTableRowElement::sectionRowIndex() const
110 const Node *n = this;
112 n = n->previousSibling();
113 if (n && n->hasTagName(trTag))
121 PassRefPtr<HTMLElement> HTMLTableRowElement::insertCell(int index, ExceptionCode& ec)
123 RefPtr<HTMLCollection> children = cells();
124 int numCells = children ? children->length() : 0;
125 if (index < -1 || index > numCells) {
130 RefPtr<HTMLTableCellElement> cell = HTMLTableCellElement::create(tdTag, document());
131 if (index < 0 || index >= numCells)
132 appendChild(cell, ec);
138 n = children->item(index);
139 insertBefore(cell, n, ec);
141 return cell.release();
144 void HTMLTableRowElement::deleteCell(int index, ExceptionCode& ec)
146 RefPtr<HTMLCollection> children = cells();
147 int numCells = children ? children->length() : 0;
150 if (index >= 0 && index < numCells) {
151 RefPtr<Node> cell = children->item(index);
152 HTMLElement::removeChild(cell.get(), ec);
157 PassRefPtr<HTMLCollection> HTMLTableRowElement::cells()
159 return ensureCachedHTMLCollection(TRCells);
162 void HTMLTableRowElement::setCells(HTMLCollection*, ExceptionCode& ec)
164 ec = NO_MODIFICATION_ALLOWED_ERR;