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

Allow for URL query string params #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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.