@gtkx/css 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/css.js +52 -1
- package/package.json +4 -4
package/dist/css.js
CHANGED
|
@@ -1,5 +1,55 @@
|
|
|
1
1
|
import { serializeStyles } from "@emotion/serialize";
|
|
2
2
|
import { getGtkCache } from "./cache.js";
|
|
3
|
+
function expandNestedRules(styles, className) {
|
|
4
|
+
const selector = `.${className}`;
|
|
5
|
+
const expandedStyles = styles.replace(/&/g, selector);
|
|
6
|
+
const rules = [];
|
|
7
|
+
let currentRule = "";
|
|
8
|
+
let braceDepth = 0;
|
|
9
|
+
let inSelector = false;
|
|
10
|
+
let mainProperties = "";
|
|
11
|
+
for (let i = 0; i < expandedStyles.length; i++) {
|
|
12
|
+
const char = expandedStyles[i];
|
|
13
|
+
if (char === "{") {
|
|
14
|
+
braceDepth++;
|
|
15
|
+
if (braceDepth === 1 && currentRule.includes(selector)) {
|
|
16
|
+
inSelector = true;
|
|
17
|
+
currentRule += char;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
currentRule += char;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
else if (char === "}") {
|
|
24
|
+
braceDepth--;
|
|
25
|
+
currentRule += char;
|
|
26
|
+
if (braceDepth === 0 && inSelector) {
|
|
27
|
+
rules.push(currentRule.trim());
|
|
28
|
+
currentRule = "";
|
|
29
|
+
inSelector = false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else if (braceDepth === 0 && char === "." && expandedStyles.slice(i).startsWith(selector)) {
|
|
33
|
+
if (mainProperties.length > 0 || currentRule.length > 0) {
|
|
34
|
+
mainProperties += currentRule;
|
|
35
|
+
currentRule = "";
|
|
36
|
+
}
|
|
37
|
+
currentRule += char;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
currentRule += char;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (currentRule.trim()) {
|
|
44
|
+
mainProperties += currentRule;
|
|
45
|
+
}
|
|
46
|
+
const allRules = [];
|
|
47
|
+
if (mainProperties.trim()) {
|
|
48
|
+
allRules.push(`${selector}{${mainProperties.trim()}}`);
|
|
49
|
+
}
|
|
50
|
+
allRules.push(...rules);
|
|
51
|
+
return allRules.join("\n");
|
|
52
|
+
}
|
|
3
53
|
/**
|
|
4
54
|
* Creates a CSS class from styles and injects them into GTK.
|
|
5
55
|
* Works like Emotion's css function but outputs to GTK's CssProvider.
|
|
@@ -20,9 +70,10 @@ export const css = (...args) => {
|
|
|
20
70
|
const serialized = serializeStyles(args, cache.registered);
|
|
21
71
|
const className = `${cache.key}-${serialized.name}`;
|
|
22
72
|
if (cache.inserted[serialized.name] === undefined) {
|
|
23
|
-
const cssRule =
|
|
73
|
+
const cssRule = expandNestedRules(serialized.styles, className);
|
|
24
74
|
cache.sheet.insert(cssRule);
|
|
25
75
|
cache.inserted[serialized.name] = serialized.styles;
|
|
76
|
+
cache.registered[className] = serialized.styles;
|
|
26
77
|
}
|
|
27
78
|
return className;
|
|
28
79
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gtkx/css",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Emotion-style CSS-in-JS for GTKX applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"gtk",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@emotion/cache": "11.14.0",
|
|
37
|
-
"@emotion/serialize": "1.3.3",
|
|
38
|
-
"@gtkx/ffi": "0.
|
|
36
|
+
"@emotion/cache": "^11.14.0",
|
|
37
|
+
"@emotion/serialize": "^1.3.3",
|
|
38
|
+
"@gtkx/ffi": "0.9.0"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "tsc -b && cp ../../README.md .",
|