Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / html / HTMLTemplateElement.cpp
blob2a608f1c506c7bbf0b616861b102d294e57a54bc
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"
16 #include "nsAtom.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},
25 {nullptr, {}}};
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();
35 if (!contentsOwner) {
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,
50 nsGenericHTMLElement)
52 NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLTemplateElement)
54 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(HTMLTemplateElement,
55 nsGenericHTMLElement)
56 if (tmp->mContent) {
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,
65 nsGenericHTMLElement)
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,
80 bool aNotify) {
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,
93 nsAtom* aAttribute,
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