Files
palemoon27/dom/xml/nsXMLPrettyPrinter.cpp
T
roytam1 7a25ca546c import changes from `dev' branch of rmottola/Arctic-Fox:
- fix patch ordering coming from PM repo (3a8768f44)
- Bug 1119074 - If we're stopping event propagation from XUL popups, also stop them from crossing process boundaries r=smaug,felipe (965e2193e)
- Bug 1082145 - |js::WatchGuts| can leak |wpmap|. r=erahm (12c86f3d3)
- No Bug - Improve Interpreter stack accessor assertions. (rs=Waldo) (8ba7a702c)
- Bug 874842 - Return Event instead of nsIDOMEvent (748b57fd2)
- Bug 1165966 - Add test cases r=terrence (79a909d5b)
- Bug 1167025: Do not mix bool and int in bitwise-or in blendpsMask; r=sunfish (f0f23c0c4)
- Bug 1158323 - Make sure we set a base rect on document elements that have margins set. r=tn (c253a2ef8)
- Bug 1178847 - Move the code from ChromeProcessController::InitializeRoot to APZCCallbackHelper so it can be reused in the child process. r=botond (16d539bcb)
- Bug 1165966 - Add error checking when populating safepoints r=bhackett (c66d249d1)
- spacing and pointer style (cddc1bac4)
- Bug 1196027 - check the actual current marking mode instead of the permanent intention, r=terrence (eddcfd7fb)
- Bug 1206590: Move gcWeakMapList from JSCompartment to JS::Zone. r=terrence (7e5e0d505)
- Bug 1181908 part 1. Fix support for JSOP_OBJECT in scripts parsed on background threads by clearing the unboxedLayouts list on the background thread parsing compartment when merging the parse result to the target compartment. r=jandem (25c6a3b01)
- Bug 1163207 - Make RematerializedFrame store the real callee. (r=shu) (ce276e91c)
- Bug 1164448 - Handle unwound rectifier frames as exit frames in JitProfilingFrameIterator. r=jandem (bb639b4e2)
- Bug 1164448 - Add test. r=jandem (83f5cc608)
- Bug 1196497 - Don't assert that the replacer continues to pass IsArray during JSON.stringify. (If the replacer was a revocable proxy to an array, revoking the proxy would make the replacer no longer IsArray.) r=evilpie (442c3823f)
- Bug 1177247 - Prevent HandlePossibleViewportChange from clobbering a restored scroll position from forward/back navigation. r=botond (4202ac757)
- Bug 1182772, optimize ProcessGlobal out from CC graph (and also TabChild's EventListeners), r=mccr8 (ccb2278bf)
- Bug 1139155 - Add a mechanism to know when the APZ is done processing. r=botond (17328e5be)
- Bug 1171537 - Allow URIs to be the empty string in TabParent::RecvCreateWindow. r=billm. (e280e994c)
- Bug 1173219 - Return nsresults from TabParent::RecvCreateWindow to make opening windows more robust. r=billm (9f0633b15)
- Bug 1142817 - Use UniquePtr in testXDR_sourceMap. r=erahm (7ec437162)
2021-11-24 09:53:39 +08:00

266 lines
8.6 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */
#include "nsXMLPrettyPrinter.h"
#include "nsContentUtils.h"
#include "nsIDOMCSSStyleDeclaration.h"
#include "nsIDOMDocumentXBL.h"
#include "nsIObserver.h"
#include "nsIXSLTProcessor.h"
#include "nsSyncLoadService.h"
#include "nsPIDOMWindow.h"
#include "nsIDOMElement.h"
#include "nsIDOMDocument.h"
#include "nsIServiceManager.h"
#include "nsNetUtil.h"
#include "mozilla/dom/Element.h"
#include "nsIDOMDocumentFragment.h"
#include "nsBindingManager.h"
#include "nsXBLService.h"
#include "nsIScriptSecurityManager.h"
#include "mozilla/Preferences.h"
#include "nsIDocument.h"
#include "nsVariant.h"
#include "mozilla/dom/CustomEvent.h"
using namespace mozilla;
using namespace mozilla::dom;
NS_IMPL_ISUPPORTS(nsXMLPrettyPrinter,
nsIDocumentObserver,
nsIMutationObserver)
nsXMLPrettyPrinter::nsXMLPrettyPrinter() : mDocument(nullptr),
mUnhookPending(false)
{
}
nsXMLPrettyPrinter::~nsXMLPrettyPrinter()
{
NS_ASSERTION(!mDocument, "we shouldn't be referencing the document still");
}
nsresult
nsXMLPrettyPrinter::PrettyPrint(nsIDocument* aDocument,
bool* aDidPrettyPrint)
{
*aDidPrettyPrint = false;
// Check for iframe with display:none. Such iframes don't have presshells
if (!aDocument->GetShell()) {
return NS_OK;
}
// check if we're in an invisible iframe
nsPIDOMWindow *internalWin = aDocument->GetWindow();
nsCOMPtr<nsIDOMElement> frameElem;
if (internalWin) {
internalWin->GetFrameElement(getter_AddRefs(frameElem));
}
if (frameElem) {
nsCOMPtr<nsIDOMCSSStyleDeclaration> computedStyle;
nsCOMPtr<nsIDOMDocument> frameOwnerDoc;
frameElem->GetOwnerDocument(getter_AddRefs(frameOwnerDoc));
if (frameOwnerDoc) {
nsCOMPtr<nsIDOMWindow> window;
frameOwnerDoc->GetDefaultView(getter_AddRefs(window));
if (window) {
window->GetComputedStyle(frameElem,
EmptyString(),
getter_AddRefs(computedStyle));
}
}
if (computedStyle) {
nsAutoString visibility;
computedStyle->GetPropertyValue(NS_LITERAL_STRING("visibility"),
visibility);
if (!visibility.EqualsLiteral("visible")) {
return NS_OK;
}
}
}
// check the pref
if (!Preferences::GetBool("layout.xml.prettyprint", true)) {
return NS_OK;
}
// Ok, we should prettyprint. Let's do it!
*aDidPrettyPrint = true;
nsresult rv = NS_OK;
// Load the XSLT
nsCOMPtr<nsIURI> xslUri;
rv = NS_NewURI(getter_AddRefs(xslUri),
NS_LITERAL_CSTRING("chrome://global/content/xml/XMLPrettyPrint.xsl"));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDOMDocument> xslDocument;
rv = nsSyncLoadService::LoadDocument(xslUri, nsContentUtils::GetSystemPrincipal(),
nullptr, true, mozilla::net::RP_Default,
getter_AddRefs(xslDocument));
NS_ENSURE_SUCCESS(rv, rv);
// Transform the document
nsCOMPtr<nsIXSLTProcessor> transformer =
do_CreateInstance("@mozilla.org/document-transformer;1?type=xslt", &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = transformer->ImportStylesheet(xslDocument);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDOMDocumentFragment> resultFragment;
nsCOMPtr<nsIDOMDocument> sourceDocument = do_QueryInterface(aDocument);
rv = transformer->TransformToFragment(sourceDocument, sourceDocument,
getter_AddRefs(resultFragment));
NS_ENSURE_SUCCESS(rv, rv);
//
// Apply the prettprint XBL binding.
//
// We take some shortcuts here. In particular, we don't bother invoking the
// contstructor (since the binding has no constructor), and we don't bother
// calling LoadBindingDocument because it's a chrome:// URI and thus will get
// sync loaded no matter what.
//
// Grab the XBL service.
nsXBLService* xblService = nsXBLService::GetInstance();
NS_ENSURE_TRUE(xblService, NS_ERROR_NOT_AVAILABLE);
// Compute the binding URI.
nsCOMPtr<nsIURI> bindingUri;
rv = NS_NewURI(getter_AddRefs(bindingUri),
NS_LITERAL_STRING("chrome://global/content/xml/XMLPrettyPrint.xml#prettyprint"));
NS_ENSURE_SUCCESS(rv, rv);
// Compute the bound element.
nsCOMPtr<nsIContent> rootCont = aDocument->GetRootElement();
NS_ENSURE_TRUE(rootCont, NS_ERROR_UNEXPECTED);
// Grab the system principal.
nsCOMPtr<nsIPrincipal> sysPrincipal;
nsContentUtils::GetSecurityManager()->
GetSystemPrincipal(getter_AddRefs(sysPrincipal));
// Load the bindings.
nsRefPtr<nsXBLBinding> unused;
bool ignored;
rv = xblService->LoadBindings(rootCont, bindingUri, sysPrincipal,
getter_AddRefs(unused), &ignored);
NS_ENSURE_SUCCESS(rv, rv);
// Fire an event at the bound element to pass it |resultFragment|.
nsRefPtr<CustomEvent> event =
NS_NewDOMCustomEvent(rootCont, nullptr, nullptr);
MOZ_ASSERT(event);
nsCOMPtr<nsIWritableVariant> resultFragmentVariant = new nsVariant();
rv = resultFragmentVariant->SetAsISupports(resultFragment);
MOZ_ASSERT(NS_SUCCEEDED(rv));
rv = event->InitCustomEvent(NS_LITERAL_STRING("prettyprint-dom-created"),
/* bubbles = */ false, /* cancelable = */ false,
/* detail = */ resultFragmentVariant);
NS_ENSURE_SUCCESS(rv, rv);
event->SetTrusted(true);
bool dummy;
rv = rootCont->DispatchEvent(static_cast<Event*>(event), &dummy);
NS_ENSURE_SUCCESS(rv, rv);
// 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 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) {
// Can't blindly to mUnhookPending after AddScriptRunner,
// since AddScriptRunner _could_ in theory run us
// synchronously
mUnhookPending = true;
nsContentUtils::AddScriptRunner(
NS_NewRunnableMethod(this, &nsXMLPrettyPrinter::Unhook));
}
}
void
nsXMLPrettyPrinter::Unhook()
{
mDocument->RemoveObserver(this);
nsCOMPtr<Element> element = mDocument->GetDocumentElement();
if (element) {
mDocument->BindingManager()->ClearBinding(element);
}
mDocument = nullptr;
NS_RELEASE_THIS();
}
void
nsXMLPrettyPrinter::AttributeChanged(nsIDocument* aDocument,
Element* aElement,
int32_t aNameSpaceID,
nsIAtom* aAttribute,
int32_t aModType,
const nsAttrValue* aOldValue)
{
MaybeUnhook(aElement);
}
void
nsXMLPrettyPrinter::ContentAppended(nsIDocument* aDocument,
nsIContent* aContainer,
nsIContent* aFirstNewContent,
int32_t aNewIndexInContainer)
{
MaybeUnhook(aContainer);
}
void
nsXMLPrettyPrinter::ContentInserted(nsIDocument* aDocument,
nsIContent* aContainer,
nsIContent* aChild,
int32_t aIndexInContainer)
{
MaybeUnhook(aContainer);
}
void
nsXMLPrettyPrinter::ContentRemoved(nsIDocument* aDocument,
nsIContent* aContainer,
nsIContent* aChild,
int32_t aIndexInContainer,
nsIContent* aPreviousSibling)
{
MaybeUnhook(aContainer);
}
void
nsXMLPrettyPrinter::NodeWillBeDestroyed(const nsINode* aNode)
{
mDocument = nullptr;
NS_RELEASE_THIS();
}
nsresult NS_NewXMLPrettyPrinter(nsXMLPrettyPrinter** aPrinter)
{
*aPrinter = new nsXMLPrettyPrinter;
NS_ADDREF(*aPrinter);
return NS_OK;
}