diff --git a/content/base/public/nsContentUtils.h b/content/base/public/nsContentUtils.h index 233879470a44..8858d1e26dd5 100644 --- a/content/base/public/nsContentUtils.h +++ b/content/base/public/nsContentUtils.h @@ -192,20 +192,18 @@ public: static nsresult Init(); /** - * Get a scope from aOldDocument and one from aNewDocument. Also get a - * context through one of the scopes, from the stack or the safe context. + * Get a scope from aNewDocument. Also get a context through the scope of one + * of the documents, from the stack or the safe context. * - * @param aOldDocument The document to get aOldScope from. + * @param aOldDocument The document to try to get a context from. May be null. * @param aNewDocument The document to get aNewScope from. * @param aCx [out] Context gotten through one of the scopes, from the stack * or the safe context. - * @param aOldScope [out] Scope gotten from aOldDocument. * @param aNewScope [out] Scope gotten from aNewDocument. */ - static nsresult GetContextAndScopes(nsIDocument *aOldDocument, - nsIDocument *aNewDocument, - JSContext **aCx, JSObject **aOldScope, - JSObject **aNewScope); + static nsresult GetContextAndScope(nsIDocument *aOldDocument, + nsIDocument *aNewDocument, + JSContext **aCx, JSObject **aNewScope); /** * When a document's scope changes (e.g., from document.open(), call this diff --git a/content/base/src/nsContentUtils.cpp b/content/base/src/nsContentUtils.cpp index eece10335fbe..8dfe9c5995d4 100644 --- a/content/base/src/nsContentUtils.cpp +++ b/content/base/src/nsContentUtils.cpp @@ -1371,22 +1371,17 @@ nsContentUtils::InProlog(nsINode *aNode) } static JSContext * -GetContextFromDocument(nsIDocument *aDocument, JSObject** aGlobalObject) +GetContextFromDocument(nsIDocument *aDocument) { nsIScriptGlobalObject *sgo = aDocument->GetScopeObject(); if (!sgo) { // No script global, no context. - - *aGlobalObject = nsnull; - return nsnull; } - *aGlobalObject = sgo->GetGlobalJSObject(); - nsIScriptContext *scx = sgo->GetContext(); if (!scx) { - // No context left in the old scope... + // No context left in the scope... return nsnull; } @@ -1396,39 +1391,27 @@ GetContextFromDocument(nsIDocument *aDocument, JSObject** aGlobalObject) // static nsresult -nsContentUtils::GetContextAndScopes(nsIDocument *aOldDocument, - nsIDocument *aNewDocument, JSContext **aCx, - JSObject **aOldScope, JSObject **aNewScope) +nsContentUtils::GetContextAndScope(nsIDocument *aOldDocument, + nsIDocument *aNewDocument, JSContext **aCx, + JSObject **aNewScope) { *aCx = nsnull; - *aOldScope = nsnull; *aNewScope = nsnull; - JSObject *newScope = nsnull; - nsIScriptGlobalObject *newSGO = aNewDocument->GetScopeObject(); - if (!newSGO || !(newScope = newSGO->GetGlobalJSObject())) { - return NS_OK; + JSObject *newScope = aNewDocument->GetWrapper(); + JSObject *global; + if (!newScope) { + nsIScriptGlobalObject *newSGO = aNewDocument->GetScopeObject(); + if (!newSGO || !(global = newSGO->GetGlobalJSObject())) { + return NS_OK; + } } NS_ENSURE_TRUE(sXPConnect, NS_ERROR_NOT_INITIALIZED); - // Make sure to get our hands on the right scope object, since - // GetWrappedNativeOfNativeObject doesn't call PreCreate and hence won't get - // the right scope if we pass in something bogus. The right scope lives on - // the script global of the old document. - // XXXbz note that if GetWrappedNativeOfNativeObject did call PreCreate it - // would get the wrong scope (that of the _new_ document), so we should be - // glad it doesn't! - JSObject *oldScope = nsnull; - JSContext *cx = GetContextFromDocument(aOldDocument, &oldScope); - - if (!oldScope) { - return NS_OK; - } - + JSContext *cx = aOldDocument ? GetContextFromDocument(aOldDocument) : nsnull; if (!cx) { - JSObject *dummy; - cx = GetContextFromDocument(aNewDocument, &dummy); + cx = GetContextFromDocument(aNewDocument); if (!cx) { // No context reachable from the old or new document, use the @@ -1450,8 +1433,15 @@ nsContentUtils::GetContextAndScopes(nsIDocument *aOldDocument, } } + if (!newScope && cx) { + jsval v; + nsresult rv = WrapNative(cx, global, aNewDocument, aNewDocument, &v); + NS_ENSURE_SUCCESS(rv, rv); + + newScope = JSVAL_TO_OBJECT(v); + } + *aCx = cx; - *aOldScope = oldScope; *aNewScope = newScope; return NS_OK; diff --git a/content/base/src/nsDOMDocumentType.cpp b/content/base/src/nsDOMDocumentType.cpp index 77fb7ab3a197..ab02ef9aad5f 100644 --- a/content/base/src/nsDOMDocumentType.cpp +++ b/content/base/src/nsDOMDocumentType.cpp @@ -268,16 +268,15 @@ nsDOMDocumentType::BindToTree(nsIDocument *aDocument, nsIContent *aParent, mNodeInfo.swap(newNodeInfo); - nsCOMPtr oldOwnerDoc = - do_QueryInterface(nsContentUtils::GetDocumentFromContext()); - nsIDocument *newOwnerDoc = nimgr->GetDocument(); - if (oldOwnerDoc && newOwnerDoc) { + JSObject *oldScope = GetWrapper(); + if (oldScope) { nsIXPConnect *xpc = nsContentUtils::XPConnect(); JSContext *cx = nsnull; - JSObject *oldScope = nsnull, *newScope = nsnull; - nsresult rv = nsContentUtils::GetContextAndScopes(oldOwnerDoc, newOwnerDoc, &cx, - &oldScope, &newScope); + JSObject *newScope = nsnull; + nsresult rv = nsContentUtils::GetContextAndScope(nsnull, + nimgr->GetDocument(), + &cx, &newScope); if (cx && xpc) { nsISupports *node = NS_ISUPPORTS_CAST(nsIContent*, this); nsCOMPtr oldWrapper; diff --git a/content/base/src/nsDocument.cpp b/content/base/src/nsDocument.cpp index 5c0d6af72eea..bdb7165ddaae 100644 --- a/content/base/src/nsDocument.cpp +++ b/content/base/src/nsDocument.cpp @@ -6082,11 +6082,10 @@ nsDocument::AdoptNode(nsIDOMNode *aAdoptedNode, nsIDOMNode **aResult) PRBool sameDocument = oldDocument == this; JSContext *cx = nsnull; - JSObject *oldScope = nsnull; + JSObject *oldScope = adoptedNode->GetWrapper(); JSObject *newScope = nsnull; - if (!sameDocument && oldDocument) { - rv = nsContentUtils::GetContextAndScopes(oldDocument, this, &cx, &oldScope, - &newScope); + if (oldScope && !sameDocument) { + rv = nsContentUtils::GetContextAndScope(oldDocument, this, &cx, &newScope); NS_ENSURE_SUCCESS(rv, rv); }