Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Built with react in mind, this is a naive implementation of converting a string of css to a style object (css-stylo). This lib was created as more of a thought experiment and a chance to get my head around recursion. Never the less, it made for a pretty cool REPL, and if you did want to use it in production, I've whipped up some docs for you below.
npm install --save css-stylo
Live demo: cameronbourke.github.io/css-stylo
To build the example locally, clone this repo then run:
npm install
npm start
Then open localhost:8080 in a browser.
Simply pass a string to stylo
. Using ES6 template strings makes creating a string of css trivial. The function will look for certain css selectors in the string, at the moment only .classes
and #ids
are supported. It will then go on and create a corresponding object of each selector.
stylo(cssString, parsePixels)
// using babel and a module loader
import stylo from 'css-stylo';
const style = stylo(`
.example {
color: #34e233;
line-height: 1.4;
padding: 10px 30px 20px 0px;
border-bottom: 2px solid red;
}
`);
The returned value looks like:
{
"example": {
"color": "#34e233",
"lineHeight": 1.4,
"padding": "10 30 20 0",
"borderBottom": "2 solid red"
}
}
You can then use this object to apply inline styles to your components.
<div style={style.example}>I'm an example</div>
This can be particularly handy when working with React Native because you are able to define your styles using css's syntax. Note when using px
values, stylo
will remove it so that both React and React Native are able to interpret the value, as React handles the number 10
to equal 10px
. Remember, you can pass false as the second argument to stop stylo
from doing so.
The best part is that the styles will cascade just like they do in css, so something like this will work as expected.
// css string representation
const style = stylo(`
.first {
color: red;
font-size: 20px;
}
.first {
color: blue
}
`);
// returned value
{
"first": {
"color": "blue",
"fontSize": 20
}
}
We can even do nesting! Just like in Sass and Less.
// css string representation
const style = stylo(`
.initial {
color: orange;
.nested {
border: '1px solid grey';
.nested-again {
width: 500px
}
}
}
`);
// returned value
{
"initial": {
"color": "orange",
"nested": {
"border": "'1 solid grey'",
"nested-again": {
"width": 500
}
}
}
}
MIT Licensed Copyright (c) Cameron Bourke 2016
FAQs
'stylo takes any css string and converts it to a style object'
We found that css-stylo demonstrated a not healthy version release cadence and project activity because the last version was released 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
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.