diff --git a/dom/html/nsGenericHTMLElement.cpp b/dom/html/nsGenericHTMLElement.cpp index 6cac85bf37fd..7e8b39ccce1b 100644 --- a/dom/html/nsGenericHTMLElement.cpp +++ b/dom/html/nsGenericHTMLElement.cpp @@ -177,8 +177,12 @@ NS_INTERFACE_MAP_END_INHERITING(nsGenericHTMLElementBase) nsresult nsGenericHTMLElement::CopyInnerTo(Element* aDst, bool aPreallocateChildren) { + MOZ_ASSERT(!aDst->GetUncomposedDoc(), + "Should not CopyInnerTo an Element in a document"); nsresult rv; + bool reparse = (aDst->OwnerDoc() != OwnerDoc()); + rv = static_cast(aDst)->mAttrsAndChildren. EnsureCapacityToClone(mAttrsAndChildren, aPreallocateChildren); NS_ENSURE_SUCCESS(rv, rv); @@ -188,11 +192,14 @@ nsGenericHTMLElement::CopyInnerTo(Element* aDst, bool aPreallocateChildren) const nsAttrName *name = mAttrsAndChildren.AttrNameAt(i); const nsAttrValue *value = mAttrsAndChildren.AttrAt(i); - nsAutoString valStr; - value->ToString(valStr); - if (name->Equals(nsGkAtoms::style, kNameSpaceID_None) && value->Type() == nsAttrValue::eCSSDeclaration) { + // We still clone CSS attributes, even in the cross-document case. + // https://github.com/w3c/webappsec-csp/issues/212 + + nsAutoString valStr; + value->ToString(valStr); + DeclarationBlock* decl = value->GetCSSDeclarationValue(); // We can't just set this as a string, because that will fail // to reparse the string into style data until the node is @@ -201,13 +208,19 @@ nsGenericHTMLElement::CopyInnerTo(Element* aDst, bool aPreallocateChildren) rv = aDst->SetInlineStyleDeclaration(declClone, &valStr, false); NS_ENSURE_SUCCESS(rv, rv); + } else if (reparse) { + nsAutoString valStr; + value->ToString(valStr); - continue; + rv = aDst->SetAttr(name->NamespaceID(), name->LocalName(), + name->GetPrefix(), valStr, false); + NS_ENSURE_SUCCESS(rv, rv); + } else { + nsAttrValue valueCopy(*value); + rv = aDst->SetParsedAttr(name->NamespaceID(), name->LocalName(), + name->GetPrefix(), valueCopy, false); + NS_ENSURE_SUCCESS(rv, rv); } - - rv = aDst->SetAttr(name->NamespaceID(), name->LocalName(), - name->GetPrefix(), valStr, false); - NS_ENSURE_SUCCESS(rv, rv); } return NS_OK;