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
handling AttributeSelector
matcher
#207
Comments
For now, implemented this as an interim solution leave: function(node, item) {
switch (node.type) {
...
case 'AttributeSelector':
if (node.matcher) {
optimizedCss = optimizedCss.replace(`${node.name.name}'${node.value.value}'`,`${node.name.name}${node.matcher}'${node.value.value}'`);
}
optimizedCss += ']';
break;
}
} |
Sorry, but I didn't get your question. A |
Thanks for the reply and apologies, I should have included a better actual vs expected there. So specifically in this part of the snippet [class*="language-"] Even though the
Walking AttributeSelector with value or name => {
value: { type: 'String', loc: null, value: 'language-' },
name: { type: 'Identifier', loc: null, name: 'class' }
}
Walking Identifier with value or name => { value: undefined, name: 'class' }
Walking String with value or name => { value: 'language-', name: undefined } What is making it a little challenging to me is how to know when I can use that matcher value from
I made a demo repo you can check out to see how I am approaching it And you can load it directly in your browser in a Stackblitz too. Hope that makes sense and let me know if there's any more info I can provide! 🙏 Just to add context, although it looks like what I want is the same as the input, I'm using CSSTree to minify the input, hence the string concat in the demo, so for this example it's to remove whitespace and line breaks from the final output CSS. So starting input would be this (whitespace and line breaks) pre[class*="language-"] {
color: white;
} With the desired output (no whitespace and line breaks) pre[class*="language-"]{color: white;} |
I see. The walker's API is not intended for AST serialisation. You need to use There are several ways on how you can alter a serialisation:
import { fork } from 'css-tree';
const mySyntax = fork({
node: {
AttributeSelector: {
generate() { ... }
}
}
}); See
|
OH, haha. Yes, that's pretty convenient indeed, can't believe I missed that one 😅
This is good to know because I do a little light bundling in my more complete solution, by also inlining relative So for example this /* theme.css */
:root {
--primary-color: red;
--secondary-color: blue;
} /* main.css */
import ./theme.css
body {
color: var(--primary-color);
} Would come out as this, (plus minification) /* main.min.css */
:root {
--primary-color: red;
--secondary-color: blue;
}
body {
color: var(--primary-color);
} So seems like I would have a similar hook to do a tranformation like that? walk(ast, {
enter: function (node, item) {
const { type, name, value } = node;
if ((type === 'String' || type === 'Url') && this.atrulePrelude && this.atrule.name === 'import') {
const { value } = node;
if (value.indexOf('.') === 0) {
const location = path.resolve(path.dirname(url), value);
const importContents = fs.readFileSync(location, 'utf-8');
optimizedCss += bundleCss(importContents, url); // recursive function to keep walking all nested relative @import
} else {
optimizedCss += `@import url('${value}');`;
}
} else {
...
}
} Do you think that would still be feasible with Thanks so much for your help and for maintaining this project! ✌️ |
Hello!
I'm a little stuck on how to handle this case where an
AttributeSelector
has a matcher property.Given this rule
I would want to produce this output (basic minification).
What I'm having trouble with is figuring how the Identifier of
class
and / or the String of"language-"
would be aware of being in the context of anAttributeSelector
so I would be able to accurately add the matcher in between them?Using ASTExplorer, I would get a tree like this
If I were to log the node types of that CSS
The output I would get is the following
Since
AttributeSelector
is walked first, and that contains the value of matcher, when theIdentifier
andString
nodes are visited, how would I know when to capture the matcher and put that value in between them to achieve this?Thanks!
The text was updated successfully, but these errors were encountered: