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"
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() =
36 nsresult
XMLStylesheetProcessingInstruction::BindToTree(BindContext
& aContext
,
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
));
49 void XMLStylesheetProcessingInstruction::UnbindFromTree(bool aNullParent
) {
50 nsCOMPtr
<Document
> oldDoc
= GetUncomposedDoc();
52 ProcessingInstruction::UnbindFromTree(aNullParent
);
53 Unused
<< UpdateStyleSheetInternal(oldDoc
, nullptr);
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
);
68 void XMLStylesheetProcessingInstruction::GetCharset(nsAString
& aCharset
) {
69 if (!GetAttrValue(nsGkAtoms::charset
, aCharset
)) {
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)) {
86 if (!GetAttrValue(nsGkAtoms::href
, href
)) {
94 nsContentUtils::GetPseudoAttributeValue(data
, nsGkAtoms::title
, title
);
96 nsAutoString alternateAttr
;
97 nsContentUtils::GetPseudoAttributeValue(data
, nsGkAtoms::alternate
,
100 bool alternate
= alternateAttr
.EqualsLiteral("yes");
101 if (alternate
&& title
.IsEmpty()) {
102 // alternates must have title
107 nsContentUtils::GetPseudoAttributeValue(data
, nsGkAtoms::media
, media
);
109 // Make sure the type handling here matches
110 // nsXMLContentSink::HandleProcessingInstruction
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")) {
120 Document
* doc
= OwnerDoc();
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
{
132 MakeAndAddRef
<ReferrerInfo
>(*doc
),
136 /* integrity = */ u
""_ns
,
137 /* nonce = */ u
""_ns
,
138 alternate
? HasAlternateRel::Yes
: HasAlternateRel::No
,
140 IsExplicitlyEnabled::No
,
144 already_AddRefed
<CharacterData
>
145 XMLStylesheetProcessingInstruction::CloneDataNode(
146 mozilla::dom::NodeInfo
* aNodeInfo
, bool aCloneText
) const {
149 RefPtr
<mozilla::dom::NodeInfo
> ni
= aNodeInfo
;
150 auto* nim
= ni
->NodeInfoManager();
151 return do_AddRef(new (nim
)
152 XMLStylesheetProcessingInstruction(ni
.forget(), data
));
156 } // namespace mozilla