Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions lib/parse-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ async function parseStyles(
stmt.parentMedia = media
stmt.layer = joinLayer(layer, stmt.layer || [])

// skip protocol base uri (protocol://url) or protocol-relative
if (stmt.type !== "import" || /^(?:[a-z]+:)?\/\//i.test(stmt.uri)) {
if (stmt.type !== "import" || !isProcessableURL(stmt.uri)) {
continue
}

Expand Down Expand Up @@ -219,4 +218,27 @@ async function loadImportContent(
)
}

function isProcessableURL(uri) {
// skip protocol base uri (protocol://url) or protocol-relative
if (/^(?:[a-z]+:)?\/\//i.test(uri)) {
return false
}

// check for fragment or query
try {
// needs a base to parse properly
const url = new URL(uri, "https://example.com")

if (url.hash) {
return false
}

if (url.search) {
return false
}
} catch {} // Ignore

return true
}

module.exports = parseStyles
2 changes: 2 additions & 0 deletions test/fixtures/filter-ignore.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
@import url("//css");
@import url('//css');
@import url(//css);
@import url('foo.css?query=string');
@import url('foo.css#hash');
content{}
2 changes: 2 additions & 0 deletions test/fixtures/filter-ignore.expected.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@import url("//css");
@import url('//css');
@import url(//css);
@import url('foo.css?query=string');
@import url('foo.css#hash');
@media (min-width: 25em){
ignore{}
}
Expand Down