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