Bug 1437956 - Pretty print XML with Shadow DOM r=smaug

This patch puts the transformed pretty print DOM into a Shadow DOM.
The stylesheet is loaded with an @import in a <style> block, so the
monospace stylesheet had to be left out.

The XBL binding is kept, pending removal when Shadow DOM ships.
It's still needed to handle the case when Shadow DOM is pref'd off too.

MozReview-Commit-ID: DQRsXB8tumF

--HG--
extra : rebase_source : 6edc3d82392af4d98de454a5228328379a0fb7ee
This commit is contained in:
Timothy Guan-tin Chien 2018-06-01 17:45:11 +08:00
Родитель 9862c7afdc
Коммит b9a40e754b
9 изменённых файлов: 137 добавлений и 86 удалений

Просмотреть файл

@ -1224,6 +1224,16 @@ Element::AttachShadow(const ShadowRootInit& aInit, ErrorResult& aError)
return nullptr;
}
if (StaticPrefs::dom_webcomponents_shadowdom_report_usage()) {
OwnerDoc()->ReportShadowDOMUsage();
}
return AttachShadowWithoutNameChecks(aInit.mMode);
}
already_AddRefed<ShadowRoot>
Element::AttachShadowWithoutNameChecks(ShadowRootMode aMode)
{
nsAutoScriptBlocker scriptBlocker;
RefPtr<mozilla::dom::NodeInfo> nodeInfo =
@ -1244,14 +1254,10 @@ Element::AttachShadow(const ShadowRootInit& aInit, ErrorResult& aError)
* and mode is inits mode.
*/
RefPtr<ShadowRoot> shadowRoot =
new ShadowRoot(this, aInit.mMode, nodeInfo.forget());
new ShadowRoot(this, aMode, nodeInfo.forget());
shadowRoot->SetIsComposedDocParticipant(IsInComposedDoc());
if (StaticPrefs::dom_webcomponents_shadowdom_report_usage()) {
OwnerDoc()->ReportShadowDOMUsage();
}
/**
* 5. Set context objects shadow root to shadow.
*/
@ -1263,6 +1269,27 @@ Element::AttachShadow(const ShadowRootInit& aInit, ErrorResult& aError)
return shadowRoot.forget();
}
void
Element::UnattachShadow()
{
if (!GetShadowRoot()) {
return;
}
nsAutoScriptBlocker scriptBlocker;
if (nsIDocument* doc = GetComposedDoc()) {
if (nsIPresShell* shell = doc->GetShell()) {
shell->DestroyFramesForAndRestyle(this);
}
}
MOZ_ASSERT(!GetPrimaryFrame());
// Simply unhook the shadow root from the element.
MOZ_ASSERT(!GetShadowRoot()->HasSlots(), "Won't work when shadow root has slots!");
SetShadowRoot(nullptr);
}
void
Element::GetAttribute(const nsAString& aName, DOMString& aReturn)
{

Просмотреть файл

@ -1263,6 +1263,10 @@ public:
// Shadow DOM v1
already_AddRefed<ShadowRoot> AttachShadow(const ShadowRootInit& aInit,
ErrorResult& aError);
already_AddRefed<ShadowRoot> AttachShadowWithoutNameChecks(ShadowRootMode aMode);
void UnattachShadow();
ShadowRoot* GetShadowRootByMode() const;
void SetSlot(const nsAString& aName, ErrorResult& aError);
void GetSlot(nsAString& aName);

Просмотреть файл

@ -139,6 +139,7 @@ private:
public:
void AddSlot(HTMLSlotElement* aSlot);
void RemoveSlot(HTMLSlotElement* aSlot);
bool HasSlots() const { return !mSlotMap.IsEmpty(); };
const RawServoAuthorStyles* ServoStyles() const
{

Просмотреть файл

@ -124,6 +124,21 @@ nsXMLPrettyPrinter::PrettyPrint(nsIDocument* aDocument,
return err.StealNSResult();
}
// Find the root element
RefPtr<Element> rootElement = aDocument->GetRootElement();
NS_ENSURE_TRUE(rootElement, NS_ERROR_UNEXPECTED);
if (nsContentUtils::IsShadowDOMEnabled()) {
// Attach a closed shadow root on it.
RefPtr<ShadowRoot> shadowRoot =
rootElement->AttachShadowWithoutNameChecks(ShadowRootMode::Closed);
// Append the document fragment to the shadow dom.
shadowRoot->AppendChild(*resultFragment, err);
if (NS_WARN_IF(err.Failed())) {
return err.StealNSResult();
}
} else {
//
// Apply the prettprint XBL binding.
//
@ -143,10 +158,6 @@ nsXMLPrettyPrinter::PrettyPrint(nsIDocument* aDocument,
NS_LITERAL_STRING("chrome://global/content/xml/XMLPrettyPrint.xml#prettyprint"));
NS_ENSURE_SUCCESS(rv, rv);
// Compute the bound element.
RefPtr<Element> rootElement = aDocument->GetRootElement();
NS_ENSURE_TRUE(rootElement, NS_ERROR_UNEXPECTED);
// Grab the system principal.
nsCOMPtr<nsIPrincipal> sysPrincipal;
nsContentUtils::GetSecurityManager()->
@ -187,6 +198,7 @@ nsXMLPrettyPrinter::PrettyPrint(nsIDocument* aDocument,
if (NS_WARN_IF(err.Failed())) {
return err.StealNSResult();
}
}
// Observe the document so we know when to switch to "normal" view
aDocument->AddObserver(this);
@ -200,9 +212,15 @@ nsXMLPrettyPrinter::PrettyPrint(nsIDocument* aDocument,
void
nsXMLPrettyPrinter::MaybeUnhook(nsIContent* aContent)
{
// If there either aContent is null (the document-node was modified) or
// there isn't a binding parent we know it's non-anonymous content.
if ((!aContent || !aContent->GetBindingParent()) && !mUnhookPending) {
// If aContent is null, the document-node was modified.
// If it is not null but in the shadow tree, the <scrollbar> NACs,
// or the XBL binding, the change was in the generated content, and
// it should be ignored.
bool isGeneratedContent = !aContent ?
false :
aContent->GetBindingParent() || aContent->IsInShadowTree();
if (!isGeneratedContent && !mUnhookPending) {
// Can't blindly to mUnhookPending after AddScriptRunner,
// since AddScriptRunner _could_ in theory run us
// synchronously
@ -219,6 +237,10 @@ nsXMLPrettyPrinter::Unhook()
nsCOMPtr<Element> element = mDocument->GetDocumentElement();
if (element) {
// Remove the shadow root
element->UnattachShadow();
// Remove the bound XBL binding
mDocument->BindingManager()->ClearBinding(element);
}

Просмотреть файл

@ -44,7 +44,7 @@ private:
/**
* Signals for unhooking by setting mUnhookPending if the node changed is
* non-anonymous content.
* not in the shadow root tree nor in anonymous content.
*
* @param aContent content that has changed
*/

Просмотреть файл

@ -1,10 +0,0 @@
@charset "UTF-8";
/* 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/. */
#tree,
.expandable-opening::-moz-list-bullet {
font-family: monospace;
white-space: pre;
}

Просмотреть файл

@ -15,7 +15,12 @@
<handlers>
<handler event="prettyprint-dom-created" allowuntrusted="false">
<![CDATA[
document.getAnonymousNodes(this).item(0).appendChild(event.detail);
let container = document.getAnonymousNodes(this).item(0);
// Take the child nodes from the passed <div id="top">
// and append them to our own.
for (let el of event.detail.childNodes) {
container.appendChild(el);
}
]]>
</handler>
</handlers>

Просмотреть файл

@ -17,8 +17,10 @@
<xsl:output method="xml"/>
<xsl:template match="/">
<link href="chrome://global/content/xml/XMLPrettyPrint.css" type="text/css" rel="stylesheet"/>
<link title="Monospace" href="chrome://global/content/xml/XMLMonoPrint.css" type="text/css" rel="alternate stylesheet"/>
<div id="top">
<style>
@import url("chrome://global/content/xml/XMLPrettyPrint.css");
</style>
<div id="header" dir="&locale.dir;">
<p>
&xml.nostylesheet;
@ -27,6 +29,7 @@
<main id="tree" class="highlight">
<xsl:apply-templates/>
</main>
</div>
</xsl:template>
<xsl:template match="*">

Просмотреть файл

@ -4,6 +4,5 @@
toolkit.jar:
content/global/xml/XMLPrettyPrint.css (XMLPrettyPrint.css)
content/global/xml/XMLMonoPrint.css (XMLMonoPrint.css)
content/global/xml/XMLPrettyPrint.xml (XMLPrettyPrint.xml)
content/global/xml/XMLPrettyPrint.xsl (XMLPrettyPrint.xsl)
content/global/xml/XMLPrettyPrint.xml (XMLPrettyPrint.xml)