2003-10-22 10:09:48 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2002-10-22 09:28:36 +04:00
|
|
|
|
|
|
|
#include "nsXMLPrettyPrinter.h"
|
2004-04-30 03:34:19 +04:00
|
|
|
#include "nsContentUtils.h"
|
2002-10-22 09:28:36 +04:00
|
|
|
#include "nsIDOMCSSStyleDeclaration.h"
|
|
|
|
#include "nsIDOMDocumentXBL.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIXSLTProcessor.h"
|
2006-06-16 00:30:44 +04:00
|
|
|
#include "nsSyncLoadService.h"
|
2005-11-29 02:56:44 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2002-10-22 09:28:36 +04:00
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsNetUtil.h"
|
2010-08-24 11:05:56 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2002-10-22 09:28:36 +04:00
|
|
|
#include "nsIDOMDocumentFragment.h"
|
2007-02-17 02:02:08 +03:00
|
|
|
#include "nsBindingManager.h"
|
2013-01-24 21:45:50 +04:00
|
|
|
#include "nsXBLService.h"
|
2010-08-28 09:54:28 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2011-05-25 10:31:59 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-10-03 00:09:18 +04:00
|
|
|
#include "nsIDocument.h"
|
2013-10-03 18:02:36 +04:00
|
|
|
#include "nsVariant.h"
|
2015-08-12 14:39:31 +03:00
|
|
|
#include "mozilla/dom/CustomEvent.h"
|
2002-10-22 09:28:36 +04:00
|
|
|
|
2011-05-25 10:31:59 +04:00
|
|
|
using namespace mozilla;
|
2010-08-24 11:05:56 +04:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsXMLPrettyPrinter,
|
|
|
|
nsIDocumentObserver,
|
|
|
|
nsIMutationObserver)
|
2002-10-22 09:28:36 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsXMLPrettyPrinter::nsXMLPrettyPrinter() : mDocument(nullptr),
|
2011-10-17 18:59:28 +04:00
|
|
|
mUnhookPending(false)
|
2002-10-22 09:28:36 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXMLPrettyPrinter::~nsXMLPrettyPrinter()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mDocument, "we shouldn't be referencing the document still");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-31 05:42:48 +04:00
|
|
|
nsXMLPrettyPrinter::PrettyPrint(nsIDocument* aDocument,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* aDidPrettyPrint)
|
2002-10-22 09:28:36 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aDidPrettyPrint = false;
|
2012-08-10 22:01:16 +04:00
|
|
|
|
2002-10-22 09:28:36 +04:00
|
|
|
// Check for iframe with display:none. Such iframes don't have presshells
|
2010-06-25 17:59:57 +04:00
|
|
|
if (!aDocument->GetShell()) {
|
2002-10-22 09:28:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we're in an invisible iframe
|
2005-11-29 02:56:44 +03:00
|
|
|
nsPIDOMWindow *internalWin = aDocument->GetWindow();
|
2002-10-22 09:28:36 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> frameElem;
|
|
|
|
if (internalWin) {
|
|
|
|
internalWin->GetFrameElement(getter_AddRefs(frameElem));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frameElem) {
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleDeclaration> computedStyle;
|
|
|
|
nsCOMPtr<nsIDOMDocument> frameOwnerDoc;
|
|
|
|
frameElem->GetOwnerDocument(getter_AddRefs(frameOwnerDoc));
|
2011-04-24 10:54:25 +04:00
|
|
|
if (frameOwnerDoc) {
|
|
|
|
nsCOMPtr<nsIDOMWindow> window;
|
|
|
|
frameOwnerDoc->GetDefaultView(getter_AddRefs(window));
|
|
|
|
if (window) {
|
|
|
|
window->GetComputedStyle(frameElem,
|
|
|
|
EmptyString(),
|
|
|
|
getter_AddRefs(computedStyle));
|
2002-10-22 09:28:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (computedStyle) {
|
|
|
|
nsAutoString visibility;
|
|
|
|
computedStyle->GetPropertyValue(NS_LITERAL_STRING("visibility"),
|
|
|
|
visibility);
|
2004-05-23 02:15:22 +04:00
|
|
|
if (!visibility.EqualsLiteral("visible")) {
|
2002-10-22 09:28:36 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the pref
|
2011-09-29 10:19:26 +04:00
|
|
|
if (!Preferences::GetBool("layout.xml.prettyprint", true)) {
|
2004-04-30 03:34:19 +04:00
|
|
|
return NS_OK;
|
2002-10-22 09:28:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, we should prettyprint. Let's do it!
|
2011-10-17 18:59:28 +04:00
|
|
|
*aDidPrettyPrint = true;
|
2002-10-22 09:28:36 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// Load the XSLT
|
|
|
|
nsCOMPtr<nsIURI> xslUri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(xslUri),
|
2005-05-20 20:42:15 +04:00
|
|
|
NS_LITERAL_CSTRING("chrome://global/content/xml/XMLPrettyPrint.xsl"));
|
2002-10-22 09:28:36 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> xslDocument;
|
2014-09-22 15:49:12 +04:00
|
|
|
rv = nsSyncLoadService::LoadDocument(xslUri, nsContentUtils::GetSystemPrincipal(),
|
2014-11-18 16:47:20 +03:00
|
|
|
nullptr, true, mozilla::net::RP_Default,
|
2006-06-16 00:30:44 +04:00
|
|
|
getter_AddRefs(xslDocument));
|
2002-10-22 09:28:36 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Transform the document
|
|
|
|
nsCOMPtr<nsIXSLTProcessor> transformer =
|
2003-04-01 23:50:42 +04:00
|
|
|
do_CreateInstance("@mozilla.org/document-transformer;1?type=xslt", &rv);
|
2002-10-22 09:28:36 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = transformer->ImportStylesheet(xslDocument);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> resultFragment;
|
|
|
|
nsCOMPtr<nsIDOMDocument> sourceDocument = do_QueryInterface(aDocument);
|
2005-10-02 18:40:30 +04:00
|
|
|
rv = transformer->TransformToFragment(sourceDocument, sourceDocument,
|
2002-10-22 09:28:36 +04:00
|
|
|
getter_AddRefs(resultFragment));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-01-24 21:45:50 +04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2002-10-22 09:28:36 +04:00
|
|
|
|
2013-01-24 21:45:50 +04:00
|
|
|
// Grab the XBL service.
|
|
|
|
nsXBLService* xblService = nsXBLService::GetInstance();
|
|
|
|
NS_ENSURE_TRUE(xblService, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
// Compute the binding URI.
|
2010-08-28 09:54:28 +04:00
|
|
|
nsCOMPtr<nsIURI> bindingUri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(bindingUri),
|
|
|
|
NS_LITERAL_STRING("chrome://global/content/xml/XMLPrettyPrint.xml#prettyprint"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-10-22 09:28:36 +04:00
|
|
|
|
2013-01-24 21:45:50 +04:00
|
|
|
// Compute the bound element.
|
2010-08-28 09:54:28 +04:00
|
|
|
nsCOMPtr<nsIContent> rootCont = aDocument->GetRootElement();
|
|
|
|
NS_ENSURE_TRUE(rootCont, NS_ERROR_UNEXPECTED);
|
2002-10-22 09:28:36 +04:00
|
|
|
|
2013-01-24 21:45:50 +04:00
|
|
|
// Grab the system principal.
|
|
|
|
nsCOMPtr<nsIPrincipal> sysPrincipal;
|
|
|
|
nsContentUtils::GetSecurityManager()->
|
|
|
|
GetSystemPrincipal(getter_AddRefs(sysPrincipal));
|
|
|
|
|
|
|
|
// Load the bindings.
|
|
|
|
nsRefPtr<nsXBLBinding> unused;
|
|
|
|
bool ignored;
|
2013-01-24 21:45:50 +04:00
|
|
|
rv = xblService->LoadBindings(rootCont, bindingUri, sysPrincipal,
|
2013-01-24 21:45:50 +04:00
|
|
|
getter_AddRefs(unused), &ignored);
|
2002-10-22 09:28:36 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-10-03 18:02:36 +04:00
|
|
|
// Fire an event at the bound element to pass it |resultFragment|.
|
2015-08-12 14:39:31 +03:00
|
|
|
nsRefPtr<CustomEvent> event =
|
|
|
|
NS_NewDOMCustomEvent(rootCont, nullptr, nullptr);
|
|
|
|
MOZ_ASSERT(event);
|
2013-10-03 18:02:36 +04:00
|
|
|
nsCOMPtr<nsIWritableVariant> resultFragmentVariant = new nsVariant();
|
|
|
|
rv = resultFragmentVariant->SetAsISupports(resultFragment);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2015-08-12 14:39:31 +03:00
|
|
|
rv = event->InitCustomEvent(NS_LITERAL_STRING("prettyprint-dom-created"),
|
|
|
|
/* bubbles = */ false, /* cancelable = */ false,
|
|
|
|
/* detail = */ resultFragmentVariant);
|
2013-10-03 18:02:36 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-08-12 14:39:31 +03:00
|
|
|
event->SetTrusted(true);
|
2013-10-03 18:02:36 +04:00
|
|
|
bool dummy;
|
2015-08-12 14:39:31 +03:00
|
|
|
rv = rootCont->DispatchEvent(static_cast<Event*>(event), &dummy);
|
2002-10-22 09:28:36 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-01-30 01:04:45 +03:00
|
|
|
|
2002-10-22 09:28:36 +04:00
|
|
|
// 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.
|
2011-10-03 23:13:33 +04:00
|
|
|
if ((!aContent || !aContent->GetBindingParent()) && !mUnhookPending) {
|
2011-06-01 01:26:13 +04:00
|
|
|
// Can't blindly to mUnhookPending after AddScriptRunner,
|
|
|
|
// since AddScriptRunner _could_ in theory run us
|
|
|
|
// synchronously
|
2011-10-17 18:59:28 +04:00
|
|
|
mUnhookPending = true;
|
2011-06-01 01:26:13 +04:00
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
NS_NewRunnableMethod(this, &nsXMLPrettyPrinter::Unhook));
|
2002-10-22 09:28:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
2011-06-01 01:26:13 +04:00
|
|
|
nsXMLPrettyPrinter::Unhook()
|
2002-10-22 09:28:36 +04:00
|
|
|
{
|
2011-06-01 01:26:13 +04:00
|
|
|
mDocument->RemoveObserver(this);
|
2013-01-24 21:45:50 +04:00
|
|
|
nsCOMPtr<Element> element = mDocument->GetDocumentElement();
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
mDocument->BindingManager()->ClearBinding(element);
|
2011-06-01 01:26:13 +04:00
|
|
|
}
|
2002-10-22 09:28:36 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mDocument = nullptr;
|
2002-10-22 09:28:36 +04:00
|
|
|
|
2011-06-01 01:26:13 +04:00
|
|
|
NS_RELEASE_THIS();
|
2002-10-22 09:28:36 +04:00
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
2002-10-22 09:28:36 +04:00
|
|
|
nsXMLPrettyPrinter::AttributeChanged(nsIDocument* aDocument,
|
2010-08-24 11:05:56 +04:00
|
|
|
Element* aElement,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aNameSpaceID,
|
2002-10-22 09:28:36 +04:00
|
|
|
nsIAtom* aAttribute,
|
2015-07-25 09:01:19 +03:00
|
|
|
int32_t aModType,
|
|
|
|
const nsAttrValue* aOldValue)
|
2002-10-22 09:28:36 +04:00
|
|
|
{
|
2010-08-24 11:05:56 +04:00
|
|
|
MaybeUnhook(aElement);
|
2002-10-22 09:28:36 +04:00
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
2002-10-22 09:28:36 +04:00
|
|
|
nsXMLPrettyPrinter::ContentAppended(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
2010-05-11 05:12:34 +04:00
|
|
|
nsIContent* aFirstNewContent,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aNewIndexInContainer)
|
2002-10-22 09:28:36 +04:00
|
|
|
{
|
|
|
|
MaybeUnhook(aContainer);
|
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
2002-10-22 09:28:36 +04:00
|
|
|
nsXMLPrettyPrinter::ContentInserted(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aIndexInContainer)
|
2002-10-22 09:28:36 +04:00
|
|
|
{
|
|
|
|
MaybeUnhook(aContainer);
|
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
2002-10-22 09:28:36 +04:00
|
|
|
nsXMLPrettyPrinter::ContentRemoved(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aIndexInContainer,
|
2010-07-22 02:05:17 +04:00
|
|
|
nsIContent* aPreviousSibling)
|
2002-10-22 09:28:36 +04:00
|
|
|
{
|
|
|
|
MaybeUnhook(aContainer);
|
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
2006-07-02 11:23:10 +04:00
|
|
|
nsXMLPrettyPrinter::NodeWillBeDestroyed(const nsINode* aNode)
|
2002-10-22 09:28:36 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mDocument = nullptr;
|
2002-10-22 09:28:36 +04:00
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult NS_NewXMLPrettyPrinter(nsXMLPrettyPrinter** aPrinter)
|
|
|
|
{
|
|
|
|
*aPrinter = new nsXMLPrettyPrinter;
|
|
|
|
NS_ADDREF(*aPrinter);
|
|
|
|
return NS_OK;
|
|
|
|
}
|