Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore/skip urls with a fragment or query #539

Merged
merged 2 commits into from
Aug 1, 2023
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.
Jump to
Jump to file
Failed to load files.
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)) {
romainmenke marked this conversation as resolved.
Show resolved Hide resolved
return false
}

// check for fragment or query
try {
romainmenke marked this conversation as resolved.
Show resolved Hide resolved
// 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