From 3cff255f244d2ae4c89a75daa9f8af527a609c43 Mon Sep 17 00:00:00 2001 From: William Chen Date: Tue, 3 Jun 2014 16:09:41 -0700 Subject: [PATCH] Bug 1017896 - Ensure there is a template contents owner when creating template elements. r=bz --- content/base/src/nsDocument.cpp | 14 ++++++-- .../html/content/src/HTMLTemplateElement.cpp | 28 +++------------- .../html/content/src/HTMLTemplateElement.h | 2 -- .../mochitest/webcomponents/mochitest.ini | 1 + .../webcomponents/test_bug1017896.html | 32 +++++++++++++++++++ 5 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 dom/tests/mochitest/webcomponents/test_bug1017896.html diff --git a/content/base/src/nsDocument.cpp b/content/base/src/nsDocument.cpp index d97ead4dbcb..827eb86126f 100644 --- a/content/base/src/nsDocument.cpp +++ b/content/base/src/nsDocument.cpp @@ -4513,6 +4513,11 @@ nsDocument::SetScriptGlobalObject(nsIScriptGlobalObject *aScriptGlobalObject) // doing the initial document load and don't want to fire the event for this // change. mVisibilityState = GetVisibilityState(); + + // The global in the template contents owner document should be the same. + if (mTemplateContentsOwner && mTemplateContentsOwner != this) { + mTemplateContentsOwner->SetScriptGlobalObject(aScriptGlobalObject); + } } nsIScriptGlobalObject* @@ -9443,7 +9448,6 @@ nsDocument::GetTemplateContentsOwner() bool hasHadScriptObject = true; nsIScriptGlobalObject* scriptObject = GetScriptHandlingObject(hasHadScriptObject); - NS_ENSURE_TRUE(scriptObject || !hasHadScriptObject, nullptr); nsCOMPtr domDocument; nsresult rv = NS_NewDOMDocument(getter_AddRefs(domDocument), @@ -9461,12 +9465,16 @@ nsDocument::GetTemplateContentsOwner() mTemplateContentsOwner = do_QueryInterface(domDocument); NS_ENSURE_TRUE(mTemplateContentsOwner, nullptr); - mTemplateContentsOwner->SetScriptHandlingObject(scriptObject); + nsDocument* doc = static_cast(mTemplateContentsOwner.get()); + doc->mHasHadScriptHandlingObject = hasHadScriptObject; + + if (!scriptObject) { + mTemplateContentsOwner->SetScopeObject(GetScopeObject()); + } // Set |doc| as the template contents owner of itself so that // |doc| is the template contents owner of template elements created // by |doc|. - nsDocument* doc = static_cast(mTemplateContentsOwner.get()); doc->mTemplateContentsOwner = doc; } diff --git a/content/html/content/src/HTMLTemplateElement.cpp b/content/html/content/src/HTMLTemplateElement.cpp index 881b0c76748..f56e769dce5 100644 --- a/content/html/content/src/HTMLTemplateElement.cpp +++ b/content/html/content/src/HTMLTemplateElement.cpp @@ -11,21 +11,7 @@ #include "nsIAtom.h" #include "nsRuleData.h" -using namespace mozilla::dom; - -nsGenericHTMLElement* -NS_NewHTMLTemplateElement(already_AddRefed&& aNodeInfo, - FromParser aFromParser) -{ - HTMLTemplateElement* it = new HTMLTemplateElement(aNodeInfo); - nsresult rv = it->Init(); - if (NS_FAILED(rv)) { - delete it; - return nullptr; - } - - return it; -} +NS_IMPL_NS_NEW_HTML_ELEMENT(Template) namespace mozilla { namespace dom { @@ -34,18 +20,14 @@ HTMLTemplateElement::HTMLTemplateElement(already_AddRefed& aNodeInf : nsGenericHTMLElement(aNodeInfo) { SetHasWeirdParserInsertionMode(); -} -nsresult -HTMLTemplateElement::Init() -{ nsIDocument* contentsOwner = OwnerDoc()->GetTemplateContentsOwner(); - NS_ENSURE_TRUE(contentsOwner, NS_ERROR_UNEXPECTED); + if (!contentsOwner) { + MOZ_CRASH("There should always be a template contents owner."); + } mContent = contentsOwner->CreateDocumentFragment(); mContent->SetHost(this); - - return NS_OK; } HTMLTemplateElement::~HTMLTemplateElement() @@ -77,7 +59,7 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(HTMLTemplateElement) NS_INTERFACE_MAP_END_INHERITING(nsGenericHTMLElement) -NS_IMPL_ELEMENT_CLONE_WITH_INIT(HTMLTemplateElement) +NS_IMPL_ELEMENT_CLONE(HTMLTemplateElement) JSObject* HTMLTemplateElement::WrapNode(JSContext *aCx) diff --git a/content/html/content/src/HTMLTemplateElement.h b/content/html/content/src/HTMLTemplateElement.h index c7a51aa07c0..be14da09df5 100644 --- a/content/html/content/src/HTMLTemplateElement.h +++ b/content/html/content/src/HTMLTemplateElement.h @@ -28,8 +28,6 @@ public: virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const MOZ_OVERRIDE; - nsresult Init(); - DocumentFragment* Content() { return mContent; diff --git a/dom/tests/mochitest/webcomponents/mochitest.ini b/dom/tests/mochitest/webcomponents/mochitest.ini index cb91c3aa601..cc0829f6dbe 100644 --- a/dom/tests/mochitest/webcomponents/mochitest.ini +++ b/dom/tests/mochitest/webcomponents/mochitest.ini @@ -3,6 +3,7 @@ support-files = inert_style.css [test_bug900724.html] +[test_bug1017896.html] [test_content_element.html] [test_nested_content_element.html] [test_dest_insertion_points.html] diff --git a/dom/tests/mochitest/webcomponents/test_bug1017896.html b/dom/tests/mochitest/webcomponents/test_bug1017896.html new file mode 100644 index 00000000000..befec7d6d2a --- /dev/null +++ b/dom/tests/mochitest/webcomponents/test_bug1017896.html @@ -0,0 +1,32 @@ + + + + + Test template element in stale document. + + + + +Bug 1017896 +
+ + +