Bug 1425864 - Ensure printing documents which have ShadowDOM works, r=mrbkap,emilio

This commit is contained in:
Olli Pettay 2018-03-04 19:41:13 +09:00
parent 6042a9738b
commit 3faf40c897
3 changed files with 58 additions and 8 deletions

View File

@ -94,6 +94,24 @@ ShadowRoot::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
return mozilla::dom::ShadowRootBinding::Wrap(aCx, this, aGivenProto);
}
void
ShadowRoot::CloneInternalDataFrom(ShadowRoot* aOther)
{
size_t sheetCount = aOther->SheetCount();
for (size_t i = 0; i < sheetCount; ++i) {
StyleSheet* sheet = aOther->SheetAt(i);
if (sheet->IsApplicable()) {
RefPtr<StyleSheet> clonedSheet =
sheet->Clone(nullptr, nullptr, nullptr, nullptr);
if (clonedSheet) {
AppendStyleSheet(*clonedSheet.get());
Servo_AuthorStyles_AppendStyleSheet(mServoStyles.get(),
clonedSheet->AsServo());
}
}
}
}
void
ShadowRoot::AddSlot(HTMLSlotElement* aSlot)
{

View File

@ -89,6 +89,10 @@ public:
return &DocumentOrShadowRoot::EnsureDOMStyleSheets();
}
/**
* Clones internal state, for example stylesheets, of aOther to 'this'.
*/
void CloneInternalDataFrom(ShadowRoot* aOther);
private:
/**

View File

@ -657,14 +657,42 @@ nsNodeUtils::CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep,
}
}
if (aDeep && !aClone && aNode->IsElement()) {
if (ShadowRoot* shadowRoot = aNode->AsElement()->GetShadowRoot()) {
nsCOMPtr<nsINode> child =
CloneAndAdopt(shadowRoot, aClone, aDeep, nodeInfoManager,
aReparentScope, aNodesWithProperties, clone,
aError);
if (NS_WARN_IF(aError.Failed())) {
return nullptr;
if (aDeep && aNode->IsElement()) {
if (aClone) {
if (clone->OwnerDoc()->IsStaticDocument()) {
ShadowRoot* originalShadowRoot = aNode->AsElement()->GetShadowRoot();
if (originalShadowRoot) {
ShadowRootInit init;
init.mMode = originalShadowRoot->Mode();
RefPtr<ShadowRoot> newShadowRoot =
clone->AsElement()->AttachShadow(init, aError);
if (NS_WARN_IF(aError.Failed())) {
return nullptr;
}
newShadowRoot->CloneInternalDataFrom(originalShadowRoot);
for (nsIContent* origChild = originalShadowRoot->GetFirstChild();
origChild;
origChild = origChild->GetNextSibling()) {
nsCOMPtr<nsINode> child =
CloneAndAdopt(origChild, aClone, aDeep, nodeInfoManager,
aReparentScope, aNodesWithProperties, newShadowRoot,
aError);
if (NS_WARN_IF(aError.Failed())) {
return nullptr;
}
}
}
}
} else {
if (ShadowRoot* shadowRoot = aNode->AsElement()->GetShadowRoot()) {
nsCOMPtr<nsINode> child =
CloneAndAdopt(shadowRoot, aClone, aDeep, nodeInfoManager,
aReparentScope, aNodesWithProperties, clone,
aError);
if (NS_WARN_IF(aError.Failed())) {
return nullptr;
}
}
}
}