Merge from Chromium at DEPS revision r217147

This commit was generated by merge_to_master.py.

Change-Id: Icd67b02e5f1ff621b8d2455a6e7e4eb98aba6900
diff --git a/Source/core/dom/Element.cpp b/Source/core/dom/Element.cpp
index 0aedfec..b7147bc 100644
--- a/Source/core/dom/Element.cpp
+++ b/Source/core/dom/Element.cpp
@@ -1238,28 +1238,21 @@
     if (isUpgradedCustomElement() && inDocument())
         CustomElement::didEnterDocument(this, document());
 
-    if (insertionPoint->treeScope() != treeScope())
+    TreeScope* scope = insertionPoint->treeScope();
+    if (scope != treeScope())
         return InsertionDone;
 
-    HTMLDocument* newDocument = inDocument() && !isInShadowTree() && document()->isHTMLDocument() ? toHTMLDocument(document()) : 0;
-
     const AtomicString& idValue = getIdAttribute();
-    if (!idValue.isNull()) {
-        updateIdForTreeScope(treeScope(), nullAtom, idValue);
-        if (newDocument)
-            updateIdForDocument(newDocument, nullAtom, idValue);
-    }
+    if (!idValue.isNull())
+        updateId(scope, nullAtom, idValue);
 
     const AtomicString& nameValue = getNameAttribute();
-    if (!nameValue.isNull()) {
-        updateNameForTreeScope(treeScope(), nullAtom, nameValue);
-        if (newDocument)
-            updateNameForDocument(newDocument, nullAtom, nameValue);
-    }
+    if (!nameValue.isNull())
+        updateName(nullAtom, nameValue);
 
     if (hasTagName(labelTag)) {
-        if (treeScope()->shouldCacheLabelsByForAttribute())
-            updateLabel(treeScope(), nullAtom, fastGetAttribute(forAttr));
+        if (scope->shouldCacheLabelsByForAttribute())
+            updateLabel(scope, nullAtom, fastGetAttribute(forAttr));
     }
 
     if (parentElement() && parentElement()->isInCanvasSubtree())
@@ -1271,7 +1264,6 @@
 void Element::removedFrom(ContainerNode* insertionPoint)
 {
     bool wasInDocument = insertionPoint->inDocument();
-    bool wasInShadowTree = isInShadowTree(); // Of course, we might still be in a shadow tree...
 
     if (Element* before = pseudoElement(BEFORE))
         before->removedFrom(insertionPoint);
@@ -1292,31 +1284,22 @@
     setSavedLayerScrollOffset(IntSize());
 
     if (insertionPoint->isInTreeScope() && treeScope() == document()) {
-        TreeScope* oldScope = insertionPoint->treeScope();
-        HTMLDocument* oldDocument = wasInDocument && !wasInShadowTree && oldScope->documentScope()->isHTMLDocument() ? toHTMLDocument(oldScope->documentScope()) : 0;
-
         const AtomicString& idValue = getIdAttribute();
-        if (!idValue.isNull()) {
-            updateIdForTreeScope(oldScope, idValue, nullAtom);
-            if (oldDocument)
-                updateIdForDocument(oldDocument, idValue, nullAtom);
-        }
+        if (!idValue.isNull())
+            updateId(insertionPoint->treeScope(), idValue, nullAtom);
 
         const AtomicString& nameValue = getNameAttribute();
-        if (!nameValue.isNull()) {
-            updateNameForTreeScope(oldScope, nameValue, nullAtom);
-            if (oldDocument)
-                updateNameForDocument(oldDocument, nameValue, nullAtom);
-        }
+        if (!nameValue.isNull())
+            updateName(nameValue, nullAtom);
 
         if (hasTagName(labelTag)) {
-            if (oldScope->shouldCacheLabelsByForAttribute())
-                updateLabel(oldScope, fastGetAttribute(forAttr), nullAtom);
+            TreeScope* treeScope = insertionPoint->treeScope();
+            if (treeScope->shouldCacheLabelsByForAttribute())
+                updateLabel(treeScope, fastGetAttribute(forAttr), nullAtom);
         }
     }
 
     ContainerNode::removedFrom(insertionPoint);
-
     if (wasInDocument) {
         if (hasPendingResources())
             document()->accessSVGExtensions()->removeElementFromPendingResources(this);
@@ -2766,47 +2749,19 @@
 }
 #endif
 
-void Element::updateName(const AtomicString& oldName, const AtomicString& newName)
+inline void Element::updateName(const AtomicString& oldName, const AtomicString& newName)
 {
-    if (!isInTreeScope())
+    if (!inDocument() || isInShadowTree())
         return;
 
     if (oldName == newName)
         return;
 
-    updateNameForTreeScope(treeScope(), oldName, newName);
-
-    if (!inDocument() || isInShadowTree())
-        return;
-
-    Document* htmlDocument = document();
-    if (!htmlDocument->isHTMLDocument())
-        return;
-
-    updateNameForDocument(toHTMLDocument(htmlDocument), oldName, newName);
-}
-
-void Element::updateNameForTreeScope(TreeScope* scope, const AtomicString& oldName, const AtomicString& newName)
-{
-    ASSERT(isInTreeScope());
-    ASSERT(oldName != newName);
-
-    if (!oldName.isEmpty())
-        scope->removeElementByName(oldName, this);
-    if (!newName.isEmpty())
-        scope->addElementByName(newName, this);
-}
-
-void Element::updateNameForDocument(HTMLDocument* document, const AtomicString& oldName, const AtomicString& newName)
-{
-    ASSERT(inDocument() && !isInShadowTree());
-    ASSERT(oldName != newName);
-
     if (shouldRegisterAsNamedItem())
         updateNamedItemRegistration(oldName, newName);
 }
 
-void Element::updateId(const AtomicString& oldId, const AtomicString& newId)
+inline void Element::updateId(const AtomicString& oldId, const AtomicString& newId)
 {
     if (!isInTreeScope())
         return;
@@ -2814,19 +2769,10 @@
     if (oldId == newId)
         return;
 
-    updateIdForTreeScope(treeScope(), oldId, newId);
-
-    if (!inDocument() || isInShadowTree())
-        return;
-
-    Document* htmlDocument = document();
-    if (!htmlDocument->isHTMLDocument())
-        return;
-
-    updateIdForDocument(toHTMLDocument(htmlDocument), oldId, newId);
+    updateId(treeScope(), oldId, newId);
 }
 
-void Element::updateIdForTreeScope(TreeScope* scope, const AtomicString& oldId, const AtomicString& newId)
+inline void Element::updateId(TreeScope* scope, const AtomicString& oldId, const AtomicString& newId)
 {
     ASSERT(isInTreeScope());
     ASSERT(oldId != newId);
@@ -2835,12 +2781,6 @@
         scope->removeElementById(oldId, this);
     if (!newId.isEmpty())
         scope->addElementById(newId, this);
-}
-
-void Element::updateIdForDocument(HTMLDocument* document, const AtomicString& oldId, const AtomicString& newId)
-{
-    ASSERT(inDocument() && !isInShadowTree());
-    ASSERT(oldId != newId);
 
     if (shouldRegisterAsExtraNamedItem())
         updateExtraNamedItemRegistration(oldId, newId);
@@ -2931,7 +2871,8 @@
 
 void Element::updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName)
 {
-    ASSERT(document()->isHTMLDocument());
+    if (!document()->isHTMLDocument())
+        return;
 
     if (!oldName.isEmpty())
         toHTMLDocument(document())->removeNamedItem(oldName);
@@ -2942,7 +2883,8 @@
 
 void Element::updateExtraNamedItemRegistration(const AtomicString& oldId, const AtomicString& newId)
 {
-    ASSERT(document()->isHTMLDocument());
+    if (!document()->isHTMLDocument())
+        return;
 
     if (!oldId.isEmpty())
         toHTMLDocument(document())->removeExtraNamedItem(oldId);
@@ -3080,10 +3022,6 @@
         return;
     }
 
-    // We can't update window and document's named item maps since the presence of image and object elements depend on other attributes and children.
-    // Fortunately, those named item maps are only updated when this element is in the document, which should never be the case.
-    ASSERT(!inDocument());
-
     const AtomicString& oldID = getIdAttribute();
     const AtomicString& newID = other.getIdAttribute();