Skip to content

Commit

Permalink
ignore urls with a fragment or query
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke committed Jul 23, 2023
1 parent 81a7701 commit a8e96f1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/parse-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function parseStyles(
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 +219,25 @@ async function loadImportContent(
)
}

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

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

0 comments on commit a8e96f1

Please sign in to comment.