/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// check the pref if (!Preferences::GetBool("layout.xml.prettyprint", true)) { return NS_OK;
}
// Find the root element
RefPtr<Element> rootElement = aDocument->GetRootElement();
NS_ENSURE_TRUE(rootElement, NS_ERROR_UNEXPECTED);
// nsXMLContentSink should not ask us to pretty print an XML doc that comes // with a CanAttachShadowDOM() == true root element, but just in case: if (rootElement->CanAttachShadowDOM()) {
MOZ_DIAGNOSTIC_CRASH("We shouldn't be getting this root element"); return NS_ERROR_UNEXPECTED;
}
// Ok, we should prettyprint. Let's do it!
*aDidPrettyPrint = true;
nsresult rv = NS_OK;
// Attach an UA Widget Shadow Root on it.
rootElement->AttachAndSetUAShadowRoot(Element::NotifyUAWidgetSetup::No);
RefPtr<ShadowRoot> shadowRoot = rootElement->GetShadowRoot();
MOZ_RELEASE_ASSERT(shadowRoot && shadowRoot->IsUAWidget(), "There should be a UA Shadow Root here.");
// Append the document fragment to the shadow dom.
shadowRoot->AppendChild(*resultFragment, err); if (NS_WARN_IF(err.Failed())) { return err.StealNSResult();
}
// Create a DocumentL10n, as the XML document is not allowed to have one. // Make it sync so that the test for bug 590812 does not require a setTimeout.
RefPtr<DocumentL10n> l10n = DocumentL10n::Create(aDocument, true);
NS_ENSURE_TRUE(l10n, NS_ERROR_UNEXPECTED);
l10n->AddResourceId("dom/XMLPrettyPrint.ftl"_ns);
// Localize the shadow DOM header
Element* l10nRoot = shadowRoot->GetElementById(u"header"_ns);
NS_ENSURE_TRUE(l10nRoot, NS_ERROR_UNEXPECTED);
l10n->SetRootInfo(l10nRoot);
l10n->ConnectRoot(*l10nRoot, true, err); if (NS_WARN_IF(err.Failed())) { return err.StealNSResult();
}
RefPtr<Promise> promise = l10n->TranslateRoots(err); if (NS_WARN_IF(err.Failed())) { return err.StealNSResult();
}
// Observe the document so we know when to switch to "normal" view
aDocument->AddObserver(this);
mDocument = aDocument;
NS_ADDREF_THIS();
return NS_OK;
}
void nsXMLPrettyPrinter::MaybeUnhook(nsIContent* aContent) { // If aContent is null, the document-node was modified. // If it is not null but in the shadow tree or the <scrollbar> NACs, // the change was in the generated content, and it should be ignored. bool isGeneratedContent =
aContent &&
(aContent->IsInNativeAnonymousSubtree() || aContent->IsInShadowTree());
if (!isGeneratedContent && !mUnhookPending) { // Can't blindly to mUnhookPending after AddScriptRunner, // since AddScriptRunner _could_ in theory run us // synchronously
mUnhookPending = true;
nsContentUtils::AddScriptRunner(NewRunnableMethod( "nsXMLPrettyPrinter::Unhook", this, &nsXMLPrettyPrinter::Unhook));
}
}
void nsXMLPrettyPrinter::Unhook() {
mDocument->RemoveObserver(this);
nsCOMPtr<Element> element = mDocument->GetDocumentElement();
if (element) { // Remove the shadow root
element->UnattachShadow();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.