Fix root-relative @import URLs as passed to importers (#1371)

Closes #1137
This commit is contained in:
Natalie Weizenbaum 2021-06-22 17:22:29 -07:00 committed by GitHub
parent 5a9dd9161b
commit 629881212c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 1 deletions

View File

@ -6,6 +6,10 @@
relying on this bug, but if so they can simply add `!global` to the variable
declaration to preserve the old behavior.
* **Potentially breaking bug fix:** Fix a bug where imports of root-relative
URLs (those that begin with `/`) in `@import` rules would be passed to
both Dart and JS importers as `file:` URLs.
## 1.35.1
* Fix a bug where the quiet dependency flag didn't silence warnings in some

View File

@ -1120,7 +1120,9 @@ abstract class StylesheetParser extends Parser {
String parseImportUrl(String url) {
// Backwards-compatibility for implementations that allow absolute Windows
// paths in imports.
if (p.windows.isAbsolute(url)) return p.windows.toUri(url).toString();
if (p.windows.isAbsolute(url) && !p.url.isRootRelative(url)) {
return p.windows.toUri(url).toString();
}
// Throw a [FormatException] if [url] is invalid.
Uri.parse(url);

View File

@ -93,6 +93,27 @@ void main() {
'''));
});
group("the imported URL", () {
// Regression test for #1137.
test("isn't changed if it's root-relative", () {
compileString('@import "/orange";', importers: [
TestImporter(expectAsync1((url) {
expect(url, equals(Uri.parse("/orange")));
return Uri.parse("u:$url");
}), (url) => ImporterResult('', syntax: Syntax.scss))
]);
});
test("is converted to a file: URL if it's an absolute Windows path", () {
compileString('@import "C:/orange";', importers: [
TestImporter(expectAsync1((url) {
expect(url, equals(Uri.parse("file:///C:/orange")));
return Uri.parse("u:$url");
}), (url) => ImporterResult('', syntax: Syntax.scss))
]);
});
});
test("uses an importer's source map URL", () {
late SingleMapping map;
compileString('@import "orange";',

View File

@ -364,6 +364,25 @@ void main() {
}))));
expect(result.stats.includedFiles, equals(['foo']));
});
// Regression test for #1137.
test("isn't changed if it's root-relative", () {
renderSync(RenderOptions(
data: "@import '/foo'",
importer: allowInterop(expectAsync2((url, _) {
expect(url, equals('/foo'));
return NodeImporterResult(contents: '');
}))));
});
test("is converted to a file: URL if it's an absolute Windows path", () {
renderSync(RenderOptions(
data: "@import 'C:/foo'",
importer: allowInterop(expectAsync2((url, _) {
expect(url, equals('file:///C:/foo'));
return NodeImporterResult(contents: '');
}))));
});
});
group("the previous URL", () {