MCPcopy Create free account
hub / github.com/CanadaHonk/shadow / CSSRule

Class CSSRule

engine/cssparser.js:40–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

38const isspace = c => c === ' ' || c === '\n';
39
40export class CSSRule {
41 static {
42 this.prototype.type = RuleType.None;
43 }
44 selectors = [];
45 properties = {};
46
47 static parseSelector(input) {
48 input = input.trim()
49 .replaceAll('\r\n', '\n'); // normalize newlines
50
51 const out = [];
52
53 for (let x of input.split(',')) {
54 x = x.trim();
55
56 let cType = CombinatorType.Descendant;
57 let sType = SelectorType.Tag;
58 let text = '';
59
60 const sels = [];
61 let conds = [];
62
63 const pushCond = () => {
64 if (text || sType === SelectorType.Universal) conds.push({ type: sType, text });
65 sType = SelectorType.Tag;
66 text = '';
67 };
68
69 const isCombinator = c => [' ', '>'].includes(c);
70
71 for (let i = 0; i < x.length; i++) {
72 const c = x[i];
73
74 if (isCombinator(c) && text && (c !== ' ' || (!isCombinator(x[i + 1]) && !isspace(x[i + 1])))) {
75 pushCond();
76
77 sels.push({ type: cType, conds });
78 conds = [];
79
80 if (c === ' ') cType = CombinatorType.Descendant;
81 if (c === '>') cType = CombinatorType.Child;
82
83 continue;
84 }
85
86 if (['#', '.', '*', ':'].includes(c) || i === x.length - 1) {
87 if (i === x.length - 1) text += c;
88
89 if (c === '*') sType = SelectorType.Universal;
90 pushCond();
91
92 if (c === '#') sType = SelectorType.Id;
93 if (c === '.') sType = SelectorType.Class;
94 if (c === ':') sType = SelectorType.Pseudo;
95
96 continue;
97 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected