Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
als-css-parse
Advanced tools
A utility to parse CSS strings into structured JSON, preserving global comments and handling nested structures without validating CSS properties or selectors.
als-css-parse
is a JavaScript library for parsing CSS strings into a structured JSON format. This utility focuses on transforming CSS into a nested JSON object that represents the hierarchy and relationships of CSS rules. It is ideal for applications that need to programmatically analyze or manipulate stylesheets.
als-css-parse
is to take a CSS string and turn it into a structured JSON object.To install als-css-parse
, use npm:
npm install als-css-parse
als-css-parse
is also available as a bundled JavaScript file for direct use in web browsers. This allows you to leverage the CSS parsing capabilities in frontend applications.
Include the script in your HTML by referencing the bundled file located at node_modules/als-css-parse/css-parser.js
:
<script src="path/to/your/node_modules/als-css-parse/css-parser.js"></script>
Make sure the path to the css-parser.js
file matches the structure of your project.
After including the script, you can use the global cssParser
function to parse CSS strings into JSON. Simply call cssParser(cssString)
where cssString
is your CSS code. The function returns the parsed CSS as a JSON object, which you can manipulate or display as needed.
Here's how you can use als-css-parse
to convert a simple CSS string into JSON:
const cssParser = require('als-css-parse');
const cssString = `
/* Global comment */
body {
margin: 0;
padding: 0;
}
.container {
display: flex;
justify-content: center;
}
`;
const parsedCss = cssParser(cssString);
console.log(parsedCss);
This will output:
[
"/* Global comment */",
{
"body": {
"margin": "0",
"padding": "0"
}
},
{
".container": {
"display": "flex",
"justify-content": "center"
}
}
]
Let's parse a more complex CSS with nested rules and media queries:
const cssString = `
@media screen and (max-width: 600px) {
.container {
display: block;
}
.item {
padding: 10px;
}
}
`;
const parsedCss = cssParser(cssString);
console.log(parsedCss);
This will output:
[
{
"@media screen and (max-width: 600px)": [
{
".container": {
"display": "block"
}
},
{
".item": {
"padding": "10px"
}
}
]
}
]
You can remove the comments by using second parameter which is true
by default.
abbr {
color: chocolate;
}
/* some comment */
@media (hover: hover) {
/* some multiline
inner comment
*/
abbr:hover {
color: limegreen;
transition-duration: 1s;
}
}
const parsedCss = cssParser(cssString,false);
Result:
[
{
"abbr": {
"color": "chocolate"
}
},
{
"@media (hover: hover)": [
{
"abbr:hover": {
"color": "limegreen",
"transition-duration": "1s"
}
}
]
}
]
FAQs
A utility to parse CSS strings into structured JSON, preserving global comments and handling nested structures without validating CSS properties or selectors.
We found that als-css-parse demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.