├── jscss.js
├── README.md
└── LICENSE.txt
/jscss.js:
--------------------------------------------------------------------------------
1 | const style = (() => {
2 | const parseStyles = (() => {
3 | const isSelector = (() => {
4 | var re;
5 | return str => {
6 | re = re || new RegExp("^\\s{" + str.search(/\S/) + "}\\S");
7 | return re.test(str);
8 | };
9 | })();
10 | return str =>
11 | str.split(/\n/).filter(el => el).map((el, i, arr) => {
12 | if (isSelector(el))
13 | return i > 0 ? "}" + el + "{" : el + "{";
14 | return el.replace(/\b\s/, ":") + (arr[i+1] ? ";" : "}");
15 | }).join("");
16 | })();
17 | const insertStyles = str =>
18 | document.head.insertAdjacentHTML("beforeend", "");
19 | return str => insertStyles(parseStyles(str));
20 | })();
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Usage
2 |
3 | Insert **jscss.js**:
4 |
5 | ```html
6 |
7 |
8 | ```
9 | Write your styles in JavaScript using this fancy syntax:
10 | ```javascript
11 | style(`
12 |
13 | body
14 | background #ccc
15 |
16 | div
17 | width 100px
18 | height 100px
19 | background white
20 | box-shadow 0 1px 2px rgba(0, 0, 0, .5)
21 | transform translateX(50%) scale(.5)
22 |
23 | `)
24 | ```
25 |
26 | # Why
27 |
28 | Because it drives me nuts to define some styles in my CSS file and some other styles that need to be computed in my JavaScript file. Having all my declarations in one place makes me 100% happier. Not having to type curly braces, colons and semicolons isn't unpleasant either.
29 |
30 | # Performance
31 |
32 | **jscss** takes approximately `0.03s` to parse and insert 10,000 lines of CSS (Firefox 35, OS X
33 | 10.10). Good enough. And it's just 20 lines (uncompressed), so the loading should be acceptable too.
34 |
35 | # Browser support
36 |
37 | Any ES2015-friendly browser (which basically means Firefox 35 at the time of writing).
38 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Benjamin De Cock
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------