Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 2008, 2011, 2012 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 #include "core/html/HTMLTableRowsCollection.h"
     31 
     32 #include "HTMLNames.h"
     33 #include "core/html/HTMLTableElement.h"
     34 #include "core/html/HTMLTableRowElement.h"
     35 
     36 namespace WebCore {
     37 
     38 using namespace HTMLNames;
     39 
     40 static bool isInHead(Element* row)
     41 {
     42     return row->parentNode() && toElement(row->parentNode())->hasLocalName(theadTag);
     43 }
     44 
     45 static bool isInBody(Element* row)
     46 {
     47     return row->parentNode() && toElement(row->parentNode())->hasLocalName(tbodyTag);
     48 }
     49 
     50 static bool isInFoot(Element* row)
     51 {
     52     return row->parentNode() && toElement(row->parentNode())->hasLocalName(tfootTag);
     53 }
     54 
     55 HTMLTableRowElement* HTMLTableRowsCollection::rowAfter(HTMLTableElement* table, HTMLTableRowElement* previous)
     56 {
     57     Node* child = 0;
     58 
     59     // Start by looking for the next row in this section.
     60     // Continue only if there is none.
     61     if (previous && previous->parentNode() != table) {
     62         for (child = previous->nextSibling(); child; child = child->nextSibling()) {
     63             if (isHTMLTableRowElement(child))
     64                 return toHTMLTableRowElement(child);
     65         }
     66     }
     67 
     68     // If still looking at head sections, find the first row in the next head section.
     69     if (!previous)
     70         child = table->firstChild();
     71     else if (isInHead(previous))
     72         child = previous->parentNode()->nextSibling();
     73     for (; child; child = child->nextSibling()) {
     74         if (child->hasTagName(theadTag)) {
     75             for (Node* grandchild = child->firstChild(); grandchild; grandchild = grandchild->nextSibling()) {
     76                 if (isHTMLTableRowElement(grandchild))
     77                     return toHTMLTableRowElement(grandchild);
     78             }
     79         }
     80     }
     81 
     82     // If still looking at top level and bodies, find the next row in top level or the first in the next body section.
     83     if (!previous || isInHead(previous))
     84         child = table->firstChild();
     85     else if (previous->parentNode() == table)
     86         child = previous->nextSibling();
     87     else if (isInBody(previous))
     88         child = previous->parentNode()->nextSibling();
     89     for (; child; child = child->nextSibling()) {
     90         if (isHTMLTableRowElement(child))
     91             return toHTMLTableRowElement(child);
     92         if (child->hasTagName(tbodyTag)) {
     93             for (Node* grandchild = child->firstChild(); grandchild; grandchild = grandchild->nextSibling()) {
     94                 if (isHTMLTableRowElement(grandchild))
     95                     return toHTMLTableRowElement(grandchild);
     96             }
     97         }
     98     }
     99 
    100     // Find the first row in the next foot section.
    101     if (!previous || !isInFoot(previous))
    102         child = table->firstChild();
    103     else
    104         child = previous->parentNode()->nextSibling();
    105     for (; child; child = child->nextSibling()) {
    106         if (child->hasTagName(tfootTag)) {
    107             for (Node* grandchild = child->firstChild(); grandchild; grandchild = grandchild->nextSibling()) {
    108                 if (isHTMLTableRowElement(grandchild))
    109                     return toHTMLTableRowElement(grandchild);
    110             }
    111         }
    112     }
    113 
    114     return 0;
    115 }
    116 
    117 HTMLTableRowElement* HTMLTableRowsCollection::lastRow(HTMLTableElement* table)
    118 {
    119     for (Node* child = table->lastChild(); child; child = child->previousSibling()) {
    120         if (child->hasTagName(tfootTag)) {
    121             for (Node* grandchild = child->lastChild(); grandchild; grandchild = grandchild->previousSibling()) {
    122                 if (isHTMLTableRowElement(grandchild))
    123                     return toHTMLTableRowElement(grandchild);
    124             }
    125         }
    126     }
    127 
    128     for (Node* child = table->lastChild(); child; child = child->previousSibling()) {
    129         if (isHTMLTableRowElement(child))
    130             return toHTMLTableRowElement(child);
    131         if (child->hasTagName(tbodyTag)) {
    132             for (Node* grandchild = child->lastChild(); grandchild; grandchild = grandchild->previousSibling()) {
    133                 if (isHTMLTableRowElement(grandchild))
    134                     return toHTMLTableRowElement(grandchild);
    135             }
    136         }
    137     }
    138 
    139     for (Node* child = table->lastChild(); child; child = child->previousSibling()) {
    140         if (child->hasTagName(theadTag)) {
    141             for (Node* grandchild = child->lastChild(); grandchild; grandchild = grandchild->previousSibling()) {
    142                 if (isHTMLTableRowElement(grandchild))
    143                     return toHTMLTableRowElement(grandchild);
    144             }
    145         }
    146     }
    147 
    148     return 0;
    149 }
    150 
    151 // Must call get() on the table in case that argument is compiled before dereferencing the
    152 // table to get at the collection cache. Order of argument evaluation is undefined and can
    153 // differ between compilers.
    154 HTMLTableRowsCollection::HTMLTableRowsCollection(Node* table)
    155     : HTMLCollection(table, TableRows, OverridesItemAfter)
    156 {
    157     ASSERT(isHTMLTableElement(table));
    158 }
    159 
    160 PassRefPtr<HTMLTableRowsCollection> HTMLTableRowsCollection::create(Node* table, CollectionType)
    161 {
    162     return adoptRef(new HTMLTableRowsCollection(table));
    163 }
    164 
    165 Element* HTMLTableRowsCollection::virtualItemAfter(unsigned& offsetInArray, Element* previous) const
    166 {
    167     ASSERT_UNUSED(offsetInArray, !offsetInArray);
    168     return rowAfter(toHTMLTableElement(ownerNode()), toHTMLTableRowElement(previous));
    169 }
    170 
    171 }
    172