Skip to content

Commit b459898

Browse files
committed
Update spacing for StyleJason for @media rules, and add a test for it
1 parent 2df9977 commit b459898

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

testsuite/tests/util/StyleJson.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,20 @@ describe('StyleJsonSheet object', () => {
3636
expect(styles.cssText).toBe('');
3737
});
3838

39+
test('Compound style', () => {
40+
expect(new StyleJsonSheet({
41+
'@media (prefers-color-scheme: dark)': {
42+
'mjx-container': {
43+
'color': '#E0E0E0',
44+
},
45+
}
46+
}).cssText).toBe([
47+
'@media (prefers-color-scheme: dark) {',
48+
' mjx-container {',
49+
' color: #E0E0E0;',
50+
' }',
51+
'}'
52+
].join('\n'));
53+
});
54+
3955
});

ts/util/StyleJson.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,17 @@ export class StyleJsonSheet {
100100
* @param {StyleJson} styles The style list to convert
101101
* @returns {string[]} An array of rule strings for the style list
102102
*/
103-
public getStyleRules(styles: StyleJson = this.styles): string[] {
103+
public getStyleRules(
104+
styles: StyleJson = this.styles,
105+
spaces: string = ''
106+
): string[] {
104107
const selectors = Object.keys(styles);
105108
const defs: string[] = new Array(selectors.length);
106109
let i = 0;
107110
for (const selector of selectors) {
108111
const data = styles[selector];
109-
defs[i++] = `${selector} {\n${this.getStyleDefString(data)}\n}`;
112+
defs[i++] =
113+
`${spaces}${selector} {\n${this.getStyleDefString(data, spaces)}\n${spaces}}`;
110114
}
111115
return defs;
112116
}
@@ -115,19 +119,25 @@ export class StyleJsonSheet {
115119
* @param {StyleJsonData | StyleJson} styles The style data to be stringified
116120
* @returns {string} The CSS string for the given data
117121
*/
118-
public getStyleDefString(styles: StyleJsonData | StyleJson): string {
122+
public getStyleDefString(
123+
styles: StyleJsonData | StyleJson,
124+
spaces: string
125+
): string {
119126
const properties = Object.keys(styles);
120127
const values: string[] = new Array(properties.length);
121128
let i = 0;
122129
for (const property of properties) {
123130
values[i++] =
124131
styles[property] instanceof Object
125-
? ' ' +
126-
this.getStyleRules({
127-
[property]: styles[property],
128-
} as StyleJson).join('\n ')
129-
: ' ' + property + ': ' + styles[property] + ';';
132+
? spaces +
133+
this.getStyleRules(
134+
{
135+
[property]: styles[property],
136+
} as StyleJson,
137+
spaces + ' '
138+
).join('\n' + spaces)
139+
: ' ' + spaces + property + ': ' + styles[property] + ';';
130140
}
131-
return values.join('\n');
141+
return values.join('\n' + spaces);
132142
}
133143
}

0 commit comments

Comments
 (0)