1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/HTMLTemplateElement.h"
8 #include "mozilla/dom/HTMLTemplateElementBinding.h"
10 #include "mozilla/dom/Document.h"
11 #include "mozilla/dom/NameSpaceConstants.h"
12 #include "mozilla/dom/ShadowRootBinding.h"
13 #include "nsGenericHTMLElement.h"
14 #include "nsGkAtoms.h"
15 #include "nsStyleConsts.h"
18 NS_IMPL_NS_NEW_HTML_ELEMENT(Template
)
20 namespace mozilla::dom
{
22 static constexpr nsAttrValue::EnumTable kShadowRootModeTable
[] = {
23 {"open", ShadowRootMode::Open
},
24 {"closed", ShadowRootMode::Closed
},
27 const nsAttrValue::EnumTable
* kShadowRootModeDefault
= &kShadowRootModeTable
[2];
29 HTMLTemplateElement::HTMLTemplateElement(
30 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
)
31 : nsGenericHTMLElement(std::move(aNodeInfo
)) {
32 SetHasWeirdParserInsertionMode();
34 Document
* contentsOwner
= OwnerDoc()->GetTemplateContentsOwner();
36 MOZ_CRASH("There should always be a template contents owner.");
39 mContent
= contentsOwner
->CreateDocumentFragment();
40 mContent
->SetHost(this);
43 HTMLTemplateElement::~HTMLTemplateElement() {
44 if (mContent
&& mContent
->GetHost() == this) {
45 mContent
->SetHost(nullptr);
49 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(HTMLTemplateElement
,
52 NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLTemplateElement
)
54 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(HTMLTemplateElement
,
57 if (tmp
->mContent
->GetHost() == tmp
) {
58 tmp
->mContent
->SetHost(nullptr);
60 tmp
->mContent
= nullptr;
62 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
64 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(HTMLTemplateElement
,
66 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContent
)
67 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
69 NS_IMPL_ELEMENT_CLONE(HTMLTemplateElement
)
71 JSObject
* HTMLTemplateElement::WrapNode(JSContext
* aCx
,
72 JS::Handle
<JSObject
*> aGivenProto
) {
73 return HTMLTemplateElement_Binding::Wrap(aCx
, this, aGivenProto
);
76 void HTMLTemplateElement::AfterSetAttr(int32_t aNamespaceID
, nsAtom
* aName
,
77 const nsAttrValue
* aValue
,
78 const nsAttrValue
* aOldValue
,
79 nsIPrincipal
* aMaybeScriptedPrincipal
,
81 if (aNamespaceID
== kNameSpaceID_None
&& aName
== nsGkAtoms::shadowrootmode
&&
82 aValue
&& aValue
->Type() == nsAttrValue::ValueType::eEnum
&&
83 !mShadowRootMode
.isSome()) {
84 mShadowRootMode
.emplace(
85 static_cast<ShadowRootMode
>(aValue
->GetEnumValue()));
88 nsGenericHTMLElement::AfterSetAttr(aNamespaceID
, aName
, aValue
, aOldValue
,
89 aMaybeScriptedPrincipal
, aNotify
);
92 bool HTMLTemplateElement::ParseAttribute(int32_t aNamespaceID
,
94 const nsAString
& aValue
,
95 nsIPrincipal
* aMaybeScriptedPrincipal
,
96 nsAttrValue
& aResult
) {
97 if (aNamespaceID
== kNameSpaceID_None
&&
98 aAttribute
== nsGkAtoms::shadowrootmode
) {
99 return aResult
.ParseEnumValue(aValue
, kShadowRootModeTable
, false, nullptr);
101 return nsGenericHTMLElement::ParseAttribute(aNamespaceID
, aAttribute
, aValue
,
102 aMaybeScriptedPrincipal
, aResult
);
105 void HTMLTemplateElement::SetHTMLUnsafe(const nsAString
& aHTML
) {
106 RefPtr
<DocumentFragment
> content
= mContent
;
107 nsContentUtils::SetHTMLUnsafe(content
, this, aHTML
);
110 } // namespace mozilla::dom