Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / dom / xml / XMLStylesheetProcessingInstruction.cpp
blob9285672bbf079893cdee52e2beea5f9d84c1b353
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/ReferrerInfo.h"
11 #include "nsContentUtils.h"
12 #include "nsNetUtil.h"
14 namespace mozilla::dom {
16 // nsISupports implementation
18 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(
19 XMLStylesheetProcessingInstruction, ProcessingInstruction)
21 NS_IMPL_CYCLE_COLLECTION_CLASS(XMLStylesheetProcessingInstruction)
23 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(
24 XMLStylesheetProcessingInstruction, ProcessingInstruction)
25 tmp->LinkStyle::Traverse(cb);
26 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
28 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(
29 XMLStylesheetProcessingInstruction, ProcessingInstruction)
30 tmp->LinkStyle::Unlink();
31 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
33 XMLStylesheetProcessingInstruction::~XMLStylesheetProcessingInstruction() =
34 default;
36 // nsIContent
38 nsresult XMLStylesheetProcessingInstruction::BindToTree(BindContext& aContext,
39 nsINode& aParent) {
40 nsresult rv = ProcessingInstruction::BindToTree(aContext, aParent);
41 NS_ENSURE_SUCCESS(rv, rv);
43 void (XMLStylesheetProcessingInstruction::*update)() =
44 &XMLStylesheetProcessingInstruction::UpdateStyleSheetInternal;
45 nsContentUtils::AddScriptRunner(NewRunnableMethod(
46 "dom::XMLStylesheetProcessingInstruction::BindToTree", this, update));
48 return rv;
51 void XMLStylesheetProcessingInstruction::UnbindFromTree(bool aNullParent) {
52 nsCOMPtr<Document> oldDoc = GetUncomposedDoc();
54 ProcessingInstruction::UnbindFromTree(aNullParent);
55 Unused << UpdateStyleSheetInternal(oldDoc, nullptr);
58 // nsINode
60 void XMLStylesheetProcessingInstruction::SetNodeValueInternal(
61 const nsAString& aNodeValue, ErrorResult& aError) {
62 CharacterData::SetNodeValueInternal(aNodeValue, aError);
63 if (!aError.Failed()) {
64 Unused << UpdateStyleSheetInternal(nullptr, nullptr, ForceUpdate::Yes);
68 // LinkStyle
70 void XMLStylesheetProcessingInstruction::GetCharset(nsAString& aCharset) {
71 if (!GetAttrValue(nsGkAtoms::charset, aCharset)) {
72 aCharset.Truncate();
76 void XMLStylesheetProcessingInstruction::OverrideBaseURI(nsIURI* aNewBaseURI) {
77 mOverriddenBaseURI = aNewBaseURI;
80 Maybe<LinkStyle::SheetInfo>
81 XMLStylesheetProcessingInstruction::GetStyleSheetInfo() {
82 // xml-stylesheet PI is special only in prolog
83 if (!nsContentUtils::InProlog(this)) {
84 return Nothing();
87 nsAutoString href;
88 if (!GetAttrValue(nsGkAtoms::href, href)) {
89 return Nothing();
92 nsAutoString data;
93 GetData(data);
95 nsAutoString title;
96 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::title, title);
98 nsAutoString alternateAttr;
99 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::alternate,
100 alternateAttr);
102 bool alternate = alternateAttr.EqualsLiteral("yes");
103 if (alternate && title.IsEmpty()) {
104 // alternates must have title
105 return Nothing();
108 nsAutoString media;
109 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::media, media);
111 // Make sure the type handling here matches
112 // nsXMLContentSink::HandleProcessingInstruction
113 nsAutoString type;
114 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::type, type);
116 nsAutoString mimeType, notUsed;
117 nsContentUtils::SplitMimeType(type, mimeType, notUsed);
118 if (!mimeType.IsEmpty() && !mimeType.LowerCaseEqualsLiteral("text/css")) {
119 return Nothing();
122 Document* doc = OwnerDoc();
123 nsIURI* baseURL =
124 mOverriddenBaseURI ? mOverriddenBaseURI.get() : doc->GetDocBaseURI();
125 auto encoding = doc->GetDocumentCharacterSet();
126 nsCOMPtr<nsIURI> uri;
127 NS_NewURI(getter_AddRefs(uri), href, encoding, baseURL);
129 return Some(SheetInfo{
130 *doc,
131 this,
132 uri.forget(),
133 nullptr,
134 MakeAndAddRef<ReferrerInfo>(*doc),
135 CORS_NONE,
136 title,
137 media,
138 /* integrity = */ u""_ns,
139 /* nonce = */ u""_ns,
140 alternate ? HasAlternateRel::Yes : HasAlternateRel::No,
141 IsInline::No,
142 IsExplicitlyEnabled::No,
146 already_AddRefed<CharacterData>
147 XMLStylesheetProcessingInstruction::CloneDataNode(
148 mozilla::dom::NodeInfo* aNodeInfo, bool aCloneText) const {
149 nsAutoString data;
150 GetData(data);
151 RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
152 auto* nim = ni->NodeInfoManager();
153 return do_AddRef(new (nim)
154 XMLStylesheetProcessingInstruction(ni.forget(), data));
157 } // namespace mozilla::dom