Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / dom / html / HTMLMetaElement.cpp
blob01c7e1e8248ca5f128b84b3e3f9425c7dd47188d
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/AsyncEventDispatcher.h"
8 #include "mozilla/dom/BindContext.h"
9 #include "mozilla/dom/HTMLMetaElement.h"
10 #include "mozilla/dom/HTMLMetaElementBinding.h"
11 #include "mozilla/dom/nsCSPService.h"
12 #include "mozilla/dom/nsCSPUtils.h"
13 #include "mozilla/dom/ViewportMetaData.h"
14 #include "mozilla/Logging.h"
15 #include "mozilla/StaticPrefs_security.h"
16 #include "nsContentUtils.h"
17 #include "nsSandboxFlags.h"
18 #include "nsStyleConsts.h"
19 #include "nsIXMLContentSink.h"
21 static mozilla::LazyLogModule gMetaElementLog("nsMetaElement");
22 #define LOG(msg) MOZ_LOG(gMetaElementLog, mozilla::LogLevel::Debug, msg)
23 #define LOG_ENABLED() MOZ_LOG_TEST(gMetaElementLog, mozilla::LogLevel::Debug)
25 NS_IMPL_NS_NEW_HTML_ELEMENT(Meta)
27 namespace mozilla::dom {
29 HTMLMetaElement::HTMLMetaElement(
30 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
31 : nsGenericHTMLElement(std::move(aNodeInfo)) {}
33 HTMLMetaElement::~HTMLMetaElement() = default;
35 NS_IMPL_ELEMENT_CLONE(HTMLMetaElement)
37 void HTMLMetaElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
38 const nsAttrValue* aValue,
39 const nsAttrValue* aOldValue,
40 nsIPrincipal* aSubjectPrincipal,
41 bool aNotify) {
42 if (aNameSpaceID == kNameSpaceID_None) {
43 if (Document* document = GetUncomposedDoc()) {
44 if (aName == nsGkAtoms::content) {
45 if (const nsAttrValue* name = GetParsedAttr(nsGkAtoms::name)) {
46 MetaAddedOrChanged(*document, *name, ChangeKind::ContentChange);
48 CreateAndDispatchEvent(*document, u"DOMMetaChanged"_ns);
49 } else if (aName == nsGkAtoms::name) {
50 if (aOldValue) {
51 MetaRemoved(*document, *aOldValue, ChangeKind::NameChange);
53 if (aValue) {
54 MetaAddedOrChanged(*document, *aValue, ChangeKind::NameChange);
56 CreateAndDispatchEvent(*document, u"DOMMetaChanged"_ns);
61 return nsGenericHTMLElement::AfterSetAttr(
62 aNameSpaceID, aName, aValue, aOldValue, aSubjectPrincipal, aNotify);
65 nsresult HTMLMetaElement::BindToTree(BindContext& aContext, nsINode& aParent) {
66 nsresult rv = nsGenericHTMLElement::BindToTree(aContext, aParent);
67 NS_ENSURE_SUCCESS(rv, rv);
68 if (!IsInUncomposedDoc()) {
69 return rv;
71 Document& doc = aContext.OwnerDoc();
73 bool shouldProcessMeta = true;
74 // We don't want to call ProcessMETATag when we are pretty print
75 // the document
76 if (doc.IsXMLDocument()) {
77 if (nsCOMPtr<nsIXMLContentSink> xmlSink =
78 do_QueryInterface(doc.GetCurrentContentSink())) {
79 if (xmlSink->IsPrettyPrintXML() &&
80 xmlSink->IsPrettyPrintHasSpecialRoot()) {
81 shouldProcessMeta = false;
86 if (shouldProcessMeta) {
87 doc.ProcessMETATag(this);
90 if (AttrValueIs(kNameSpaceID_None, nsGkAtoms::httpEquiv, nsGkAtoms::headerCSP,
91 eIgnoreCase)) {
92 // only accept <meta http-equiv="Content-Security-Policy" content=""> if it
93 // appears in the <head> element.
94 Element* headElt = doc.GetHeadElement();
95 if (headElt && IsInclusiveDescendantOf(headElt)) {
96 nsAutoString content;
97 GetContent(content);
99 if (LOG_ENABLED()) {
100 nsAutoCString documentURIspec;
101 if (nsIURI* documentURI = doc.GetDocumentURI()) {
102 documentURI->GetAsciiSpec(documentURIspec);
105 LOG(
106 ("HTMLMetaElement %p sets CSP '%s' on document=%p, "
107 "document-uri=%s",
108 this, NS_ConvertUTF16toUTF8(content).get(), &doc,
109 documentURIspec.get()));
111 CSP_ApplyMetaCSPToDoc(doc, content);
115 if (const nsAttrValue* name = GetParsedAttr(nsGkAtoms::name)) {
116 MetaAddedOrChanged(doc, *name, ChangeKind::TreeChange);
118 CreateAndDispatchEvent(doc, u"DOMMetaAdded"_ns);
119 return rv;
122 void HTMLMetaElement::UnbindFromTree(UnbindContext& aContext) {
123 if (Document* oldDoc = GetUncomposedDoc()) {
124 if (const nsAttrValue* name = GetParsedAttr(nsGkAtoms::name)) {
125 MetaRemoved(*oldDoc, *name, ChangeKind::TreeChange);
127 CreateAndDispatchEvent(*oldDoc, u"DOMMetaRemoved"_ns);
129 nsGenericHTMLElement::UnbindFromTree(aContext);
132 void HTMLMetaElement::CreateAndDispatchEvent(Document&,
133 const nsAString& aEventName) {
134 AsyncEventDispatcher::RunDOMEventWhenSafe(*this, aEventName, CanBubble::eYes,
135 ChromeOnlyDispatch::eYes);
138 JSObject* HTMLMetaElement::WrapNode(JSContext* aCx,
139 JS::Handle<JSObject*> aGivenProto) {
140 return HTMLMetaElement_Binding::Wrap(aCx, this, aGivenProto);
143 void HTMLMetaElement::MetaAddedOrChanged(Document& aDoc,
144 const nsAttrValue& aName,
145 ChangeKind aChangeKind) {
146 nsAutoString content;
147 const bool hasContent = GetAttr(nsGkAtoms::content, content);
148 if (aName.Equals(nsGkAtoms::viewport, eIgnoreCase)) {
149 if (hasContent) {
150 aDoc.SetMetaViewportData(MakeUnique<ViewportMetaData>(content));
152 return;
155 if (aName.Equals(nsGkAtoms::referrer, eIgnoreCase)) {
156 content = nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>(
157 content);
158 return aDoc.UpdateReferrerInfoFromMeta(content,
159 /* aPreload = */ false);
161 if (aName.Equals(nsGkAtoms::color_scheme, eIgnoreCase)) {
162 if (aChangeKind != ChangeKind::ContentChange) {
163 return aDoc.AddColorSchemeMeta(*this);
165 return aDoc.RecomputeColorScheme();
169 void HTMLMetaElement::MetaRemoved(Document& aDoc, const nsAttrValue& aName,
170 ChangeKind aChangeKind) {
171 MOZ_ASSERT(aChangeKind != ChangeKind::ContentChange,
172 "Content change can't trigger removal");
173 if (aName.Equals(nsGkAtoms::color_scheme, eIgnoreCase)) {
174 return aDoc.RemoveColorSchemeMeta(*this);
178 } // namespace mozilla::dom