Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / html / HTMLTableSectionElement.cpp
blob6dfad7841c29cb4ac1efe8592555404e87e89f47
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.
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.
25 #include "config.h"
26 #include "core/html/HTMLTableSectionElement.h"
28 #include "bindings/core/v8/ExceptionState.h"
29 #include "core/HTMLNames.h"
30 #include "core/dom/ElementTraversal.h"
31 #include "core/dom/ExceptionCode.h"
32 #include "core/dom/NodeListsNodeData.h"
33 #include "core/html/HTMLCollection.h"
34 #include "core/html/HTMLTableElement.h"
35 #include "core/html/HTMLTableRowElement.h"
37 namespace blink {
39 using namespace HTMLNames;
41 inline HTMLTableSectionElement::HTMLTableSectionElement(const QualifiedName& tagName, Document& document)
42 : HTMLTablePartElement(tagName, document)
46 DEFINE_ELEMENT_FACTORY_WITH_TAGNAME(HTMLTableSectionElement)
48 const StylePropertySet* HTMLTableSectionElement::additionalPresentationAttributeStyle()
50 if (HTMLTableElement* table = findParentTable())
51 return table->additionalGroupStyle(true);
52 return nullptr;
55 // these functions are rather slow, since we need to get the row at
56 // the index... but they aren't used during usual HTML parsing anyway
57 PassRefPtrWillBeRawPtr<HTMLElement> HTMLTableSectionElement::insertRow(int index, ExceptionState& exceptionState)
59 RefPtrWillBeRawPtr<HTMLCollection> children = rows();
60 int numRows = children ? static_cast<int>(children->length()) : 0;
61 if (index < -1 || index > numRows) {
62 exceptionState.throwDOMException(IndexSizeError, "The provided index (" + String::number(index) + " is outside the range [-1, " + String::number(numRows) + "].");
63 return nullptr;
66 RefPtrWillBeRawPtr<HTMLTableRowElement> row = HTMLTableRowElement::create(document());
67 if (numRows == index || index == -1)
68 appendChild(row, exceptionState);
69 else
70 insertBefore(row, children->item(index), exceptionState);
71 return row.release();
74 void HTMLTableSectionElement::deleteRow(int index, ExceptionState& exceptionState)
76 RefPtrWillBeRawPtr<HTMLCollection> children = rows();
77 int numRows = children ? (int)children->length() : 0;
78 if (index == -1)
79 index = numRows - 1;
80 if (index >= 0 && index < numRows) {
81 RefPtrWillBeRawPtr<Element> row = children->item(index);
82 HTMLElement::removeChild(row.get(), exceptionState);
83 } else {
84 exceptionState.throwDOMException(IndexSizeError, "The provided index (" + String::number(index) + " is outside the range [-1, " + String::number(numRows) + "].");
88 int HTMLTableSectionElement::numRows() const
90 int rowCount = 0;
91 for (const HTMLTableRowElement* row = Traversal<HTMLTableRowElement>::firstChild(*this); row; row = Traversal<HTMLTableRowElement>::nextSibling(*row))
92 ++rowCount;
93 return rowCount;
96 PassRefPtrWillBeRawPtr<HTMLCollection> HTMLTableSectionElement::rows()
98 return ensureCachedCollection<HTMLCollection>(TSectionRows);