Skip to content

Commit 6be601c

Browse files
authored
Error on embedded imports in data urls (#543)
* properly ignore embedded imports in data urls * add a warning * wording * wording * cleaner solution * hard error
1 parent f99379c commit 6be601c

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

lib/parse-styles.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ async function resolveImportId(result, stmt, options, state, postcss) {
9292

9393
return
9494
} else if (dataURL.isValid(stmt.from.slice(-1))) {
95-
// Data urls can't be used a base url to resolve imports.
96-
// When the parent statement has a data url
97-
// and the current statement doesn't have a data url we ignore the statement.
98-
return
95+
// Data urls can't be used as a base url to resolve imports.
96+
throw stmt.node.error(
97+
`Unable to import '${stmt.uri}' from a stylesheet that is embedded in a data url`
98+
)
9999
}
100100

101101
const atRule = stmt.node

test/data-url.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
"use strict"
22
// external tooling
33
const test = require("ava")
4+
const postcss = require("postcss")
5+
6+
// plugin
7+
const atImport = require("..")
48

59
// internal tooling
610
const checkFixture = require("./helpers/check-fixture")
711

812
test("should inline data urls", checkFixture, "data-url")
13+
14+
test("should error on relative urls from stylesheets in data urls", t => {
15+
return postcss()
16+
.use(atImport())
17+
.process(
18+
"@import url(data:text/css;base64,QGltcG9ydCB1cmwoZm9vLmNzcyk7CgpwIHsKICBjb2xvcjogYmx1ZTsKfQo=);",
19+
{ from: undefined }
20+
)
21+
.catch(error =>
22+
t.regex(
23+
error.message,
24+
/Unable to import '(?:.*?)' from a stylesheet that is embedded in a data url/
25+
)
26+
)
27+
})

test/fixtures/data-url.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
@import url("DATA:TEXT/CSS;BASE64,QGltcG9ydCB1cmwoZGF0YTp0ZXh0L2NzcztiYXNlNjQsY0NCN0lHTnZiRzl5T2lCbmNtVmxianNnZlE9PSk7CgpwIHsgY29sb3I6IGJsdWU7IH0K") layer(foo) (min-width: 320px);
33

44
/* Mixed imports: */
5-
@import url(data:text/css;base64,QGltcG9ydCB1cmwoZm9vLmNzcyk7CgpwIHsKICBjb2xvcjogYmx1ZTsKfQo=);
65
@import url(data-url.css);
76

87
/* url encoded: */
Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1-
@import url(foo.css);p { color: green; }p { color: blue; }@media (min-width: 320px){@layer foo{
2-
p { color: green; } } }@media (min-width: 320px){@layer foo{
1+
p { color: green; }
32

4-
p { color: blue; } } }/* Mixed imports: */p {
5-
color: blue;
6-
}p { color: pink; }/* url encoded: */bar { color: green }bar { color: pink }
3+
p { color: blue; }
4+
5+
@media (min-width: 320px) {
6+
7+
@layer foo {
8+
p { color: green; } } }
9+
10+
@media (min-width: 320px) {
11+
12+
@layer foo {
13+
14+
p { color: blue; } } }
15+
16+
/* Mixed imports: */
17+
18+
p { color: pink; }
19+
20+
/* url encoded: */
21+
22+
bar { color: green }
23+
24+
bar { color: pink }

0 commit comments

Comments
 (0)