MCPcopy Index your code
hub / github.com/zxlie/FeHelper / cssBeautify

Function cssBeautify

apps/code-beautify/content-script.js:10–83  ·  view source on GitHub ↗
(source_text, options)

Source from the content-addressed store, hash-verified

8 * 内联 CSS 美化器(从 beautify-worker.js 移入,避免 Worker 在内容脚本中因相对路径失败)
9 */
10 let cssBeautify = (source_text, options) => {
11 options = options || {};
12 let indentSize = options.indent_size || 4;
13 let indentCharacter = options.indent_char || ' ';
14 if (typeof indentSize === "string") indentSize = parseInt(indentSize, 10);
15
16 let whiteRe = /^\s+$/;
17 let pos = -1, ch;
18 let next = () => { ch = source_text.charAt(++pos); return ch; };
19 let peek = () => source_text.charAt(pos + 1);
20
21 let eatString = (comma) => {
22 let start = pos;
23 while (next()) {
24 if (ch === "\\") { next(); next(); }
25 else if (ch === comma) { break; }
26 else if (ch === "\n") { break; }
27 }
28 return source_text.substring(start, pos + 1);
29 };
30
31 let eatWhitespace = () => { let start = pos; while (whiteRe.test(peek())) pos++; return pos !== start; };
32 let skipWhitespace = () => { let start = pos; do {} while (whiteRe.test(next())); return pos !== start + 1; };
33
34 let eatComment = () => {
35 let start = pos; next();
36 while (next()) { if (ch === "*" && peek() === "/") { pos++; break; } }
37 return source_text.substring(start, pos + 1);
38 };
39
40 let lookBack = (str) => source_text.substring(pos - str.length, pos).toLowerCase() === str;
41
42 let indentString = source_text.match(/^[\r\n]*[\t ]*/)[0];
43 let singleIndent = Array(indentSize + 1).join(indentCharacter);
44 let indentLevel = 0;
45 let indent = () => { indentLevel++; indentString += singleIndent; };
46 let outdent = () => { indentLevel--; indentString = indentString.slice(0, -indentSize); };
47
48 let output = [];
49 let print = {};
50 print["{"] = (c) => { print.singleSpace(); output.push(c); print.newLine(); };
51 print["}"] = (c) => { print.newLine(); output.push(c); print.newLine(); };
52 print.newLine = (keepWhitespace) => {
53 if (!keepWhitespace) { while (whiteRe.test(output[output.length - 1])) output.pop(); }
54 if (output.length) output.push('\n');
55 if (indentString) output.push(indentString);
56 };
57 print.singleSpace = () => { if (output.length && !whiteRe.test(output[output.length - 1])) output.push(' '); };
58
59 if (indentString) output.push(indentString);
60
61 while (true) {
62 let isAfterSpace = skipWhitespace();
63 if (!ch) break;
64 if (ch === '{') { indent(); print["{"](ch); }
65 else if (ch === '}') { outdent(); print["}"](ch); }
66 else if (ch === '"' || ch === '\'') { output.push(eatString(ch)); }
67 else if (ch === ';') { output.push(ch, '\n', indentString); }

Callers 1

formatFunction · 0.85

Calls 9

skipWhitespaceFunction · 0.70
indentFunction · 0.70
outdentFunction · 0.70
eatStringFunction · 0.70
peekFunction · 0.70
eatCommentFunction · 0.70
lookBackFunction · 0.70
eatWhitespaceFunction · 0.70
nextFunction · 0.70

Tested by

no test coverage detected