From 87867b59ff11eb3197a52849c0945ab83867bc89 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 6 Dec 2019 17:08:05 +0800 Subject: [PATCH] feat: update function: css2json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新方法说明 --- assets/scripts/util.js | 66 ++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/assets/scripts/util.js b/assets/scripts/util.js index a9eddba..04bdd0d 100644 --- a/assets/scripts/util.js +++ b/assets/scripts/util.js @@ -27,34 +27,34 @@ function customCssWithTemplate(template) { let customCss = customCssWithTemplate(default_theme); -// css转json + +/** + * 将CSS形式的字符串转换为JSON + * + * @param {css字符串} css + */ function css2json(css) { - // Remove all comments from the css-file + // 移除CSS所有注释 while ((open = css.indexOf("/*")) !== -1 && (close = css.indexOf("*/")) !== -1) { css = css.substring(0, open) + css.substring(close + 2); } - // Initialize the return value _json_. + // 初始化返回值 let json = {}; - // Each rule gets parsed and then removed from _css_ until all rules have been - // parsed. while (css.length > 0) { - // Save the index of the first left bracket and first right bracket. + // 存储第一个左/右花括号的下标 const lbracket = css.indexOf('{'); const rbracket = css.indexOf('}'); - // ## Part 1: The declarations - // - // Transform the declarations to an object. For example, the declarations
- // `font: 'Times New Roman' 1em; color: #ff0000; margin-top: 1em;`
- // result in the object
- // `{"font": "'Times New Roman' 1em", "color": "#ff0000", "margin-top": "1em"}`. - - // Helper method that transform an array to a object, by splitting each - // declaration (_font: Arial_) into key (_font_) and value(_Arial_). + // 第一步:将声明转换为Object,如: + // `font: 'Times New Roman' 1em; color: #ff0000; margin-top: 1em;` + // ==> + // `{"font": "'Times New Roman' 1em", "color": "#ff0000", "margin-top": "1em"}` + + // 辅助方法:将array转为object function toObject(array) { let ret = {}; array.forEach(e => { @@ -66,43 +66,41 @@ function css2json(css) { 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) .split(";") .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); - // ## 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}`
- // result in the object
+ + + // 第二步:选择器处理,每个选择器会与它对应的声明相关联,如: + // `h1, 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) + // 以,切割,并移除空格:`"h1, p#bar, span.foo"` => ["h1", "p#bar", "span.foo"] .split(",") .map(selector => selector.trim()); - // Iterate through each selector from _selectors_. + // 迭代赋值 selectors.forEach(selector => { - // Initialize the json-object representing the declaration block of - // _selector_. + // 若不存在,则先初始化 if (!json[selector]) json[selector] = {}; - // Save the declarations to the right selector + // 赋值到JSON Object.keys(declarations).forEach(key => { json[selector][key] = declarations[key]; }); }); - // Continue to next instance + + // 继续下个声明块 css = css.slice(rbracket + 1).trim() } - // return the json data + + // 返回JSON形式的结果串 return json; -} +} \ No newline at end of file