Skip to content

Commit

Permalink
ignore/skip urls with a fragment or query (#539)
Browse files Browse the repository at this point in the history
* ignore urls with a fragment or query

* Apply suggestions from code review

Co-authored-by: Ryan Zimmerman <opensrc@ryanzim.com>

---------

Co-authored-by: Ryan Zimmerman <opensrc@ryanzim.com>
  • Loading branch information
romainmenke and RyanZim committed Aug 1, 2023
1 parent 96b2696 commit 6757bae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/parse-styles.js
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
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
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 6757bae

Please sign in to comment.