Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLTableRowsCollection.cpp
index 8a1f3db..ae67c03 100644 (file)
@@ -30,6 +30,7 @@
 #include "core/html/HTMLTableRowsCollection.h"
 
 #include "HTMLNames.h"
+#include "core/dom/ElementTraversal.h"
 #include "core/html/HTMLTableElement.h"
 #include "core/html/HTMLTableRowElement.h"
 
@@ -52,14 +53,23 @@ static bool isInFoot(Element* row)
     return row->parentNode() && toElement(row->parentNode())->hasLocalName(tfootTag);
 }
 
+static inline HTMLTableRowElement* findTableRowElementInChildren(Element& current)
+{
+    for (Element* child = ElementTraversal::firstWithin(current); child; child = ElementTraversal::nextSibling(*child)) {
+        if (child->hasTagName(trTag))
+            return toHTMLTableRowElement(child);
+    }
+    return 0;
+}
+
 HTMLTableRowElement* HTMLTableRowsCollection::rowAfter(HTMLTableElement* table, HTMLTableRowElement* previous)
 {
-    Node* child = 0;
+    Element* child = 0;
 
     // Start by looking for the next row in this section.
     // Continue only if there is none.
     if (previous && previous->parentNode() != table) {
-        for (child = previous->nextSibling(); child; child = child->nextSibling()) {
+        for (child = ElementTraversal::nextSibling(*previous); child; child = ElementTraversal::nextSibling(*child)) {
             if (child->hasTagName(trTag))
                 return toHTMLTableRowElement(child);
         }
@@ -67,47 +77,41 @@ HTMLTableRowElement* HTMLTableRowsCollection::rowAfter(HTMLTableElement* table,
 
     // If still looking at head sections, find the first row in the next head section.
     if (!previous)
-        child = table->firstChild();
+        child = ElementTraversal::firstWithin(*table);
     else if (isInHead(previous))
-        child = previous->parentNode()->nextSibling();
-    for (; child; child = child->nextSibling()) {
+        child = ElementTraversal::nextSibling(*previous->parentNode());
+    for (; child; child = ElementTraversal::nextSibling(*child)) {
         if (child->hasTagName(theadTag)) {
-            for (Node* grandchild = child->firstChild(); grandchild; grandchild = grandchild->nextSibling()) {
-                if (grandchild->hasTagName(trTag))
-                    return toHTMLTableRowElement(grandchild);
-            }
+            if (HTMLTableRowElement* row = findTableRowElementInChildren(*child))
+                return row;
         }
     }
 
     // If still looking at top level and bodies, find the next row in top level or the first in the next body section.
     if (!previous || isInHead(previous))
-        child = table->firstChild();
+        child = ElementTraversal::firstWithin(*table);
     else if (previous->parentNode() == table)
-        child = previous->nextSibling();
+        child = ElementTraversal::nextSibling(*previous);
     else if (isInBody(previous))
-        child = previous->parentNode()->nextSibling();
-    for (; child; child = child->nextSibling()) {
+        child = ElementTraversal::nextSibling(*previous->parentNode());
+    for (; child; child = ElementTraversal::nextSibling(*child)) {
         if (child->hasTagName(trTag))
             return toHTMLTableRowElement(child);
         if (child->hasTagName(tbodyTag)) {
-            for (Node* grandchild = child->firstChild(); grandchild; grandchild = grandchild->nextSibling()) {
-                if (grandchild->hasTagName(trTag))
-                    return toHTMLTableRowElement(grandchild);
-            }
+            if (HTMLTableRowElement* row = findTableRowElementInChildren(*child))
+                return row;
         }
     }
 
     // Find the first row in the next foot section.
     if (!previous || !isInFoot(previous))
-        child = table->firstChild();
+        child = ElementTraversal::firstWithin(*table);
     else
-        child = previous->parentNode()->nextSibling();
-    for (; child; child = child->nextSibling()) {
+        child = ElementTraversal::nextSibling(*previous->parentNode());
+    for (; child; child = ElementTraversal::nextSibling(*child)) {
         if (child->hasTagName(tfootTag)) {
-            for (Node* grandchild = child->firstChild(); grandchild; grandchild = grandchild->nextSibling()) {
-                if (grandchild->hasTagName(trTag))
-                    return toHTMLTableRowElement(grandchild);
-            }
+            if (HTMLTableRowElement* row = findTableRowElementInChildren(*child))
+                return row;
         }
     }