feat: update function: css2json

更新方法说明
This commit is contained in:
yanglbme 2019-12-06 17:08:05 +08:00
parent d6932c0d4e
commit 87867b59ff

View File

@ -27,34 +27,34 @@ function customCssWithTemplate(template) {
let customCss = customCssWithTemplate(default_theme); let customCss = customCssWithTemplate(default_theme);
// css转json
/**
* 将CSS形式的字符串转换为JSON
*
* @param {css字符串} css
*/
function css2json(css) { function css2json(css) {
// Remove all comments from the css-file // 移除CSS所有注释
while ((open = css.indexOf("/*")) !== -1 && while ((open = css.indexOf("/*")) !== -1 &&
(close = css.indexOf("*/")) !== -1) { (close = css.indexOf("*/")) !== -1) {
css = css.substring(0, open) + css.substring(close + 2); css = css.substring(0, open) + css.substring(close + 2);
} }
// Initialize the return value _json_. // 初始化返回值
let json = {}; let json = {};
// Each rule gets parsed and then removed from _css_ until all rules have been
// parsed.
while (css.length > 0) { while (css.length > 0) {
// Save the index of the first left bracket and first right bracket. // 存储第一个左/右花括号的下标
const lbracket = css.indexOf('{'); const lbracket = css.indexOf('{');
const rbracket = css.indexOf('}'); const rbracket = css.indexOf('}');
// ## Part 1: The declarations // 第一步将声明转换为Object
// // `font: 'Times New Roman' 1em; color: #ff0000; margin-top: 1em;`
// Transform the declarations to an object. For example, the declarations<br/> // ==>
// `font: 'Times New Roman' 1em; color: #ff0000; margin-top: 1em;`<br/> // `{"font": "'Times New Roman' 1em", "color": "#ff0000", "margin-top": "1em"}`
// result in the object<br/>
// `{"font": "'Times New Roman' 1em", "color": "#ff0000", "margin-top": "1em"}`. // 辅助方法将array转为object
// Helper method that transform an array to a object, by splitting each
// declaration (_font: Arial_) into key (_font_) and value(_Arial_).
function toObject(array) { function toObject(array) {
let ret = {}; let ret = {};
array.forEach(e => { array.forEach(e => {
@ -66,43 +66,41 @@ function css2json(css) {
return ret; return ret;
} }
// Split the declaration block of the first rule into an array and remove // 切割声明块并移除空白符,然后放入数组中
// whitespace from each declaration.
let declarations = css.substring(lbracket + 1, rbracket) let declarations = css.substring(lbracket + 1, rbracket)
.split(";") .split(";")
.map(e => e.trim()) .map(e => e.trim())
.filter(e => e.length > 0); // Remove any empty ("") values from the array .filter(e => e.length > 0); // 移除所有""空值
// _declaration_ is now an array reado to be transformed into an object. // 转为Object对象
declarations = toObject(declarations); declarations = toObject(declarations);
// ## Part 2: The selectors
//
// Each selector in the selectors block will be associated with the // 第二步:选择器处理,每个选择器会与它对应的声明相关联,如:
// declarations defined above. For example, `h1, p#bar {color: red}`<br/> // `h1, p#bar {color: red}`
// result in the object<br/> // ==>
// {"h1": {color: red}, "p#bar": {color: red}} // {"h1": {color: red}, "p#bar": {color: red}}
// Split the selectors block of the first rule into an array and remove
// whitespace, e.g. `"h1, p#bar, span.foo"` get parsed to
// `["h1", "p#bar", "span.foo"]`.
let selectors = css.substring(0, lbracket) let selectors = css.substring(0, lbracket)
// 以,切割,并移除空格:`"h1, p#bar, span.foo"` => ["h1", "p#bar", "span.foo"]
.split(",") .split(",")
.map(selector => selector.trim()); .map(selector => selector.trim());
// Iterate through each selector from _selectors_. // 迭代赋值
selectors.forEach(selector => { selectors.forEach(selector => {
// Initialize the json-object representing the declaration block of // 若不存在,则先初始化
// _selector_.
if (!json[selector]) json[selector] = {}; if (!json[selector]) json[selector] = {};
// Save the declarations to the right selector // 赋值到JSON
Object.keys(declarations).forEach(key => { Object.keys(declarations).forEach(key => {
json[selector][key] = declarations[key]; json[selector][key] = declarations[key];
}); });
}); });
// Continue to next instance
// 继续下个声明块
css = css.slice(rbracket + 1).trim() css = css.slice(rbracket + 1).trim()
} }
// return the json data
// 返回JSON形式的结果串
return json; return json;
} }