WebCore/html/HTMLTableRowElement.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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, 2007, 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 "HTMLTableRowElement.h"
       
    27 
       
    28 #include "ExceptionCode.h"
       
    29 #include "HTMLCollection.h"
       
    30 #include "HTMLNames.h"
       
    31 #include "HTMLTableCellElement.h"
       
    32 #include "HTMLTableElement.h"
       
    33 #include "HTMLTableSectionElement.h"
       
    34 #include "NodeList.h"
       
    35 #include "Text.h"
       
    36 
       
    37 namespace WebCore {
       
    38 
       
    39 using namespace HTMLNames;
       
    40 
       
    41 HTMLTableRowElement::HTMLTableRowElement(const QualifiedName& tagName, Document* document)
       
    42     : HTMLTablePartElement(tagName, document)
       
    43 {
       
    44     ASSERT(hasTagName(trTag));
       
    45 }
       
    46 
       
    47 PassRefPtr<HTMLTableRowElement> HTMLTableRowElement::create(Document* document)
       
    48 {
       
    49     return adoptRef(new HTMLTableRowElement(trTag, document));
       
    50 }
       
    51 
       
    52 PassRefPtr<HTMLTableRowElement> HTMLTableRowElement::create(const QualifiedName& tagName, Document* document)
       
    53 {
       
    54     return adoptRef(new HTMLTableRowElement(tagName, document));
       
    55 }
       
    56 
       
    57 bool HTMLTableRowElement::checkDTD(const Node* newChild)
       
    58 {
       
    59     if (newChild->isTextNode())
       
    60         return static_cast<const Text*>(newChild)->containsOnlyWhitespace();
       
    61     return newChild->hasTagName(tdTag) || newChild->hasTagName(thTag) ||
       
    62            newChild->hasTagName(formTag) || newChild->hasTagName(scriptTag);
       
    63 }
       
    64 
       
    65 ContainerNode* HTMLTableRowElement::legacyParserAddChild(PassRefPtr<Node> child)
       
    66 {
       
    67     if (child->hasTagName(formTag)) {
       
    68         // First add the child.
       
    69         HTMLTablePartElement::legacyParserAddChild(child);
       
    70 
       
    71         // Now simply return ourselves as the container to insert into.
       
    72         // This has the effect of demoting the form to a leaf and moving it safely out of the way.
       
    73         return this;
       
    74     }
       
    75 
       
    76     return HTMLTablePartElement::legacyParserAddChild(child);
       
    77 }
       
    78 
       
    79 int HTMLTableRowElement::rowIndex() const
       
    80 {
       
    81     Node *table = parentNode();
       
    82     if (!table)
       
    83         return -1;
       
    84     table = table->parentNode();
       
    85     if (!table || !table->hasTagName(tableTag))
       
    86         return -1;
       
    87 
       
    88     // To match Firefox, the row indices work like this:
       
    89     //   Rows from the first <thead> are numbered before all <tbody> rows.
       
    90     //   Rows from the first <tfoot> are numbered after all <tbody> rows.
       
    91     //   Rows from other <thead> and <tfoot> elements don't get row indices at all.
       
    92 
       
    93     int rIndex = 0;
       
    94 
       
    95     if (HTMLTableSectionElement* head = static_cast<HTMLTableElement*>(table)->tHead()) {
       
    96         for (Node *row = head->firstChild(); row; row = row->nextSibling()) {
       
    97             if (row == this)
       
    98                 return rIndex;
       
    99             if (row->hasTagName(trTag))
       
   100                 ++rIndex;
       
   101         }
       
   102     }
       
   103     
       
   104     for (Node *node = table->firstChild(); node; node = node->nextSibling()) {
       
   105         if (node->hasTagName(tbodyTag)) {
       
   106             HTMLTableSectionElement* section = static_cast<HTMLTableSectionElement*>(node);
       
   107             for (Node* row = section->firstChild(); row; row = row->nextSibling()) {
       
   108                 if (row == this)
       
   109                     return rIndex;
       
   110                 if (row->hasTagName(trTag))
       
   111                     ++rIndex;
       
   112             }
       
   113         }
       
   114     }
       
   115 
       
   116     if (HTMLTableSectionElement* foot = static_cast<HTMLTableElement*>(table)->tFoot()) {
       
   117         for (Node *row = foot->firstChild(); row; row = row->nextSibling()) {
       
   118             if (row == this)
       
   119                 return rIndex;
       
   120             if (row->hasTagName(trTag))
       
   121                 ++rIndex;
       
   122         }
       
   123     }
       
   124 
       
   125     // We get here for rows that are in <thead> or <tfoot> sections other than the main header and footer.
       
   126     return -1;
       
   127 }
       
   128 
       
   129 int HTMLTableRowElement::sectionRowIndex() const
       
   130 {
       
   131     int rIndex = 0;
       
   132     const Node *n = this;
       
   133     do {
       
   134         n = n->previousSibling();
       
   135         if (n && n->hasTagName(trTag))
       
   136             rIndex++;
       
   137     }
       
   138     while (n);
       
   139 
       
   140     return rIndex;
       
   141 }
       
   142 
       
   143 PassRefPtr<HTMLElement> HTMLTableRowElement::insertCell(int index, ExceptionCode& ec)
       
   144 {
       
   145     RefPtr<HTMLCollection> children = cells();
       
   146     int numCells = children ? children->length() : 0;
       
   147     if (index < -1 || index > numCells) {
       
   148         ec = INDEX_SIZE_ERR;
       
   149         return 0;
       
   150     }
       
   151 
       
   152     RefPtr<HTMLTableCellElement> cell = HTMLTableCellElement::create(tdTag, document());
       
   153     if (index < 0 || index >= numCells)
       
   154         appendChild(cell, ec);
       
   155     else {
       
   156         Node* n;
       
   157         if (index < 1)
       
   158             n = firstChild();
       
   159         else
       
   160             n = children->item(index);
       
   161         insertBefore(cell, n, ec);
       
   162     }
       
   163     return cell.release();
       
   164 }
       
   165 
       
   166 void HTMLTableRowElement::deleteCell(int index, ExceptionCode& ec)
       
   167 {
       
   168     RefPtr<HTMLCollection> children = cells();
       
   169     int numCells = children ? children->length() : 0;
       
   170     if (index == -1)
       
   171         index = numCells-1;
       
   172     if (index >= 0 && index < numCells) {
       
   173         RefPtr<Node> cell = children->item(index);
       
   174         HTMLElement::removeChild(cell.get(), ec);
       
   175     } else
       
   176         ec = INDEX_SIZE_ERR;
       
   177 }
       
   178 
       
   179 PassRefPtr<HTMLCollection> HTMLTableRowElement::cells()
       
   180 {
       
   181     return HTMLCollection::create(this, TRCells);
       
   182 }
       
   183 
       
   184 void HTMLTableRowElement::setCells(HTMLCollection*, ExceptionCode& ec)
       
   185 {
       
   186     ec = NO_MODIFICATION_ALLOWED_ERR;
       
   187 }
       
   188 
       
   189 }