Bug 1335356 - Part 1: Insert caption and thead in the right order. r=smaug
authorJessica Jong <jjong@mozilla.com>
Mon, 20 Feb 2017 22:43:00 -0500
changeset 373306 b4f882927cb00ca40742a67bd33eadcc9dcdc418
parent 373305 bbd4c0e017c80cf09a578a723d23b94f38cbda57
child 373307 9b78f38eef9ea01642807a95b0f28660d9a99d58
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1335356
milestone54.0a1
Bug 1335356 - Part 1: Insert caption and thead in the right order. r=smaug
dom/html/HTMLTableElement.cpp
dom/html/HTMLTableElement.h
testing/web-platform/meta/html/semantics/tabular-data/the-caption-element/caption_001.html.ini
testing/web-platform/meta/html/semantics/tabular-data/the-table-element/caption-methods.html.ini
testing/web-platform/meta/html/semantics/tabular-data/the-table-element/tHead.html.ini
--- a/dom/html/HTMLTableElement.cpp
+++ b/dom/html/HTMLTableElement.cpp
@@ -406,18 +406,29 @@ HTMLTableElement::CreateTHead()
     nsContentUtils::NameChanged(mNodeInfo, nsGkAtoms::thead,
                                 getter_AddRefs(nodeInfo));
 
     head = NS_NewHTMLTableSectionElement(nodeInfo.forget());
     if (!head) {
       return nullptr;
     }
 
-    ErrorResult rv;
-    nsCOMPtr<nsINode> refNode = nsINode::GetFirstChild();
+    nsCOMPtr<nsIContent> refNode = nullptr;
+    for (refNode = nsINode::GetFirstChild();
+         refNode;
+         refNode = refNode->GetNextSibling()) {
+
+      if (refNode->IsHTMLElement() &&
+          !refNode->IsHTMLElement(nsGkAtoms::caption) &&
+          !refNode->IsHTMLElement(nsGkAtoms::colgroup)) {
+        break;
+      }
+    }
+
+    IgnoredErrorResult rv;
     nsINode::InsertBefore(*head, refNode, rv);
   }
   return head.forget();
 }
 
 void
 HTMLTableElement::DeleteTHead()
 {
@@ -470,17 +481,19 @@ HTMLTableElement::CreateCaption()
     nsContentUtils::NameChanged(mNodeInfo, nsGkAtoms::caption,
                                 getter_AddRefs(nodeInfo));
 
     caption = NS_NewHTMLTableCaptionElement(nodeInfo.forget());
     if (!caption) {
       return nullptr;
     }
 
-    AppendChildTo(caption, true);
+    IgnoredErrorResult rv;
+    nsCOMPtr<nsINode> firsChild = nsINode::GetFirstChild();
+    nsINode::InsertBefore(*caption, firsChild, rv);
   }
   return caption.forget();
 }
 
 void
 HTMLTableElement::DeleteCaption()
 {
   HTMLTableCaptionElement* caption = GetCaption();
@@ -509,17 +522,17 @@ HTMLTableElement::CreateTBody()
        child;
        child = child->GetPreviousSibling()) {
     if (child->IsHTMLElement(nsGkAtoms::tbody)) {
       referenceNode = child->GetNextSibling();
       break;
     }
   }
 
-  ErrorResult rv;
+  IgnoredErrorResult rv;
   nsINode::InsertBefore(*newBody, referenceNode, rv);
 
   return newBody.forget();
 }
 
 already_AddRefed<nsGenericHTMLElement>
 HTMLTableElement::InsertRow(int32_t aIndex, ErrorResult& aError)
 {
--- a/dom/html/HTMLTableElement.h
+++ b/dom/html/HTMLTableElement.h
@@ -31,17 +31,18 @@ public:
   HTMLTableCaptionElement* GetCaption() const
   {
     return static_cast<HTMLTableCaptionElement*>(GetChild(nsGkAtoms::caption));
   }
   void SetCaption(HTMLTableCaptionElement* aCaption, ErrorResult& aError)
   {
     DeleteCaption();
     if (aCaption) {
-      nsINode::AppendChild(*aCaption, aError);
+      nsCOMPtr<nsINode> firstChild = nsINode::GetFirstChild();
+      nsINode::InsertBefore(*aCaption, firstChild, aError);
     }
   }
 
   void DeleteTFoot();
 
   already_AddRefed<nsGenericHTMLElement> CreateCaption();
 
   void DeleteCaption();
@@ -54,17 +55,28 @@ public:
   {
     if (aTHead && !aTHead->IsHTMLElement(nsGkAtoms::thead)) {
       aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
       return;
     }
 
     DeleteTHead();
     if (aTHead) {
-      nsCOMPtr<nsINode> refNode = nsINode::GetFirstChild();
+
+      nsCOMPtr<nsIContent> refNode = nullptr;
+      for (refNode = nsINode::GetFirstChild();
+           refNode;
+           refNode = refNode->GetNextSibling()) {
+        if (refNode->IsHTMLElement() &&
+            !refNode->IsHTMLElement(nsGkAtoms::caption) &&
+            !refNode->IsHTMLElement(nsGkAtoms::colgroup)) {
+          break;
+        }
+      }
+
       nsINode::InsertBefore(*aTHead, refNode, aError);
     }
   }
   already_AddRefed<nsGenericHTMLElement> CreateTHead();
 
   void DeleteTHead();
 
   HTMLTableSectionElement* GetTFoot() const
deleted file mode 100644
--- a/testing/web-platform/meta/html/semantics/tabular-data/the-caption-element/caption_001.html.ini
+++ /dev/null
@@ -1,5 +0,0 @@
-[caption_001.html]
-  type: testharness
-  [setting caption on a table]
-    expected: FAIL
-
--- a/testing/web-platform/meta/html/semantics/tabular-data/the-table-element/caption-methods.html.ini
+++ b/testing/web-platform/meta/html/semantics/tabular-data/the-table-element/caption-methods.html.ini
@@ -1,14 +1,4 @@
 [caption-methods.html]
-  type: testharness
-  [createCaption method creates a new caption and inserts it as the first node of the table element]
-    expected: FAIL
-
-  [createCaption method creates new caption if existing caption is not in html namespace]
-    expected: FAIL
-
   [createCaption will not copy table's prefix]
     expected: FAIL
 
-  [Assigning a caption to table.caption]
-    expected: FAIL
-
deleted file mode 100644
--- a/testing/web-platform/meta/html/semantics/tabular-data/the-table-element/tHead.html.ini
+++ /dev/null
@@ -1,5 +0,0 @@
-[tHead.html]
-  type: testharness
-  [tHead tests]
-    expected: FAIL
-