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/. */
8 * A base class which implements nsIStyleSheetLinkingElement and can
9 * be subclassed by various content nodes that want to load
10 * stylesheets (<style>, <link>, processing instructions, etc).
13 #include "mozilla/dom/LinkStyle.h"
15 #include "mozilla/StyleSheet.h"
16 #include "mozilla/StyleSheetInlines.h"
17 #include "mozilla/css/Loader.h"
18 #include "mozilla/dom/Element.h"
19 #include "mozilla/dom/FragmentOrElement.h"
20 #include "mozilla/dom/HTMLLinkElement.h"
21 #include "mozilla/dom/ShadowRoot.h"
22 #include "mozilla/dom/SRILogHelper.h"
23 #include "mozilla/Preferences.h"
24 #include "mozilla/StaticPrefs_dom.h"
25 #include "nsIContent.h"
26 #include "mozilla/dom/Document.h"
27 #include "nsUnicharUtils.h"
29 #include "nsXPCOMCIDInternal.h"
30 #include "nsUnicharInputStream.h"
31 #include "nsContentUtils.h"
32 #include "nsStyleUtil.h"
33 #include "nsQueryObject.h"
35 namespace mozilla::dom
{
37 LinkStyle::SheetInfo::SheetInfo(
38 const Document
& aDocument
, nsIContent
* aContent
,
39 already_AddRefed
<nsIURI
> aURI
,
40 already_AddRefed
<nsIPrincipal
> aTriggeringPrincipal
,
41 already_AddRefed
<nsIReferrerInfo
> aReferrerInfo
,
42 mozilla::CORSMode aCORSMode
, const nsAString
& aTitle
,
43 const nsAString
& aMedia
, const nsAString
& aIntegrity
,
44 const nsAString
& aNonce
, HasAlternateRel aHasAlternateRel
,
45 IsInline aIsInline
, IsExplicitlyEnabled aIsExplicitlyEnabled
)
48 mTriggeringPrincipal(aTriggeringPrincipal
),
49 mReferrerInfo(aReferrerInfo
),
53 mIntegrity(aIntegrity
),
55 mHasAlternateRel(aHasAlternateRel
== HasAlternateRel::Yes
),
56 mIsInline(aIsInline
== IsInline::Yes
),
57 mIsExplicitlyEnabled(aIsExplicitlyEnabled
) {
58 MOZ_ASSERT(!mIsInline
|| aContent
);
59 MOZ_ASSERT_IF(aContent
, aContent
->OwnerDoc() == &aDocument
);
60 MOZ_ASSERT(mReferrerInfo
);
61 MOZ_ASSERT(mIntegrity
.IsEmpty() || !mIsInline
,
62 "Integrity only applies to <link>");
65 LinkStyle::SheetInfo::~SheetInfo() = default;
67 LinkStyle::LinkStyle()
68 : mUpdatesEnabled(true), mLineNumber(1), mColumnNumber(1) {}
70 LinkStyle::~LinkStyle() { LinkStyle::SetStyleSheet(nullptr); }
72 StyleSheet
* LinkStyle::GetSheetForBindings() const {
73 if (mStyleSheet
&& mStyleSheet
->IsComplete()) {
79 void LinkStyle::GetTitleAndMediaForElement(const Element
& aSelf
,
80 nsString
& aTitle
, nsString
& aMedia
) {
81 // Only honor title as stylesheet name for elements in the document (that is,
82 // ignore for Shadow DOM), per [1] and [2]. See [3].
84 // [1]: https://html.spec.whatwg.org/#attr-link-title
85 // [2]: https://html.spec.whatwg.org/#attr-style-title
86 // [3]: https://github.com/w3c/webcomponents/issues/535
87 if (aSelf
.IsInUncomposedDoc()) {
88 aSelf
.GetAttr(kNameSpaceID_None
, nsGkAtoms::title
, aTitle
);
89 aTitle
.CompressWhitespace();
92 aSelf
.GetAttr(kNameSpaceID_None
, nsGkAtoms::media
, aMedia
);
93 // The HTML5 spec is formulated in terms of the CSSOM spec, which specifies
94 // that media queries should be ASCII lowercased during serialization.
96 // FIXME(emilio): How does it matter? This is going to be parsed anyway, CSS
97 // should take care of serializing it properly.
98 nsContentUtils::ASCIIToLower(aMedia
);
101 bool LinkStyle::IsCSSMimeTypeAttributeForStyleElement(const Element
& aSelf
) {
103 // https://html.spec.whatwg.org/multipage/semantics.html#the-style-element:update-a-style-block
104 // step 4, for style elements we should only accept empty and "text/css" type
107 aSelf
.GetAttr(kNameSpaceID_None
, nsGkAtoms::type
, type
);
108 return type
.IsEmpty() || type
.LowerCaseEqualsLiteral("text/css");
111 void LinkStyle::Unlink() { LinkStyle::SetStyleSheet(nullptr); }
113 void LinkStyle::Traverse(nsCycleCollectionTraversalCallback
& cb
) {
114 LinkStyle
* tmp
= this;
115 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStyleSheet
);
118 void LinkStyle::SetStyleSheet(StyleSheet
* aStyleSheet
) {
120 mStyleSheet
->SetOwningNode(nullptr);
123 mStyleSheet
= aStyleSheet
;
125 mStyleSheet
->SetOwningNode(&AsContent());
129 void LinkStyle::GetCharset(nsAString
& aCharset
) { aCharset
.Truncate(); }
131 static uint32_t ToLinkMask(const nsAString
& aLink
) {
132 // Keep this in sync with sRelValues in HTMLLinkElement.cpp
133 if (aLink
.EqualsLiteral("prefetch"))
134 return LinkStyle::ePREFETCH
;
135 else if (aLink
.EqualsLiteral("dns-prefetch"))
136 return LinkStyle::eDNS_PREFETCH
;
137 else if (aLink
.EqualsLiteral("stylesheet"))
138 return LinkStyle::eSTYLESHEET
;
139 else if (aLink
.EqualsLiteral("next"))
140 return LinkStyle::eNEXT
;
141 else if (aLink
.EqualsLiteral("alternate"))
142 return LinkStyle::eALTERNATE
;
143 else if (aLink
.EqualsLiteral("preconnect"))
144 return LinkStyle::ePRECONNECT
;
145 else if (aLink
.EqualsLiteral("preload"))
146 return LinkStyle::ePRELOAD
;
151 uint32_t LinkStyle::ParseLinkTypes(const nsAString
& aTypes
) {
152 uint32_t linkMask
= 0;
153 nsAString::const_iterator start
, done
;
154 aTypes
.BeginReading(start
);
155 aTypes
.EndReading(done
);
156 if (start
== done
) return linkMask
;
158 nsAString::const_iterator
current(start
);
159 bool inString
= !nsContentUtils::IsHTMLWhitespace(*current
);
160 nsAutoString subString
;
162 while (current
!= done
) {
163 if (nsContentUtils::IsHTMLWhitespace(*current
)) {
165 nsContentUtils::ASCIIToLower(Substring(start
, current
), subString
);
166 linkMask
|= ToLinkMask(subString
);
178 nsContentUtils::ASCIIToLower(Substring(start
, current
), subString
);
179 linkMask
|= ToLinkMask(subString
);
184 Result
<LinkStyle::Update
, nsresult
> LinkStyle::UpdateStyleSheet(
185 nsICSSLoaderObserver
* aObserver
) {
186 return DoUpdateStyleSheet(nullptr, nullptr, aObserver
, ForceUpdate::No
);
189 Result
<LinkStyle::Update
, nsresult
> LinkStyle::UpdateStyleSheetInternal(
190 Document
* aOldDocument
, ShadowRoot
* aOldShadowRoot
,
191 ForceUpdate aForceUpdate
) {
192 return DoUpdateStyleSheet(aOldDocument
, aOldShadowRoot
, nullptr,
196 Result
<LinkStyle::Update
, nsresult
> LinkStyle::DoUpdateStyleSheet(
197 Document
* aOldDocument
, ShadowRoot
* aOldShadowRoot
,
198 nsICSSLoaderObserver
* aObserver
, ForceUpdate aForceUpdate
) {
199 nsIContent
& thisContent
= AsContent();
200 if (thisContent
.IsInSVGUseShadowTree()) {
201 // Stylesheets in <use>-cloned subtrees are disabled until we figure out
202 // how they should behave.
206 if (mStyleSheet
&& (aOldDocument
|| aOldShadowRoot
)) {
207 MOZ_ASSERT(!(aOldDocument
&& aOldShadowRoot
),
208 "ShadowRoot content is never in document, thus "
209 "there should not be a old document and old "
210 "ShadowRoot simultaneously.");
212 // We're removing the link element from the document or shadow tree, unload
215 // We want to do this even if updates are disabled, since otherwise a sheet
216 // with a stale linking element pointer will be hanging around -- not good!
217 if (mStyleSheet
->IsComplete()) {
218 if (aOldShadowRoot
) {
219 aOldShadowRoot
->RemoveStyleSheet(*mStyleSheet
);
221 aOldDocument
->RemoveStyleSheet(*mStyleSheet
);
225 SetStyleSheet(nullptr);
228 Document
* doc
= thisContent
.GetComposedDoc();
230 // Loader could be null during unlink, see bug 1425866.
231 if (!doc
|| !doc
->CSSLoader() || !doc
->CSSLoader()->GetEnabled()) {
235 // When static documents are created, stylesheets are cloned manually.
236 if (!mUpdatesEnabled
|| doc
->IsStaticDocument()) {
240 Maybe
<SheetInfo
> info
= GetStyleSheetInfo();
241 if (aForceUpdate
== ForceUpdate::No
&& mStyleSheet
&& info
&&
242 !info
->mIsInline
&& info
->mURI
) {
243 if (nsIURI
* oldURI
= mStyleSheet
->GetSheetURI()) {
245 nsresult rv
= oldURI
->Equals(info
->mURI
, &equal
);
246 if (NS_SUCCEEDED(rv
) && equal
) {
253 if (mStyleSheet
->IsComplete()) {
254 if (thisContent
.IsInShadowTree()) {
255 ShadowRoot
* containingShadow
= thisContent
.GetContainingShadow();
256 // Could be null only during unlink.
257 if (MOZ_LIKELY(containingShadow
)) {
258 containingShadow
->RemoveStyleSheet(*mStyleSheet
);
261 doc
->RemoveStyleSheet(*mStyleSheet
);
265 SetStyleSheet(nullptr);
272 if (!info
->mURI
&& !info
->mIsInline
) {
273 // If href is empty and this is not inline style then just bail
277 if (info
->mIsInline
) {
279 if (!nsContentUtils::GetNodeTextContent(&thisContent
, false, text
,
281 return Err(NS_ERROR_OUT_OF_MEMORY
);
284 MOZ_ASSERT(thisContent
.NodeInfo()->NameAtom() != nsGkAtoms::link
,
285 "<link> is not 'inline', and needs different CSP checks");
286 MOZ_ASSERT(thisContent
.IsElement());
288 if (!nsStyleUtil::CSPAllowsInlineStyle(
289 thisContent
.AsElement(), doc
, info
->mTriggeringPrincipal
,
290 mLineNumber
, mColumnNumber
, text
, &rv
)) {
297 // Parse the style sheet.
298 return doc
->CSSLoader()->LoadInlineStyle(*info
, text
, mLineNumber
,
301 if (thisContent
.IsElement()) {
302 nsAutoString integrity
;
303 thisContent
.AsElement()->GetAttr(kNameSpaceID_None
, nsGkAtoms::integrity
,
305 if (!integrity
.IsEmpty()) {
306 MOZ_LOG(SRILogHelper::GetSriLog(), mozilla::LogLevel::Debug
,
307 ("LinkStyle::DoUpdateStyleSheet, integrity=%s",
308 NS_ConvertUTF16toUTF8(integrity
).get()));
311 auto resultOrError
= doc
->CSSLoader()->LoadStyleLink(*info
, aObserver
);
312 if (resultOrError
.isErr()) {
313 // Don't propagate LoadStyleLink() errors further than this, since some
314 // consumers (e.g. nsXMLContentSink) will completely abort on innocuous
315 // things like a stylesheet load being blocked by the security system.
318 return resultOrError
;
321 } // namespace mozilla::dom