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 "core/html/HTMLTableSectionElement.h"
27
28#include "bindings/v8/ExceptionState.h"
29#include "core/HTMLNames.h"
30#include "core/dom/ElementTraversal.h"
31#include "core/dom/ExceptionCode.h"
32#include "core/html/HTMLCollection.h"
33#include "core/html/HTMLTableElement.h"
34#include "core/html/HTMLTableRowElement.h"
35
36namespace WebCore {
37
38using namespace HTMLNames;
39
40inline HTMLTableSectionElement::HTMLTableSectionElement(const QualifiedName& tagName, Document& document)
41    : HTMLTablePartElement(tagName, document)
42{
43    ScriptWrappable::init(this);
44}
45
46DEFINE_ELEMENT_FACTORY_WITH_TAGNAME(HTMLTableSectionElement)
47
48const StylePropertySet* HTMLTableSectionElement::additionalPresentationAttributeStyle()
49{
50    if (HTMLTableElement* table = findParentTable())
51        return table->additionalGroupStyle(true);
52    return 0;
53}
54
55PassRefPtrWillBeRawPtr<HTMLElement> HTMLTableSectionElement::insertRow(ExceptionState& exceptionState)
56{
57    // The default 'index' argument value is -1.
58    return insertRow(-1, exceptionState);
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
63PassRefPtrWillBeRawPtr<HTMLElement> HTMLTableSectionElement::insertRow(int index, ExceptionState& exceptionState)
64{
65    RefPtrWillBeRawPtr<HTMLCollection> children = rows();
66    int numRows = children ? static_cast<int>(children->length()) : 0;
67    if (index < -1 || index > numRows) {
68        exceptionState.throwDOMException(IndexSizeError, "The provided index (" + String::number(index) + " is outside the range [-1, " + String::number(numRows) + "].");
69        return nullptr;
70    }
71
72    RefPtrWillBeRawPtr<HTMLTableRowElement> row = HTMLTableRowElement::create(document());
73    if (numRows == index || index == -1)
74        appendChild(row, exceptionState);
75    else
76        insertBefore(row, children->item(index), exceptionState);
77    return row.release();
78}
79
80void HTMLTableSectionElement::deleteRow(int index, ExceptionState& exceptionState)
81{
82    RefPtrWillBeRawPtr<HTMLCollection> children = rows();
83    int numRows = children ? (int)children->length() : 0;
84    if (index == -1)
85        index = numRows - 1;
86    if (index >= 0 && index < numRows) {
87        RefPtrWillBeRawPtr<Element> row = children->item(index);
88        HTMLElement::removeChild(row.get(), exceptionState);
89    } else {
90        exceptionState.throwDOMException(IndexSizeError, "The provided index (" + String::number(index) + " is outside the range [-1, " + String::number(numRows) + "].");
91    }
92}
93
94int HTMLTableSectionElement::numRows() const
95{
96    int rowCount = 0;
97    for (const HTMLTableRowElement* row = Traversal<HTMLTableRowElement>::firstChild(*this); row; row = Traversal<HTMLTableRowElement>::nextSibling(*row))
98        ++rowCount;
99    return rowCount;
100}
101
102PassRefPtrWillBeRawPtr<HTMLCollection> HTMLTableSectionElement::rows()
103{
104    return ensureCachedHTMLCollection(TSectionRows);
105}
106
107}
108