-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathStyleList.ts
127 lines (113 loc) · 3.23 KB
/
StyleList.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*************************************************************
*
* Copyright (c) 2017-2022 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Implements the CssStyles class for handling stylesheets
*
* @author dpvc@mathjax.org (Davide Cervone)
*/
/**
* The data for a selector
*/
export type StyleData = {
[property: string]: string | number;
};
/**
* A list of selectors and their data (basically a stylesheet)
*/
export type StyleList = {
[selector: string]: StyleData;
};
/******************************************************************************/
/**
* The CssStyles class (for managing a collection of CSS style definitions)
*/
export class CssStyles {
/**
* The styles as they currently stand
*/
protected styles: StyleList = {};
/**
* @return {string} The styles as a CSS string
*/
get cssText(): string {
return this.getStyleString();
}
/**
* @param {StyleList} styles The initial styles to use, if any
* @constructor
*/
constructor(styles: StyleList = null) {
this.addStyles(styles);
}
/**
* @param {StyleList} styles The styles to combine with the existing ones
*/
public addStyles(styles: StyleList) {
if (!styles) return;
for (const style of Object.keys(styles)) {
if (!this.styles[style]) {
this.styles[style] = {};
}
Object.assign(this.styles[style], styles[style]);
}
}
/**
* @param {string[]} selectors The selectors for the styles to remove
*/
public removeStyles(...selectors: string[]) {
for (const selector of selectors) {
delete this.styles[selector];
}
}
/**
* Clear all the styles
*/
public clear() {
this.styles = {};
}
/**
* @return {string} The CSS string for the style list
*/
public getStyleString(): string {
return this.getStyleRules().join('\n\n');
}
/**
* @return {string[]} An array of rule strings for the style list
*/
public getStyleRules(): string[] {
const selectors = Object.keys(this.styles);
const defs: string[] = new Array(selectors.length);
let i = 0;
for (const selector of selectors) {
defs[i++] = selector + ' {\n' + this.getStyleDefString(this.styles[selector]) + '\n}';
}
return defs;
}
/**
* @param {StyleData} styles The style data to be stringified
* @return {string} The CSS string for the given data
*/
public getStyleDefString(styles: StyleData): string {
const properties = Object.keys(styles);
const values: string[] = new Array(properties.length);
let i = 0;
for (const property of properties) {
values[i++] = ' ' + property + ': ' + styles[property] + ';';
}
return values.join('\n');
}
}