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 "core/HTMLNames.h"
     33 #include "core/dom/ElementTraversal.h"
     34 #include "core/html/HTMLTableElement.h"
     35 #include "core/html/HTMLTableRowElement.h"
     36 
     37 namespace WebCore {
     38 
     39 using namespace HTMLNames;
     40 
     41 static bool isInHead(Element* row)
     42 {
     43     return row->parentNode() && toElement(row->parentNode())->hasLocalName(theadTag);
     44 }
     45 
     46 static bool isInBody(Element* row)
     47 {
     48     return row->parentNode() && toElement(row->parentNode())->hasLocalName(tbodyTag);
     49 }
     50 
     51 static bool isInFoot(Element* row)
     52 {
     53     return row->parentNode() && toElement(row->parentNode())->hasLocalName(tfootTag);
     54 }
     55 
     56 HTMLTableRowElement* HTMLTableRowsCollection::rowAfter(HTMLTableElement& table, HTMLTableRowElement* previous)
     57 {
     58     // Start by looking for the next row in this section.
     59     // Continue only if there is none.
     60     if (previous && previous->parentNode() != table) {
     61         if (HTMLTableRowElement* row = Traversal<HTMLTableRowElement>::nextSibling(*previous))
     62             return row;
     63     }
     64 
     65     // If still looking at head sections, find the first row in the next head section.
     66     HTMLElement* child = 0;
     67     if (!previous)
     68         child = Traversal<HTMLElement>::firstChild(table);
     69     else if (isInHead(previous))
     70         child = Traversal<HTMLElement>::nextSibling(*previous->parentNode());
     71     for (; child; child = Traversal<HTMLElement>::nextSibling(*child)) {
     72         if (child->hasLocalName(theadTag)) {
     73             if (HTMLTableRowElement* row = Traversal<HTMLTableRowElement>::firstChild(*child))
     74                 return row;
     75         }
     76     }
     77 
     78     // If still looking at top level and bodies, find the next row in top level or the first in the next body section.
     79     if (!previous || isInHead(previous))
     80         child = Traversal<HTMLElement>::firstChild(table);
     81     else if (previous->parentNode() == table)
     82         child = Traversal<HTMLElement>::nextSibling(*previous);
     83     else if (isInBody(previous))
     84         child = Traversal<HTMLElement>::nextSibling(*previous->parentNode());
     85     for (; child; child = Traversal<HTMLElement>::nextSibling(*child)) {
     86         if (isHTMLTableRowElement(child))
     87             return toHTMLTableRowElement(child);
     88         if (child->hasLocalName(tbodyTag)) {
     89             if (HTMLTableRowElement* row = Traversal<HTMLTableRowElement>::firstChild(*child))
     90                 return row;
     91         }
     92     }
     93 
     94     // Find the first row in the next foot section.
     95     if (!previous || !isInFoot(previous))
     96         child = Traversal<HTMLElement>::firstChild(table);
     97     else
     98         child = Traversal<HTMLElement>::nextSibling(*previous->parentNode());
     99     for (; child; child = Traversal<HTMLElement>::nextSibling(*child)) {
    100         if (child->hasLocalName(tfootTag)) {
    101             if (HTMLTableRowElement* row = Traversal<HTMLTableRowElement>::firstChild(*child))
    102                 return row;
    103         }
    104     }
    105 
    106     return 0;
    107 }
    108 
    109 HTMLTableRowElement* HTMLTableRowsCollection::lastRow(HTMLTableElement& table)
    110 {
    111     for (HTMLElement* child = Traversal<HTMLElement>::lastChild(table); child; child = Traversal<HTMLElement>::previousSibling(*child)) {
    112         if (child->hasLocalName(tfootTag)) {
    113             if (HTMLTableRowElement* lastRow = Traversal<HTMLTableRowElement>::lastChild(*child))
    114                 return lastRow;
    115         }
    116     }
    117 
    118     for (HTMLElement* child = Traversal<HTMLElement>::lastChild(table); child; child = Traversal<HTMLElement>::previousSibling(*child)) {
    119         if (isHTMLTableRowElement(child))
    120             return toHTMLTableRowElement(child);
    121         if (child->hasLocalName(tbodyTag)) {
    122             if (HTMLTableRowElement* lastRow = Traversal<HTMLTableRowElement>::lastChild(*child))
    123                 return lastRow;
    124         }
    125     }
    126 
    127     for (HTMLElement* child = Traversal<HTMLElement>::lastChild(table); child; child = Traversal<HTMLElement>::previousSibling(*child)) {
    128         if (child->hasLocalName(theadTag)) {
    129             if (HTMLTableRowElement* lastRow = Traversal<HTMLTableRowElement>::lastChild(*child))
    130                 return lastRow;
    131         }
    132     }
    133 
    134     return 0;
    135 }
    136 
    137 // Must call get() on the table in case that argument is compiled before dereferencing the
    138 // table to get at the collection cache. Order of argument evaluation is undefined and can
    139 // differ between compilers.
    140 HTMLTableRowsCollection::HTMLTableRowsCollection(ContainerNode& table)
    141     : HTMLCollection(table, TableRows, OverridesItemAfter)
    142 {
    143     ASSERT(isHTMLTableElement(table));
    144 }
    145 
    146 PassRefPtrWillBeRawPtr<HTMLTableRowsCollection> HTMLTableRowsCollection::create(ContainerNode& table, CollectionType type)
    147 {
    148     ASSERT_UNUSED(type, type == TableRows);
    149     return adoptRefWillBeNoop(new HTMLTableRowsCollection(table));
    150 }
    151 
    152 Element* HTMLTableRowsCollection::virtualItemAfter(Element* previous) const
    153 {
    154     return rowAfter(toHTMLTableElement(ownerNode()), toHTMLTableRowElement(previous));
    155 }
    156 
    157 }
    158