Skip to content

Commit b63f5fa

Browse files
committed
Bug 1720570 - Break out separate method to create constructed stylesheets; r=layout-reviewers,firefox-style-system-reviewers,emilio
This breaks out the common code path between creating a CSSStyleSheet instance directly and creating one from a CSS module into a separate method so it can be used from both places. Differential Revision: https://phabricator.services.mozilla.com/D263413
1 parent bec9f73 commit b63f5fa

File tree

2 files changed

+60
-46
lines changed

2 files changed

+60
-46
lines changed

layout/style/StyleSheet.cpp

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -97,52 +97,7 @@ already_AddRefed<StyleSheet> StyleSheet::Constructor(
9797
return nullptr;
9898
}
9999

100-
// 1. Construct a sheet and set its properties (see spec).
101-
auto sheet =
102-
MakeRefPtr<StyleSheet>(css::SheetParsingMode::eAuthorSheetFeatures,
103-
CORSMode::CORS_NONE, dom::SRIMetadata());
104-
105-
// baseURL not yet in the spec. Implemented based on the following discussion:
106-
// https://github.com/WICG/construct-stylesheets/issues/95#issuecomment-594217180
107-
RefPtr<nsIURI> baseURI;
108-
if (!aOptions.mBaseURL.WasPassed()) {
109-
baseURI = constructorDocument->GetBaseURI();
110-
} else {
111-
nsresult rv = NS_NewURI(getter_AddRefs(baseURI), aOptions.mBaseURL.Value(),
112-
nullptr, constructorDocument->GetBaseURI());
113-
if (NS_FAILED(rv)) {
114-
aRv.ThrowNotAllowedError(
115-
"Constructed style sheets must have a valid base URL");
116-
return nullptr;
117-
}
118-
}
119-
120-
nsIURI* sheetURI = constructorDocument->GetDocumentURI();
121-
nsIURI* originalURI = nullptr;
122-
sheet->SetURIs(sheetURI, originalURI, baseURI);
123-
124-
sheet->SetPrincipal(constructorDocument->NodePrincipal());
125-
auto referrerInfo = MakeRefPtr<ReferrerInfo>(*constructorDocument);
126-
sheet->SetReferrerInfo(referrerInfo);
127-
sheet->mConstructorDocument = constructorDocument;
128-
129-
// 2. Set the sheet's media according to aOptions.
130-
if (aOptions.mMedia.IsUTF8String()) {
131-
sheet->SetMedia(MediaList::Create(aOptions.mMedia.GetAsUTF8String()));
132-
} else {
133-
sheet->SetMedia(aOptions.mMedia.GetAsMediaList()->Clone());
134-
}
135-
136-
// 3. Set the sheet's disabled flag according to aOptions.
137-
sheet->SetDisabled(aOptions.mDisabled);
138-
sheet->SetURLExtraData();
139-
sheet->SetComplete();
140-
141-
sheet->ReplaceSync(""_ns, aRv);
142-
MOZ_ASSERT(!aRv.Failed());
143-
144-
// 4. Return sheet.
145-
return sheet.forget();
100+
return CreateConstructedSheet(*constructorDocument, constructorDocument->GetBaseURI(), aOptions, aRv);
146101
}
147102

148103
StyleSheet::~StyleSheet() {
@@ -1196,6 +1151,60 @@ void StyleSheet::FixUpAfterInnerClone() {
11961151
}
11971152
}
11981153

1154+
/* static */
1155+
// https://wicg.github.io/construct-stylesheets/#dom-cssstylesheet-cssstylesheet
1156+
already_AddRefed<StyleSheet> StyleSheet::CreateConstructedSheet(
1157+
dom::Document& aConstructorDocument, nsIURI* aBaseURI, const dom::CSSStyleSheetInit& aOptions,
1158+
ErrorResult& aRv) {
1159+
1160+
// 1. Construct a sheet and set its properties (see spec).
1161+
auto sheet =
1162+
MakeRefPtr<StyleSheet>(css::SheetParsingMode::eAuthorSheetFeatures,
1163+
CORSMode::CORS_NONE, dom::SRIMetadata());
1164+
1165+
// baseURL not yet in the spec. Implemented based on the following discussion:
1166+
// https://github.com/WICG/construct-stylesheets/issues/95#issuecomment-594217180
1167+
RefPtr<nsIURI> baseURI;
1168+
if (!aOptions.mBaseURL.WasPassed()) {
1169+
baseURI = aBaseURI;
1170+
} else {
1171+
nsresult rv = NS_NewURI(getter_AddRefs(baseURI), aOptions.mBaseURL.Value(),
1172+
nullptr, aConstructorDocument.GetBaseURI());
1173+
if (NS_FAILED(rv)) {
1174+
aRv.ThrowNotAllowedError(
1175+
"Constructed style sheets must have a valid base URL");
1176+
return nullptr;
1177+
}
1178+
}
1179+
1180+
nsIURI* sheetURI = aConstructorDocument.GetDocumentURI();
1181+
nsIURI* originalURI = nullptr;
1182+
sheet->SetURIs(sheetURI, originalURI, baseURI);
1183+
1184+
sheet->SetPrincipal(aConstructorDocument.NodePrincipal());
1185+
auto referrerInfo = MakeRefPtr<ReferrerInfo>(aConstructorDocument);
1186+
sheet->SetReferrerInfo(referrerInfo);
1187+
sheet->mConstructorDocument = &aConstructorDocument;
1188+
1189+
// 2. Set the sheet's media according to aOptions.
1190+
if (aOptions.mMedia.IsUTF8String()) {
1191+
sheet->SetMedia(MediaList::Create(aOptions.mMedia.GetAsUTF8String()));
1192+
} else {
1193+
sheet->SetMedia(aOptions.mMedia.GetAsMediaList()->Clone());
1194+
}
1195+
1196+
// 3. Set the sheet's disabled flag according to aOptions.
1197+
sheet->SetDisabled(aOptions.mDisabled);
1198+
sheet->SetURLExtraData();
1199+
sheet->SetComplete();
1200+
1201+
sheet->ReplaceSync(""_ns, aRv);
1202+
MOZ_ASSERT(!aRv.Failed());
1203+
1204+
// 4. Return sheet.
1205+
return sheet.forget();
1206+
}
1207+
11991208
already_AddRefed<StyleSheet> StyleSheet::CreateEmptyChildSheet(
12001209
already_AddRefed<dom::MediaList> aMediaList) const {
12011210
auto child =

layout/style/StyleSheet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ class StyleSheet final : public nsICSSLoaderObserver, public nsWrapperCache {
123123
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
124124
NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(StyleSheet)
125125

126+
static already_AddRefed<StyleSheet> CreateConstructedSheet(dom::Document& aConstructorDocument,
127+
nsIURI* aBaseURI,
128+
const dom::CSSStyleSheetInit& aOptions,
129+
ErrorResult& aError);
130+
126131
already_AddRefed<StyleSheet> CreateEmptyChildSheet(
127132
already_AddRefed<dom::MediaList> aMediaList) const;
128133

0 commit comments

Comments
 (0)