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
50const StylePropertySet* HTMLTableSectionElement::additionalPresentationAttributeStyle()
51{
52    if (HTMLTableElement* table = findParentTable())
53        return table->additionalGroupStyle(true);
54    return 0;
55}
56
57// these functions are rather slow, since we need to get the row at
58// the index... but they aren't used during usual HTML parsing anyway
59PassRefPtr<HTMLElement> HTMLTableSectionElement::insertRow(int index, ExceptionCode& ec)
60{
61    RefPtr<HTMLTableRowElement> row;
62    RefPtr<HTMLCollection> children = rows();
63    int numRows = children ? (int)children->length() : 0;
64    if (index < -1 || index > numRows)
65        ec = INDEX_SIZE_ERR; // per the DOM
66    else {
67        row = HTMLTableRowElement::create(trTag, document());
68        if (numRows == index || index == -1)
69            appendChild(row, ec);
70        else {
71            Node* n;
72            if (index < 1)
73                n = firstChild();
74            else
75                n = children->item(index);
76            insertBefore(row, n, ec);
77        }
78    }
79    return row.release();
80}
81
82void HTMLTableSectionElement::deleteRow(int index, ExceptionCode& ec)
83{
84    RefPtr<HTMLCollection> children = rows();
85    int numRows = children ? (int)children->length() : 0;
86    if (index == -1)
87        index = numRows - 1;
88    if (index >= 0 && index < numRows) {
89        RefPtr<Node> row = children->item(index);
90        HTMLElement::removeChild(row.get(), ec);
91    } else
92        ec = INDEX_SIZE_ERR;
93}
94
95int HTMLTableSectionElement::numRows() const
96{
97    int rows = 0;
98    const Node *n = firstChild();
99    while (n) {
100        if (n->hasTagName(trTag))
101            rows++;
102        n = n->nextSibling();
103    }
104
105    return rows;
106}
107
108String HTMLTableSectionElement::align() const
109{
110    return getAttribute(alignAttr);
111}
112
113void HTMLTableSectionElement::setAlign(const String &value)
114{
115    setAttribute(alignAttr, value);
116}
117
118String HTMLTableSectionElement::ch() const
119{
120    return getAttribute(charAttr);
121}
122
123void HTMLTableSectionElement::setCh(const String &value)
124{
125    setAttribute(charAttr, value);
126}
127
128String HTMLTableSectionElement::chOff() const
129{
130    return getAttribute(charoffAttr);
131}
132
133void HTMLTableSectionElement::setChOff(const String &value)
134{
135    setAttribute(charoffAttr, value);
136}
137
138String HTMLTableSectionElement::vAlign() const
139{
140    return getAttribute(valignAttr);
141}
142
143void HTMLTableSectionElement::setVAlign(const String &value)
144{
145    setAttribute(valignAttr, value);
146}
147
148PassRefPtr<HTMLCollection> HTMLTableSectionElement::rows()
149{
150    return ensureCachedHTMLCollection(TSectionRows);
151}
152
153}
154