Bug 1665252 - remove allowpaymentrequest attribute from HTMLIFrameElement r=dom-worke...
[gecko.git] / dom / xml / XMLStylesheetProcessingInstruction.cpp
blob182f3a6307c880ea5236d4c5766d78715b956485
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"
8 #include "nsContentUtils.h"
9 #include "nsNetUtil.h"
11 namespace mozilla {
12 namespace dom {
14 // nsISupports implementation
16 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(
17 XMLStylesheetProcessingInstruction, ProcessingInstruction)
19 NS_IMPL_CYCLE_COLLECTION_CLASS(XMLStylesheetProcessingInstruction)
21 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(
22 XMLStylesheetProcessingInstruction, ProcessingInstruction)
23 tmp->LinkStyle::Traverse(cb);
24 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
26 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(
27 XMLStylesheetProcessingInstruction, ProcessingInstruction)
28 tmp->LinkStyle::Unlink();
29 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
31 XMLStylesheetProcessingInstruction::~XMLStylesheetProcessingInstruction() =
32 default;
34 // nsIContent
36 nsresult XMLStylesheetProcessingInstruction::BindToTree(BindContext& aContext,
37 nsINode& aParent) {
38 nsresult rv = ProcessingInstruction::BindToTree(aContext, aParent);
39 NS_ENSURE_SUCCESS(rv, rv);
41 void (XMLStylesheetProcessingInstruction::*update)() =
42 &XMLStylesheetProcessingInstruction::UpdateStyleSheetInternal;
43 nsContentUtils::AddScriptRunner(NewRunnableMethod(
44 "dom::XMLStylesheetProcessingInstruction::BindToTree", this, update));
46 return rv;
49 void XMLStylesheetProcessingInstruction::UnbindFromTree(bool aNullParent) {
50 nsCOMPtr<Document> oldDoc = GetUncomposedDoc();
52 ProcessingInstruction::UnbindFromTree(aNullParent);
53 Unused << UpdateStyleSheetInternal(oldDoc, nullptr);
56 // nsINode
58 void XMLStylesheetProcessingInstruction::SetNodeValueInternal(
59 const nsAString& aNodeValue, ErrorResult& aError) {
60 CharacterData::SetNodeValueInternal(aNodeValue, aError);
61 if (!aError.Failed()) {
62 Unused << UpdateStyleSheetInternal(nullptr, nullptr, ForceUpdate::Yes);
66 // LinkStyle
68 void XMLStylesheetProcessingInstruction::GetCharset(nsAString& aCharset) {
69 if (!GetAttrValue(nsGkAtoms::charset, aCharset)) {
70 aCharset.Truncate();
74 void XMLStylesheetProcessingInstruction::OverrideBaseURI(nsIURI* aNewBaseURI) {
75 mOverriddenBaseURI = aNewBaseURI;
78 Maybe<LinkStyle::SheetInfo>
79 XMLStylesheetProcessingInstruction::GetStyleSheetInfo() {
80 // xml-stylesheet PI is special only in prolog
81 if (!nsContentUtils::InProlog(this)) {
82 return Nothing();
85 nsAutoString href;
86 if (!GetAttrValue(nsGkAtoms::href, href)) {
87 return Nothing();
90 nsAutoString data;
91 GetData(data);
93 nsAutoString title;
94 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::title, title);
96 nsAutoString alternateAttr;
97 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::alternate,
98 alternateAttr);
100 bool alternate = alternateAttr.EqualsLiteral("yes");
101 if (alternate && title.IsEmpty()) {
102 // alternates must have title
103 return Nothing();
106 nsAutoString media;
107 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::media, media);
109 // Make sure the type handling here matches
110 // nsXMLContentSink::HandleProcessingInstruction
111 nsAutoString type;
112 nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::type, type);
114 nsAutoString mimeType, notUsed;
115 nsContentUtils::SplitMimeType(type, mimeType, notUsed);
116 if (!mimeType.IsEmpty() && !mimeType.LowerCaseEqualsLiteral("text/css")) {
117 return Nothing();
120 Document* doc = OwnerDoc();
121 nsIURI* baseURL =
122 mOverriddenBaseURI ? mOverriddenBaseURI.get() : doc->GetDocBaseURI();
123 auto encoding = doc->GetDocumentCharacterSet();
124 nsCOMPtr<nsIURI> uri;
125 NS_NewURI(getter_AddRefs(uri), href, encoding, baseURL);
127 return Some(SheetInfo{
128 *doc,
129 this,
130 uri.forget(),
131 nullptr,
132 MakeAndAddRef<ReferrerInfo>(*doc),
133 CORS_NONE,
134 title,
135 media,
136 /* integrity = */ u""_ns,
137 /* nonce = */ u""_ns,
138 alternate ? HasAlternateRel::Yes : HasAlternateRel::No,
139 IsInline::No,
140 IsExplicitlyEnabled::No,
144 already_AddRefed<CharacterData>
145 XMLStylesheetProcessingInstruction::CloneDataNode(
146 mozilla::dom::NodeInfo* aNodeInfo, bool aCloneText) const {
147 nsAutoString data;
148 GetData(data);
149 RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
150 auto* nim = ni->NodeInfoManager();
151 return do_AddRef(new (nim)
152 XMLStylesheetProcessingInstruction(ni.forget(), data));
155 } // namespace dom
156 } // namespace mozilla