diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WebCore/html/HTMLTableRowElement.cpp | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/WebCore/html/HTMLTableRowElement.cpp')
-rw-r--r-- | Source/WebCore/html/HTMLTableRowElement.cpp | 161 |
1 files changed, 72 insertions, 89 deletions
diff --git a/Source/WebCore/html/HTMLTableRowElement.cpp b/Source/WebCore/html/HTMLTableRowElement.cpp index 4c4d9792c..0788f204b 100644 --- a/Source/WebCore/html/HTMLTableRowElement.cpp +++ b/Source/WebCore/html/HTMLTableRowElement.cpp @@ -26,142 +26,125 @@ #include "HTMLTableRowElement.h" #include "ExceptionCode.h" -#include "HTMLCollection.h" +#include "GenericCachedHTMLCollection.h" #include "HTMLNames.h" #include "HTMLTableCellElement.h" #include "HTMLTableElement.h" #include "HTMLTableSectionElement.h" #include "NodeList.h" +#include "NodeRareData.h" #include "Text.h" namespace WebCore { using namespace HTMLNames; -HTMLTableRowElement::HTMLTableRowElement(const QualifiedName& tagName, Document& document) +inline HTMLTableRowElement::HTMLTableRowElement(const QualifiedName& tagName, Document& document) : HTMLTablePartElement(tagName, document) { ASSERT(hasTagName(trTag)); } -PassRefPtr<HTMLTableRowElement> HTMLTableRowElement::create(Document& document) +Ref<HTMLTableRowElement> HTMLTableRowElement::create(Document& document) { - return adoptRef(new HTMLTableRowElement(trTag, document)); + return adoptRef(*new HTMLTableRowElement(trTag, document)); } -PassRefPtr<HTMLTableRowElement> HTMLTableRowElement::create(const QualifiedName& tagName, Document& document) +Ref<HTMLTableRowElement> HTMLTableRowElement::create(const QualifiedName& tagName, Document& document) { - return adoptRef(new HTMLTableRowElement(tagName, document)); + return adoptRef(*new HTMLTableRowElement(tagName, document)); +} + +static inline HTMLTableElement* findTable(const HTMLTableRowElement& row) +{ + auto* parent = row.parentNode(); + if (is<HTMLTableElement>(parent)) + return downcast<HTMLTableElement>(parent); + if (is<HTMLTableSectionElement>(parent)) { + auto* grandparent = parent->parentNode(); + if (is<HTMLTableElement>(grandparent)) + return downcast<HTMLTableElement>(grandparent); + } + return nullptr; } int HTMLTableRowElement::rowIndex() const { - ContainerNode* table = parentNode(); + auto* table = findTable(*this); if (!table) return -1; - table = table->parentNode(); - if (!table || !isHTMLTableElement(table)) - return -1; - - // To match Firefox, the row indices work like this: - // Rows from the first <thead> are numbered before all <tbody> rows. - // Rows from the first <tfoot> are numbered after all <tbody> rows. - // Rows from other <thead> and <tfoot> elements don't get row indices at all. - int rIndex = 0; - - if (HTMLTableSectionElement* head = toHTMLTableElement(table)->tHead()) { - for (Node *row = head->firstChild(); row; row = row->nextSibling()) { - if (row == this) - return rIndex; - if (row->hasTagName(trTag)) - ++rIndex; - } - } - - for (Node *node = table->firstChild(); node; node = node->nextSibling()) { - if (node->hasTagName(tbodyTag)) { - HTMLTableSectionElement* section = toHTMLTableSectionElement(node); - for (Node* row = section->firstChild(); row; row = row->nextSibling()) { - if (row == this) - return rIndex; - if (row->hasTagName(trTag)) - ++rIndex; - } - } + auto rows = table->rows(); + unsigned length = rows->length(); + for (unsigned i = 0; i < length; ++i) { + if (rows->item(i) == this) + return i; } - if (HTMLTableSectionElement* foot = toHTMLTableElement(table)->tFoot()) { - for (Node *row = foot->firstChild(); row; row = row->nextSibling()) { - if (row == this) - return rIndex; - if (row->hasTagName(trTag)) - ++rIndex; - } - } - - // We get here for rows that are in <thead> or <tfoot> sections other than the main header and footer. return -1; } -int HTMLTableRowElement::sectionRowIndex() const +static inline RefPtr<HTMLCollection> findRows(const HTMLTableRowElement& row) { - int rIndex = 0; - const Node *n = this; - do { - n = n->previousSibling(); - if (n && n->hasTagName(trTag)) - rIndex++; - } - while (n); - - return rIndex; + auto* parent = row.parentNode(); + if (is<HTMLTableSectionElement>(parent)) + return downcast<HTMLTableSectionElement>(*parent).rows(); + if (is<HTMLTableElement>(parent)) + return downcast<HTMLTableElement>(*parent).rows(); + return nullptr; } -PassRefPtr<HTMLElement> HTMLTableRowElement::insertCell(int index, ExceptionCode& ec) +int HTMLTableRowElement::sectionRowIndex() const { - RefPtr<HTMLCollection> children = cells(); - int numCells = children ? children->length() : 0; - if (index < -1 || index > numCells) { - ec = INDEX_SIZE_ERR; - return 0; - } + auto rows = findRows(*this); + if (!rows) + return -1; - RefPtr<HTMLTableCellElement> cell = HTMLTableCellElement::create(tdTag, document()); - if (index < 0 || index >= numCells) - appendChild(cell, ec); - else { - Node* n; - if (index < 1) - n = firstChild(); - else - n = children->item(index); - insertBefore(cell, n, ec); + unsigned length = rows->length(); + for (unsigned i = 0; i < length; ++i) { + if (rows->item(i) == this) + return i; } - return cell.release(); + + return -1; } -void HTMLTableRowElement::deleteCell(int index, ExceptionCode& ec) +ExceptionOr<Ref<HTMLTableCellElement>> HTMLTableRowElement::insertCell(int index) { - RefPtr<HTMLCollection> children = cells(); - int numCells = children ? children->length() : 0; - if (index == -1) - index = numCells-1; - if (index >= 0 && index < numCells) { - RefPtr<Node> cell = children->item(index); - HTMLElement::removeChild(cell.get(), ec); - } else - ec = INDEX_SIZE_ERR; + if (index < -1) + return Exception { INDEX_SIZE_ERR }; + auto children = cells(); + int numCells = children->length(); + if (index > numCells) + return Exception { INDEX_SIZE_ERR }; + auto cell = HTMLTableCellElement::create(tdTag, document()); + ExceptionOr<void> result; + if (index < 0 || index >= numCells) + result = appendChild(cell); + else + result = insertBefore(cell, index < 1 ? firstChild() : children->item(index)); + if (result.hasException()) + return result.releaseException(); + return WTFMove(cell); } -PassRefPtr<HTMLCollection> HTMLTableRowElement::cells() +ExceptionOr<void> HTMLTableRowElement::deleteCell(int index) { - return ensureCachedHTMLCollection(TRCells); + auto children = cells(); + int numCells = children->length(); + if (index == -1) { + if (!numCells) + return { }; + index = numCells - 1; + } + if (index < 0 || index >= numCells) + return Exception { INDEX_SIZE_ERR }; + return removeChild(*children->item(index)); } -void HTMLTableRowElement::setCells(HTMLCollection*, ExceptionCode& ec) +Ref<HTMLCollection> HTMLTableRowElement::cells() { - ec = NO_MODIFICATION_ALLOWED_ERR; + return ensureRareData().ensureNodeLists().addCachedCollection<GenericCachedHTMLCollection<CollectionTypeTraits<TRCells>::traversalType>>(*this, TRCells); } } |