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() =
39 nsresult
XMLStylesheetProcessingInstruction::BindToTree(BindContext
& aContext
,
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
));
52 void XMLStylesheetProcessingInstruction::UnbindFromTree(
53 UnbindContext
& aContext
) {
54 nsCOMPtr
<Document
> oldDoc
= GetUncomposedDoc();
56 ProcessingInstruction::UnbindFromTree(aContext
);
57 Unused
<< UpdateStyleSheetInternal(oldDoc
, nullptr);
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
);
72 void XMLStylesheetProcessingInstruction::GetCharset(nsAString
& aCharset
) {
73 if (!GetAttrValue(nsGkAtoms::charset
, aCharset
)) {
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)) {
90 if (!GetAttrValue(nsGkAtoms::href
, href
)) {
98 nsContentUtils::GetPseudoAttributeValue(data
, nsGkAtoms::title
, title
);
100 nsAutoString alternateAttr
;
101 nsContentUtils::GetPseudoAttributeValue(data
, nsGkAtoms::alternate
,
104 bool alternate
= alternateAttr
.EqualsLiteral("yes");
105 if (alternate
&& title
.IsEmpty()) {
106 // alternates must have title
111 nsContentUtils::GetPseudoAttributeValue(data
, nsGkAtoms::media
, media
);
113 // Make sure the type handling here matches
114 // nsXMLContentSink::HandleProcessingInstruction
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")) {
124 Document
* doc
= OwnerDoc();
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
{
136 MakeAndAddRef
<ReferrerInfo
>(*doc
),
140 /* integrity = */ u
""_ns
,
141 /* nonce = */ u
""_ns
,
142 alternate
? HasAlternateRel::Yes
: HasAlternateRel::No
,
144 IsExplicitlyEnabled::No
,
149 already_AddRefed
<CharacterData
>
150 XMLStylesheetProcessingInstruction::CloneDataNode(
151 mozilla::dom::NodeInfo
* aNodeInfo
, bool aCloneText
) const {
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