Bug 1945643 - Update to mozilla-nimbus-schemas 2025.1.1 r=chumphreys
[gecko.git] / dom / xml / XMLStylesheetProcessingInstruction.cpp
bloba022ed427227071f5bc735ac057b9958be7b1dfe
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 "XMLStylesheetProcessingInstruction.h"
9 #include "mozilla/dom/Document.h"
10 #include "mozilla/dom/FetchPriority.h"
11 #include "mozilla/dom/ReferrerInfo.h"
12 #include "nsContentUtils.h"
13 #include "nsNetUtil.h"
15 namespace mozilla::dom {
17 // nsISupports implementation
19 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(
20 XMLStylesheetProcessingInstruction, ProcessingInstruction)
22 NS_IMPL_CYCLE_COLLECTION_CLASS(XMLStylesheetProcessingInstruction)
24 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(
25 XMLStylesheetProcessingInstruction, ProcessingInstruction)
26 tmp->LinkStyle::Traverse(cb);
27 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
29 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(
30 XMLStylesheetProcessingInstruction, ProcessingInstruction)
31 tmp->LinkStyle::Unlink();
32 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
34 XMLStylesheetProcessingInstruction::~XMLStylesheetProcessingInstruction() =
35 default;
37 // nsIContent
39 nsresult XMLStylesheetProcessingInstruction::BindToTree(BindContext& aContext,
40 nsINode& aParent) {
41 nsresult rv = ProcessingInstruction::BindToTree(aContext, aParent);
42 NS_ENSURE_SUCCESS(rv, rv);
44 void (XMLStylesheetProcessingInstruction::*update)() =
45 &XMLStylesheetProcessingInstruction::UpdateStyleSheetInternal;
46 nsContentUtils::AddScriptRunner(NewRunnableMethod(
47 "dom::XMLStylesheetProcessingInstruction::BindToTree", this, update));
49 return rv;
52 void XMLStylesheetProcessingInstruction::UnbindFromTree(
53 UnbindContext& aContext) {
54 nsCOMPtr<Document> oldDoc = GetUncomposedDoc();
56 ProcessingInstruction::UnbindFromTree(aContext);
57 Unused << UpdateStyleSheetInternal(oldDoc, nullptr);
60 // nsINode
62 void XMLStylesheetProcessingInstruction::SetNodeValueInternal(
63 const nsAString& aNodeValue, ErrorResult& aError) {
64 CharacterData::SetNodeValueInternal(aNodeValue, aError);
65 if (!aError.Failed()) {
66 Unused << UpdateStyleSheetInternal(nullptr, nullptr, ForceUpdate::Yes);
70 // LinkStyle
72 void XMLStylesheetProcessingInstruction::GetCharset(nsAString& aCharset) {
73 if (!GetAttrValue(nsGkAtoms::charset, aCharset)) {
74 aCharset.Truncate();
78 void XMLStylesheetProcessingInstruction::OverrideBaseURI(nsIURI* aNewBaseURI) {
79 mOverriddenBaseURI = aNewBaseURI;
82 Maybe<LinkStyle::SheetInfo>
83 XMLStylesheetProcessingInstruction::GetStyleSheetInfo() {
84 // xml-stylesheet PI is special only in prolog
85 if (!nsContentUtils::InProlog(this)) {
86 return Nothing();
89 nsAutoString href;
90 if (!GetAttrValue(nsGkAtoms::href, href)) {
91 return Nothing();
94 nsAutoString data;
95 GetData(data);
97 nsAutoString title;
98 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::title, title);
100 nsAutoString alternateAttr;
101 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::alternate,
102 alternateAttr);
104 bool alternate = alternateAttr.EqualsLiteral("yes");
105 if (alternate && title.IsEmpty()) {
106 // alternates must have title
107 return Nothing();
110 nsAutoString media;
111 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::media, media);
113 // Make sure the type handling here matches
114 // nsXMLContentSink::HandleProcessingInstruction
115 nsAutoString type;
116 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::type, type);
118 nsAutoString mimeType, notUsed;
119 nsContentUtils::SplitMimeType(type, mimeType, notUsed);
120 if (!mimeType.IsEmpty() && !mimeType.LowerCaseEqualsLiteral("text/css")) {
121 return Nothing();
124 Document* doc = OwnerDoc();
125 nsIURI* baseURL =
126 mOverriddenBaseURI ? mOverriddenBaseURI.get() : doc->GetDocBaseURI();
127 auto encoding = doc->GetDocumentCharacterSet();
128 nsCOMPtr<nsIURI> uri;
129 NS_NewURI(getter_AddRefs(uri), href, encoding, baseURL);
131 return Some(SheetInfo{
132 *doc,
133 this,
134 uri.forget(),
135 nullptr,
136 MakeAndAddRef<ReferrerInfo>(*doc),
137 CORS_NONE,
138 title,
139 media,
140 /* integrity = */ u""_ns,
141 /* nonce = */ u""_ns,
142 alternate ? HasAlternateRel::Yes : HasAlternateRel::No,
143 IsInline::No,
144 IsExplicitlyEnabled::No,
145 FetchPriority::Auto,
149 already_AddRefed<CharacterData>
150 XMLStylesheetProcessingInstruction::CloneDataNode(
151 mozilla::dom::NodeInfo* aNodeInfo, bool aCloneText) const {
152 nsAutoString data;
153 GetData(data);
154 RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
155 auto* nim = ni->NodeInfoManager();
156 return do_AddRef(new (nim)
157 XMLStylesheetProcessingInstruction(ni.forget(), data));
160 } // namespace mozilla::dom