Skip to content

Commit 904dea7

Browse files
committed
Move stringify to a toCss util
1 parent 86792d8 commit 904dea7

File tree

6 files changed

+81
-83
lines changed

6 files changed

+81
-83
lines changed

src/css.js

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,5 @@
1-
import { $, IS_NON_DIMENSIONAL } from "./render.js";
1+
import { $ } from "./render.js";
22
import { options } from "./options.js";
3-
import { isNumber, isObject, isTagged } from "./utils.js";
4-
5-
/**
6-
* @type {{[id:string]:string}}
7-
*/
8-
const PROPS = {};
9-
10-
/**
11-
* @param {string} str
12-
*/
13-
const hyphenate = (str) =>
14-
(PROPS[str] =
15-
PROPS[str] ||
16-
str
17-
.replace(/([A-Z])/g, "-$1")
18-
.toLowerCase()
19-
.replace(/^ms-/, "-ms-"));
20-
21-
/**
22-
* @param {object} obj
23-
* @returns {string}
24-
*/
25-
function stringify(obj) {
26-
return Object.entries(obj)
27-
.map(([key, value]) => {
28-
if (isObject(value)) {
29-
return `${key}{${stringify(value)};}`.replace(/;;/g, ";");
30-
}
31-
if (key[0] === "-") {
32-
return `${key}:${value}`;
33-
}
34-
return `${hyphenate(key)}:${
35-
!isNumber(value) || IS_NON_DIMENSIONAL.test(key)
36-
? value
37-
: `${value}px`
38-
}`;
39-
})
40-
.join(";")
41-
.replace(/};/g, "}");
42-
}
433

444
/**
455
* It is used only if the browser supports adoptedStyleSheets.
@@ -50,16 +10,14 @@ const SHEETS = {};
5010

5111
/**
5212
* Create a Style from a string
53-
* @param {TemplateStringsArray|{[key:string]:import("csstype").Properties<string | number>}} template
54-
* @param {...any} args
13+
* @param {TemplateStringsArray} template
14+
* @param {...any} args
5515
*/
5616
export function css(template, ...args) {
57-
const cssText = !isTagged(template)
58-
? stringify(template)
59-
: (template.raw || template).reduce(
60-
(cssText, part, i) => cssText + part + (args[i] || ""),
61-
""
62-
);
17+
const cssText = (template.raw || template).reduce(
18+
(cssText, part, i) => cssText + part + (args[i] || ""),
19+
""
20+
);
6321
return (SHEETS[cssText] = SHEETS[cssText] || createSheet(cssText));
6422
}
6523

src/tests/css.test.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,4 @@ describe("src/css", () => {
2121
options.sheet = true;
2222
expect(styleSheet).to.an.instanceOf(HTMLStyleElement);
2323
});
24-
it("stringify", () => {
25-
const styleSheet = css({
26-
":host": {
27-
width: 696,
28-
height: 100,
29-
flex: 1,
30-
},
31-
".root": {
32-
fontSize: 12,
33-
lineHeight: 1.5,
34-
},
35-
});
36-
const cssText =
37-
"cssRules" in styleSheet
38-
? Object.values(styleSheet.cssRules).map((v) => v.cssText)
39-
: [];
40-
expect(cssText).to.eql([
41-
":host { width: 696px; height: 100px; flex: 1 1 0%; }",
42-
".root { font-size: 12px; line-height: 1.5; }",
43-
]);
44-
});
4524
});

src/utils.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ export const { isArray } = Array;
3838
*/
3939
export const isNumber = (value) => typeof value == "number";
4040

41-
/**
42-
* @param {any} value
43-
* @returns {value is TemplateStringsArray}
44-
*/
45-
export const isTagged = (value) => value && value.length && value.raw;
46-
4741
/**
4842
*
4943
* @param {Element & {dataset?:object}} node

tests/utils.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from "@esm-bundle/chai";
2-
import { serialize, checkIncompatibility } from "../utils";
2+
import { serialize, checkIncompatibility, toCss } from "../utils";
33

44
describe("utils", () => {
55
it("serialize", () => {
@@ -19,4 +19,25 @@ describe("utils", () => {
1919
expect(checkIncompatibility()).to.instanceOf(Array);
2020
expect(checkIncompatibility().length).to.equal(0);
2121
});
22+
it("toCss", () => {
23+
const styleSheet = toCss({
24+
":host": {
25+
width: 696,
26+
height: 100,
27+
flex: 1,
28+
},
29+
".root": {
30+
fontSize: 12,
31+
lineHeight: 1.5,
32+
},
33+
});
34+
const cssText =
35+
"cssRules" in styleSheet
36+
? Object.values(styleSheet.cssRules).map((v) => v.cssText)
37+
: [];
38+
expect(cssText).to.eql([
39+
":host { width: 696px; height: 100px; flex: 1 1 0%; }",
40+
".root { font-size: 12px; line-height: 1.5; }",
41+
]);
42+
});
2243
});

types/css.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import * as CSS from "csstype";
2-
31
export type Sheet = CSSStyleSheet | HTMLStyleElement;
42

53
export type Sheets = Sheet | Sheet[] | Sheets[];
6-
74
/**
85
* Create a CSSStyleSheet if the browser supports it and
96
* otherwise an HTMLStyleElement to be used as a polyfill
@@ -21,6 +18,3 @@ export type Sheets = Sheet | Sheet[] | Sheets[];
2118
* @param values
2219
*/
2320
export function css(strings: TemplateStringsArray, ...values: any[]): Sheet;
24-
export function css(strings: {
25-
[key: string]: CSS.Properties<string | number>;
26-
}): Sheet;

utils.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { css } from "./src/css.js";
2+
import { IS_NON_DIMENSIONAL } from "./src/render.js";
3+
import { isNumber, isObject } from "./src/utils.js";
4+
15
const W = globalThis;
26

37
const COMPATIBILITY_LIST = [
@@ -26,3 +30,51 @@ export const checkIncompatibility = () =>
2630
//@ts-ignore
2731
.map(([check, ctx]) => (!ctx || !(check in ctx) ? check : 0))
2832
.filter((check) => check);
33+
34+
/**
35+
* @type {{[id:string]:string}}
36+
*/
37+
const PROPS = {};
38+
39+
/**
40+
* @param {string} str
41+
*/
42+
const hyphenate = (str) =>
43+
(PROPS[str] =
44+
PROPS[str] ||
45+
str
46+
.replace(/([A-Z])/g, "-$1")
47+
.toLowerCase()
48+
.replace(/^ms-/, "-ms-"));
49+
50+
/**
51+
* @param {object} obj
52+
* @returns {string}
53+
*/
54+
const stringify = (obj) =>
55+
Object.entries(obj)
56+
.map(([key, value]) => {
57+
if (isObject(value)) {
58+
return `${key}{${stringify(value)};}`.replace(/;;/g, ";");
59+
}
60+
if (key[0] === "-") {
61+
return `${key}:${value}`;
62+
}
63+
return `${hyphenate(key)}:${
64+
!isNumber(value) || IS_NON_DIMENSIONAL.test(key)
65+
? value
66+
: `${value}px`
67+
}`;
68+
})
69+
.join(";")
70+
.replace(/};/g, "}");
71+
72+
/**
73+
* Create a Style from an object
74+
* @param {{[key:string]:import("csstype").Properties<string | number>}} obj
75+
*/
76+
export function toCss(obj) {
77+
return css`
78+
${stringify(obj)}
79+
`;
80+
}

0 commit comments

Comments
 (0)