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#include "config.h"
25#include "HTMLTableSectionElement.h"
26
27#include "ExceptionCode.h"
28#include "HTMLCollection.h"
29#include "HTMLNames.h"
30#include "HTMLTableRowElement.h"
31#include "HTMLTableElement.h"
32#include "NodeList.h"
33#include "Text.h"
34
35namespace WebCore {
36
37using namespace HTMLNames;
38
39HTMLTableSectionElement::HTMLTableSectionElement(const QualifiedName& tagName, Document* document)
40    : HTMLTablePartElement(tagName, document)
41{
42}
43
44bool HTMLTableSectionElement::checkDTD(const Node* newChild)
45{
46    if (newChild->isTextNode())
47        return static_cast<const Text*>(newChild)->containsOnlyWhitespace();
48    return newChild->hasTagName(trTag) || newChild->hasTagName(formTag) ||
49           newChild->hasTagName(scriptTag);
50}
51
52ContainerNode* HTMLTableSectionElement::addChild(PassRefPtr<Node> child)
53{
54    if (child->hasTagName(formTag)) {
55        // First add the child.
56        HTMLTablePartElement::addChild(child);
57
58        // Now simply return ourselves as the container to insert into.
59        // This has the effect of demoting the form to a leaf and moving it safely out of the way.
60        return this;
61    }
62
63    return HTMLTablePartElement::addChild(child);
64}
65
66// used by table row groups to share style decls created by the enclosing table.
67void HTMLTableSectionElement::additionalAttributeStyleDecls(Vector<CSSMutableStyleDeclaration*>& results)
68{
69    Node* p = parentNode();
70    while (p && !p->hasTagName(tableTag))
71        p = p->parentNode();
72    if (!p)
73        return;
74    static_cast<HTMLTableElement*>(p)->addSharedGroupDecls(true, results);
75}
76
77// these functions are rather slow, since we need to get the row at
78// the index... but they aren't used during usual HTML parsing anyway
79PassRefPtr<HTMLElement> HTMLTableSectionElement::insertRow(int index, ExceptionCode& ec)
80{
81    RefPtr<HTMLTableRowElement> r;
82    RefPtr<HTMLCollection> children = rows();
83    int numRows = children ? (int)children->length() : 0;
84    if (index < -1 || index > numRows)
85        ec = INDEX_SIZE_ERR; // per the DOM
86    else {
87        r = new HTMLTableRowElement(trTag, document());
88        if (numRows == index || index == -1)
89            appendChild(r, ec);
90        else {
91            Node* n;
92            if (index < 1)
93                n = firstChild();
94            else
95                n = children->item(index);
96            insertBefore(r, n, ec);
97        }
98    }
99    return r.release();
100}
101
102void HTMLTableSectionElement::deleteRow(int index, ExceptionCode& ec)
103{
104    RefPtr<HTMLCollection> children = rows();
105    int numRows = children ? (int)children->length() : 0;
106    if (index == -1)
107        index = numRows - 1;
108    if (index >= 0 && index < numRows) {
109        RefPtr<Node> row = children->item(index);
110        HTMLElement::removeChild(row.get(), ec);
111    } else
112        ec = INDEX_SIZE_ERR;
113}
114
115int HTMLTableSectionElement::numRows() const
116{
117    int rows = 0;
118    const Node *n = firstChild();
119    while (n) {
120        if (n->hasTagName(trTag))
121            rows++;
122        n = n->nextSibling();
123    }
124
125    return rows;
126}
127
128String HTMLTableSectionElement::align() const
129{
130    return getAttribute(alignAttr);
131}
132
133void HTMLTableSectionElement::setAlign(const String &value)
134{
135    setAttribute(alignAttr, value);
136}
137
138String HTMLTableSectionElement::ch() const
139{
140    return getAttribute(charAttr);
141}
142
143void HTMLTableSectionElement::setCh(const String &value)
144{
145    setAttribute(charAttr, value);
146}
147
148String HTMLTableSectionElement::chOff() const
149{
150    return getAttribute(charoffAttr);
151}
152
153void HTMLTableSectionElement::setChOff(const String &value)
154{
155    setAttribute(charoffAttr, value);
156}
157
158String HTMLTableSectionElement::vAlign() const
159{
160    return getAttribute(valignAttr);
161}
162
163void HTMLTableSectionElement::setVAlign(const String &value)
164{
165    setAttribute(valignAttr, value);
166}
167
168PassRefPtr<HTMLCollection> HTMLTableSectionElement::rows()
169{
170    return HTMLCollection::create(this, TSectionRows);
171}
172
173}
174