Skip to content

Commit

Permalink
Allow for URL query string params
Browse files Browse the repository at this point in the history
  • Loading branch information
backflip committed Oct 10, 2023
1 parent ad46505 commit 9540ebd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .tape.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ module.exports = {
options: {
dirs: 'test/sprite-module'
}
}
},
'query': {
message: 'supports query params'
},
};
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
```pcss
.icon--square {
content: url("shared-sprites#square" param(--color blue));
/* Alternative syntax */
/* content: url("shared-sprites?--color=blue#square"); */
}
/* becomes */
Expand Down
31 changes: 26 additions & 5 deletions src/lib/transpile-decl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// native tooling
import path from 'path';
import { URLSearchParams } from 'url';

// external tooling
import parser from 'postcss-values-parser';
Expand Down Expand Up @@ -37,19 +38,25 @@ export default function transpileDecl(result, promises, decl, opts, cache) { //
// <url> split by fragment identifier symbol (#)
const urlParts = urlNode.value.split('#');

// <url> src
const src = urlParts[0];
// <url> src and query string
const [src, queryString] = urlParts[0].split('?');

// <url> fragment identifier
const id = urlParts.slice(1).join('#');

// whether the <url> has a fragment identifier
const hasID = urlParts.length > 1;

// whether the <url> has search params
const searchParams = new URLSearchParams(queryString);

// <url> param()s
const params = paramsFromNodes(
node.nodes.slice(2, -1)
);
const params = {
...paramsFromSearchParams(searchParams),
...paramsFromNodes(
node.nodes.slice(2, -1)
)
};

node.nodes.slice(2, -1).forEach(childNode => {
childNode.remove();
Expand Down Expand Up @@ -141,6 +148,20 @@ function paramsFromNodes(nodes) {
return params;
}

// params from URL search params
function paramsFromSearchParams(searchParams) {
// valid params as an object
const params = {};

// for each search param
searchParams.forEach((value, key) => {
params[key] = value;
});

// return valid params as an object
return params;
}

// whether the node is a filled param()
function isFilledParam(node) {
return node.type === 'func' && node.value === 'param' && node.nodes.length === 4 && node.nodes[1].type === 'word';
Expand Down
3 changes: 3 additions & 0 deletions test/query.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test-sprite {
background-image: url("sprite.svg?--color=blue#camera");
}
3 changes: 3 additions & 0 deletions test/query.expect.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9540ebd

Please sign in to comment.