include/eshu_css.h

filter: Statementstmt 98.7% 163 / 165 Branchbran 78.3% 152 / 194 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.7% 315 / 359 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/eshu_js.h ↑ Summary include/eshu_pod.h »
1 /*
2 * eshu_css.h — CSS indentation scanner
3 *
4 * Brace-based nesting like C, but with CSS-specific quoting,
5 * comment syntax (no // line comments), and url() skipping.
6 */
7  
8 #ifndef ESHU_CSS_H
9 #define ESHU_CSS_H
10  
11 #include "eshu.h"
12  
13 /* ══════════════════════════════════════════════════════════════════
14 * Scanner context
15 * ══════════════════════════════════════════════════════════════════ */
16  
17 typedef struct {
18 int depth;
19 enum eshu_state state;
20 eshu_config_t cfg;
21 } eshu_css_ctx_t;
22  
23 121 static void eshu_css_ctx_init(eshu_css_ctx_t *ctx, const eshu_config_t *cfg) {
24 121 ctx->depth = 0;
25 121 ctx->state = ESHU_CODE;
26 121 ctx->cfg = *cfg;
27 121 }
28  
29 /* ══════════════════════════════════════════════════════════════════
30 * Scan a line to compute depth changes
31 *
32 * Sets *pre_adj = depth change BEFORE indenting (leading '}')
33 * Sets *post_adj = net depth change from rest of line
34 * ══════════════════════════════════════════════════════════════════ */
35  
36 979 static void eshu_css_scan_line(eshu_css_ctx_t *ctx,
37 const char *content, const char *eol,
38 int *pre_adj, int *post_adj) {
39 979 const char *p = content;
40 979 int first_close_counted = 0;
41 979 *pre_adj = 0;
42 979 *post_adj = 0;
43  
44 18026 ▶ while (p < eol) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < eol) { 17047 979 18026
45 /* ── Block comment state ── */
46 17047 ▶ if (ctx->state == ESHU_CSS_COMMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_CSS_COMMENT) { 106 16941 17047
47 106 ▶ if (p + 1 < eol && p[0] == '*' && p[1] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (p + 1 < eol && p[0] == '*' && p[1] == '/') { 104 2 106
if (p + 1 < eol && p[0] == '*' && p[1] == '/') { 9 95 104
if (p + 1 < eol && p[0] == '*' && p[1] == '/') { 7 2 9
48 7 ctx->state = ESHU_CODE;
49 7 p += 2;
50 7 continue;
51 }
52 99 p++;
53 99 continue;
54 }
55  
56 /* ── Double-quoted string ── */
57 16941 ▶ if (ctx->state == ESHU_CSS_STRING_DQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_CSS_STRING_DQ) { 173 16768 16941
58 173 ▶ if (*p == '\\' && p + 1 < eol) {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\' && p + 1 < eol) { 3 170 173
if (*p == '\\' && p + 1 < eol) { 3 0 3
59 3 p += 2; /* skip escaped char */
60 3 continue;
61 }
62 170 ▶ if (*p == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '"') { 23 147 170
63 23 ctx->state = ESHU_CODE;
64 }
65 170 p++;
66 170 continue;
67 }
68  
69 /* ── Single-quoted string ── */
70 16768 ▶ if (ctx->state == ESHU_CSS_STRING_SQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_CSS_STRING_SQ) { 25 16743 16768
71 25 ▶ if (*p == '\\' && p + 1 < eol) {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\' && p + 1 < eol) { 0 25 25
if (*p == '\\' && p + 1 < eol) { 0 0 0
72 0 p += 2;
73 0 continue;
74 }
75 25 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 5 20 25
76 5 ctx->state = ESHU_CODE;
77 }
78 25 p++;
79 25 continue;
80 }
81  
82 /* ── url() content ── */
83 16743 ▶ if (ctx->state == ESHU_CSS_URL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_CSS_URL) { 123 16620 16743
84 123 ▶ if (*p == ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == ')') { 7 116 123
85 7 ctx->state = ESHU_CODE;
86 }
87 123 p++;
88 123 continue;
89 }
90  
91 /* ── Normal CODE state ── */
92  
93 /* Block comment: */
94 16620 ▶ if (p + 1 < eol && p[0] == '/' && p[1] == '*') {
Branch: 3 branches
BranchTrueFalseTotal
if (p + 1 < eol && p[0] == '/' && p[1] == '*') { 15659 961 16620
if (p + 1 < eol && p[0] == '/' && p[1] == '*') { 10 15649 15659
if (p + 1 < eol && p[0] == '/' && p[1] == '*') { 7 3 10
95 7 ctx->state = ESHU_CSS_COMMENT;
96 7 p += 2;
97 7 continue;
98 }
99  
100 /* Double-quoted string */
101 16613 ▶ if (*p == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '"') { 24 16589 16613
102 24 ctx->state = ESHU_CSS_STRING_DQ;
103 24 p++;
104 24 continue;
105 }
106  
107 /* Single-quoted string */
108 16589 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 5 16584 16589
109 5 ctx->state = ESHU_CSS_STRING_SQ;
110 5 p++;
111 5 continue;
112 }
113  
114 /* url( — skip content until ) */
115 16584 ▶ if (p + 3 < eol && p[0] == 'u' && p[1] == 'r' && p[2] == 'l' && p[3] == '(') {
Branch: 5 branches
BranchTrueFalseTotal
if (p + 3 < eol && p[0] == 'u' && p[1] == 'r' && p[2] == 'l' && p[3] == '(') { 14180 2404 16584
if (p + 3 < eol && p[0] == 'u' && p[1] == 'r' && p[2] == 'l' && p[3] == '(') { 223 13957 14180
if (p + 3 < eol && p[0] == 'u' && p[1] == 'r' && p[2] == 'l' && p[3] == '(') { 13 210 223
if (p + 3 < eol && p[0] == 'u' && p[1] == 'r' && p[2] == 'l' && p[3] == '(') { 7 6 13
if (p + 3 < eol && p[0] == 'u' && p[1] == 'r' && p[2] == 'l' && p[3] == '(') { 7 0 7
116 7 ctx->state = ESHU_CSS_URL;
117 7 p += 4;
118 7 continue;
119 }
120  
121 /* Opening brace */
122 16577 ▶ if (*p == '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '{') { 317 16260 16577
123 317 *post_adj += 1;
124 317 p++;
125 317 continue;
126 }
127  
128 /* Closing brace */
129 16260 ▶ if (*p == '}') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '}') { 316 15944 16260
130 316 ▶ if (p == content && !first_close_counted) {
Branch: 2 branches
BranchTrueFalseTotal
if (p == content && !first_close_counted) { 222 94 316
if (p == content && !first_close_counted) { 222 0 222
131 /* Leading '}' — apply as pre-adjust */
132 222 *pre_adj -= 1;
133 222 first_close_counted = 1;
134 } else {
135 94 *post_adj -= 1;
136 }
137 316 p++;
138 316 continue;
139 }
140  
141 15944 p++;
142 }
143 979 }
144  
145 /* ══════════════════════════════════════════════════════════════════
146 * Process a single line
147 * ══════════════════════════════════════════════════════════════════ */
148  
149 1038 static void eshu_css_process_line(eshu_css_ctx_t *ctx,
150 const char *line, const char *eol,
151 eshu_buf_t *out) {
152 1038 const char *content = eshu_skip_leading_ws(line);
153 1038 int content_len = (int)(eol - content);
154 1038 ▶ int is_blank = (content == eol || *content == '\n');
Branch: 2 branches
BranchTrueFalseTotal
int is_blank = (content == eol || *content == '\n'); 979 59 1038
int is_blank = (content == eol || *content == '\n'); 0 979 979
155  
156 /* Blank line */
157 1038 ▶ if (is_blank) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_blank) { 59 979 1038
158 59 ▶ if (*eol == '\n') eshu_buf_putc(out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (*eol == '\n') eshu_buf_putc(out, '\n'); 59 0 59
159 59 return;
160 }
161  
162 /* Determine depth adjustments */
163 {
164 979 int pre_adj = 0, post_adj = 0;
165 int indent_depth;
166  
167 /* Check for leading '}' */
168 979 ▶ if (*content == '}') {
Branch: 1 branch
BranchTrueFalseTotal
if (*content == '}') { 223 756 979
169 223 pre_adj = -1;
170 }
171  
172 979 indent_depth = ctx->depth + pre_adj;
173 979 ▶ if (indent_depth < 0) indent_depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (indent_depth < 0) indent_depth = 0; 0 979 979
174  
175 /* Emit indentation + content */
176 979 eshu_emit_indent(out, indent_depth, &ctx->cfg);
177 979 eshu_buf_write_trimmed(out, content, content_len);
178 979 ▶ if (*eol == '\n') eshu_buf_putc(out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (*eol == '\n') eshu_buf_putc(out, '\n'); 979 0 979
179  
180 /* Scan line for full depth changes */
181 {
182 979 int scan_pre = 0, scan_post = 0;
183 979 eshu_css_scan_line(ctx, content, eol, &scan_pre, &scan_post);
184  
185 979 ctx->depth += scan_pre + scan_post;
186 979 ▶ if (ctx->depth < 0) ctx->depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->depth < 0) ctx->depth = 0; 0 979 979
187 }
188 }
189 }
190  
191 /* ══════════════════════════════════════════════════════════════════
192 * Public API
193 * ══════════════════════════════════════════════════════════════════ */
194  
195 121 static char * eshu_indent_css(const char *src, size_t src_len,
196 const eshu_config_t *cfg, size_t *out_len) {
197 eshu_css_ctx_t ctx;
198 eshu_buf_t out;
199 121 const char *p = src;
200 121 const char *end = src + src_len;
201 char *result;
202  
203 121 eshu_css_ctx_init(&ctx, cfg);
204 121 eshu_buf_init(&out, src_len + 256);
205  
206 1159 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 1038 121 1159
207 1038 const char *eol = eshu_find_eol(p);
208 1038 eshu_css_process_line(&ctx, p, eol, &out);
209 1038 ▶ p = (*eol == '\n') ? eol + 1 : eol;
Branch: 1 branch
BranchTrueFalseTotal
p = (*eol == '\n') ? eol + 1 : eol; 1038 0 1038
210 1038 ▶ if (p > end) p = end;
Branch: 1 branch
BranchTrueFalseTotal
if (p > end) p = end; 0 1038 1038
211 }
212  
213 121 *out_len = out.len;
214 121 result = out.data;
215 121 return result;
216 }
217  
218  
219 #include "eshu_hl_util.h"
220  
221 /* ══════════════════════════════════════════════════════════════════
222 * CSS highlighter
223 * ══════════════════════════════════════════════════════════════════ */
224  
225 /* CSS context: are we inside a rule block (between { and })? */
226 typedef enum {
227 ESHU_HL_CSS_TOP, /* top-level: selectors / at-rules */
228 ESHU_HL_CSS_RULE, /* inside {} — property: value pairs */
229 ESHU_HL_CSS_ATRULE_PAREN /* inside @media (...) */
230 } eshu_hl_css_ctx_t;
231  
232 42 static char *eshu_highlight_css(const char *src, size_t src_len, size_t *out_len) {
233 eshu_buf_t out;
234 42 const char *p = src;
235 42 const char *end = src + src_len;
236 42 const char *plain = p;
237 42 eshu_hl_css_ctx_t ctx = ESHU_HL_CSS_TOP;
238 42 int brace_depth = 0;
239  
240 42 eshu_buf_init(&out, src_len * 2 + 64);
241  
242 #define CSS_SPAN(cls, ts, te) do { \
243 eshu_hl_flush(&out, plain, (ts)); \
244 eshu_hl_span(&out, (cls), (ts), (te)); \
245 p = (te); plain = p; \
246 } while(0)
247  
248 661 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 619 42 661
249 619 char c = *p;
250  
251 /* block comment */
252 619 ▶ if (c == '/' && p + 1 < end && *(p + 1) == '*') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 4 615 619
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 4 0 4
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 4 0 4
253 4 const char *ts = p; p += 2;
254 55 ▶ while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; 55 0 55
while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; 51 4 55
while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; 0 4 4
255 4 ▶ if (p + 1 < end) p += 2;
Branch: 1 branch
BranchTrueFalseTotal
if (p + 1 < end) p += 2; 4 0 4
256 4 CSS_SPAN("esh-c", ts, p);
257 4 continue;
258 }
259  
260 /* string */
261 615 ▶ if (c == '"' || c == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '"' || c == '\'') { 612 3 615
if (c == '"' || c == '\'') { 3 609 612
262 6 const char *ts = p++;
263 43 ▶ while (p < end && *p != c) {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != c) { 43 0 43
while (p < end && *p != c) { 37 6 43
264 37 ▶ if (*p == '\\') { p++; if (p < end) p++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\') { p++; if (p < end) p++; continue; } 0 37 37
if (*p == '\\') { p++; if (p < end) p++; continue; } 0 0 0
265 37 p++;
266 }
267 6 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 6 0 6
268 6 CSS_SPAN("esh-s", ts, p);
269 6 continue;
270 }
271  
272 /* at-rule */
273 609 ▶ if (c == '@' && (p + 1 < end) && isalpha((unsigned char)*(p + 1))) {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '@' && (p + 1 < end) && isalpha((unsigned char)*(p + 1))) { 9 600 609
if (c == '@' && (p + 1 < end) && isalpha((unsigned char)*(p + 1))) { 9 0 9
if (c == '@' && (p + 1 < end) && isalpha((unsigned char)*(p + 1))) { 9 0 9
274 9 const char *ts = p++;
275 76 ▶ while (p < end && (isalnum((unsigned char)*p) || *p == '-')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (isalnum((unsigned char)*p) || *p == '-')) p++; 76 0 76
while (p < end && (isalnum((unsigned char)*p) || *p == '-')) p++; 65 11 76
while (p < end && (isalnum((unsigned char)*p) || *p == '-')) p++; 2 9 11
276 9 CSS_SPAN("esh-p", ts, p);
277 9 continue;
278 }
279  
280 /* number with optional unit */
281 600 ▶ if (isdigit((unsigned char)c) ||
Branch: 2 branches
BranchTrueFalseTotal
if (isdigit((unsigned char)c) || 575 25 600
if (isdigit((unsigned char)c) || 4 571 575
282 575 ▶ (c == '-' && p + 1 < end && isdigit((unsigned char)*(p + 1))) ||
Branch: 3 branches
BranchTrueFalseTotal
(c == '-' && p + 1 < end && isdigit((unsigned char)*(p + 1))) || 4 0 4
(c == '-' && p + 1 < end && isdigit((unsigned char)*(p + 1))) || 3 1 4
(c == '-' && p + 1 < end && isdigit((unsigned char)*(p + 1))) || 1 573 574
283 1 ▶ (c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) {
Branch: 2 branches
BranchTrueFalseTotal
(c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) { 1 0 1
(c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) { 0 1 1
284 26 const char *ts = p;
285 26 ▶ if (c == '-') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '-') p++; 1 25 26
286 69 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 69 0 69
while (p < end && isdigit((unsigned char)*p)) p++; 43 26 69
287 26 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 26 0 26
if (p < end && *p == '.') { 7 19 26
288 15 ▶ p++; while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
p++; while (p < end && isdigit((unsigned char)*p)) p++; 15 0 15
p++; while (p < end && isdigit((unsigned char)*p)) p++; 8 7 15
289 }
290 /* unit: px, em, rem, %, vw, vh, ... */
291 59 ▶ while (p < end && (isalpha((unsigned char)*p) || *p == '%')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (isalpha((unsigned char)*p) || *p == '%')) p++; 59 0 59
while (p < end && (isalpha((unsigned char)*p) || *p == '%')) p++; 31 28 59
while (p < end && (isalpha((unsigned char)*p) || *p == '%')) p++; 2 26 28
292 26 CSS_SPAN("esh-n", ts, p);
293 26 continue;
294 }
295  
296 /* color hex: #rrggbb or #rgb */
297 574 ▶ if (c == '#' && p + 1 < end && isxdigit((unsigned char)*(p + 1))) {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '#' && p + 1 < end && isxdigit((unsigned char)*(p + 1))) { 4 570 574
if (c == '#' && p + 1 < end && isxdigit((unsigned char)*(p + 1))) { 4 0 4
if (c == '#' && p + 1 < end && isxdigit((unsigned char)*(p + 1))) { 4 0 4
298 4 const char *ts = p++;
299 24 ▶ while (p < end && isxdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isxdigit((unsigned char)*p)) p++; 24 0 24
while (p < end && isxdigit((unsigned char)*p)) p++; 20 4 24
300 4 CSS_SPAN("esh-n", ts, p);
301 4 continue;
302 }
303  
304 /* track brace depth for context */
305 570 ▶ if (c == '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '{') { 36 534 570
306 36 brace_depth++;
307 36 ctx = ESHU_HL_CSS_RULE;
308 36 p++; continue;
309 }
310 534 ▶ if (c == '}') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '}') { 36 498 534
311 36 ▶ if (brace_depth > 0) brace_depth--;
Branch: 1 branch
BranchTrueFalseTotal
if (brace_depth > 0) brace_depth--; 36 0 36
312 36 ctx = (brace_depth > 0) ? ESHU_HL_CSS_RULE : ESHU_HL_CSS_TOP;
313 36 p++; continue;
314 }
315  
316 /* property name: identifier before ':' inside a rule */
317 498 ▶ if (ctx == ESHU_HL_CSS_RULE && eshu_hl_isalpha_(c)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx == ESHU_HL_CSS_RULE && eshu_hl_isalpha_(c)) { 299 199 498
if (ctx == ESHU_HL_CSS_RULE && eshu_hl_isalpha_(c)) { 55 244 299
318 55 const char *ts = p;
319 433 ▶ while (p < end && (isalnum((unsigned char)*p) || *p == '-' || *p == '_')) p++;
Branch: 4 branches
BranchTrueFalseTotal
while (p < end && (isalnum((unsigned char)*p) || *p == '-' || *p == '_')) p++; 433 0 433
while (p < end && (isalnum((unsigned char)*p) || *p == '-' || *p == '_')) p++; 364 69 433
while (p < end && (isalnum((unsigned char)*p) || *p == '-' || *p == '_')) p++; 14 55 69
while (p < end && (isalnum((unsigned char)*p) || *p == '-' || *p == '_')) p++; 0 55 55
320 /* peek for ':' after optional whitespace */
321 55 const char *peek = p;
322 57 ▶ while (peek < end && (*peek == ' ' || *peek == '\t')) peek++;
Branch: 3 branches
BranchTrueFalseTotal
while (peek < end && (*peek == ' ' || *peek == '\t')) peek++; 57 0 57
while (peek < end && (*peek == ' ' || *peek == '\t')) peek++; 2 55 57
while (peek < end && (*peek == ' ' || *peek == '\t')) peek++; 0 55 55
323 55 ▶ if (peek < end && *peek == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (peek < end && *peek == ':') { 55 0 55
if (peek < end && *peek == ':') { 40 15 55
324 40 CSS_SPAN("esh-k", ts, p);
325 } else {
326 /* plain */
327 }
328 55 continue;
329 }
330  
331 (void)ctx;
332 443 p++;
333 }
334  
335 42 eshu_hl_flush(&out, plain, end);
336 42 eshu_buf_putc(&out, '\0');
337 42 *out_len = out.len - 1;
338 42 return out.data;
339  
340 #undef CSS_SPAN
341 }
342  
343 #endif /* ESHU_CSS_H */
« include/eshu_js.h ↑ Summary include/eshu_pod.h »