Bug 657353 part 1. Switch the XML prettyprinter to using a script runner instead of EndUpdate to determine when to drop prettyprinting. r=sicking

This commit is contained in:
Boris Zbarsky 2011-05-31 17:26:13 -04:00
parent 967c22d3ce
commit 5bb505a0b1
2 changed files with 23 additions and 34 deletions

View File

@ -198,44 +198,33 @@ 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()) {
if (!aContent || !aContent->GetBindingParent() && !mUnhookPending) {
// Can't blindly to mUnhookPending after AddScriptRunner,
// since AddScriptRunner _could_ in theory run us
// synchronously
mUnhookPending = PR_TRUE;
nsContentUtils::AddScriptRunner(
NS_NewRunnableMethod(this, &nsXMLPrettyPrinter::Unhook));
}
}
// nsIDocumentObserver implementation
void
nsXMLPrettyPrinter::BeginUpdate(nsIDocument* aDocument,
nsUpdateType aUpdateType)
nsXMLPrettyPrinter::Unhook()
{
mUpdateDepth++;
}
mDocument->RemoveObserver(this);
nsCOMPtr<nsIDOMDocument> document = do_QueryInterface(mDocument);
nsCOMPtr<nsIDOMElement> rootElem;
document->GetDocumentElement(getter_AddRefs(rootElem));
void
nsXMLPrettyPrinter::EndUpdate(nsIDocument* aDocument, nsUpdateType aUpdateType)
{
mUpdateDepth--;
// Only remove the binding once we're outside all updates. This protects us
// from nasty surprices of elements being removed from the document in the
// midst of setting attributes etc.
if (mUnhookPending && mUpdateDepth == 0) {
mDocument->RemoveObserver(this);
nsCOMPtr<nsIDOMDocument> document = do_QueryInterface(mDocument);
nsCOMPtr<nsIDOMElement> rootElem;
document->GetDocumentElement(getter_AddRefs(rootElem));
if (rootElem) {
nsCOMPtr<nsIDOMDocumentXBL> xblDoc = do_QueryInterface(mDocument);
xblDoc->RemoveBinding(rootElem,
NS_LITERAL_STRING("chrome://global/content/xml/XMLPrettyPrint.xml#prettyprint"));
}
mDocument = nsnull;
NS_RELEASE_THIS();
if (rootElem) {
nsCOMPtr<nsIDOMDocumentXBL> xblDoc = do_QueryInterface(mDocument);
xblDoc->RemoveBinding(rootElem,
NS_LITERAL_STRING("chrome://global/content/xml/XMLPrettyPrint.xml#prettyprint"));
}
mDocument = nsnull;
NS_RELEASE_THIS();
}
void

View File

@ -51,10 +51,6 @@ public:
NS_DECL_ISUPPORTS
// nsIDocumentObserver
NS_DECL_NSIDOCUMENTOBSERVER_BEGINUPDATE
NS_DECL_NSIDOCUMENTOBSERVER_ENDUPDATE
// nsIMutationObserver
NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
@ -72,6 +68,10 @@ public:
*/
nsresult PrettyPrint(nsIDocument* aDocument, PRBool* aDidPrettyPrint);
/**
* Unhook the prettyprinter
*/
void Unhook();
private:
/**
* Signals for unhooking by setting mUnhookPending if the node changed is