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() =
38 nsresult
XMLStylesheetProcessingInstruction::BindToTree(BindContext
& aContext
,
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
));
51 void XMLStylesheetProcessingInstruction::UnbindFromTree(bool aNullParent
) {
52 nsCOMPtr
<Document
> oldDoc
= GetUncomposedDoc();
54 ProcessingInstruction::UnbindFromTree(aNullParent
);
55 Unused
<< UpdateStyleSheetInternal(oldDoc
, nullptr);
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
);
70 void XMLStylesheetProcessingInstruction::GetCharset(nsAString
& aCharset
) {
71 if (!GetAttrValue(nsGkAtoms::charset
, aCharset
)) {
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)) {
88 if (!GetAttrValue(nsGkAtoms::href
, href
)) {
96 nsContentUtils::GetPseudoAttributeValue(data
, nsGkAtoms::title
, title
);
98 nsAutoString alternateAttr
;
99 nsContentUtils::GetPseudoAttributeValue(data
, nsGkAtoms::alternate
,
102 bool alternate
= alternateAttr
.EqualsLiteral("yes");
103 if (alternate
&& title
.IsEmpty()) {
104 // alternates must have title
109 nsContentUtils::GetPseudoAttributeValue(data
, nsGkAtoms::media
, media
);
111 // Make sure the type handling here matches
112 // nsXMLContentSink::HandleProcessingInstruction
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")) {
122 Document
* doc
= OwnerDoc();
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
{
134 MakeAndAddRef
<ReferrerInfo
>(*doc
),
138 /* integrity = */ u
""_ns
,
139 /* nonce = */ u
""_ns
,
140 alternate
? HasAlternateRel::Yes
: HasAlternateRel::No
,
142 IsExplicitlyEnabled::No
,
146 already_AddRefed
<CharacterData
>
147 XMLStylesheetProcessingInstruction::CloneDataNode(
148 mozilla::dom::NodeInfo
* aNodeInfo
, bool aCloneText
) const {
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