mirror of
https://github.com/doocs/md.git
synced 2024-10-31 00:07:50 +08:00
feat: update function: css2json
更新方法说明
This commit is contained in:
parent
d6932c0d4e
commit
87867b59ff
@ -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<br/>
|
||||
// `font: 'Times New Roman' 1em; color: #ff0000; margin-top: 1em;`<br/>
|
||||
// result in the object<br/>
|
||||
// `{"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}`<br/>
|
||||
// result in the object<br/>
|
||||
|
||||
|
||||
// 第二步:选择器处理,每个选择器会与它对应的声明相关联,如:
|
||||
// `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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user