include/eshu_xml.h

filter: Statementstmt 96.6% 400 / 414 Branchbran 76.7% 316 / 412 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.6% 716 / 826 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/eshu_c.h ↑ Summary include/eshu_js.h »
1 /*
2 * eshu_xml.h — XML/HTML indentation scanner
3 *
4 * Tracks tag nesting depth. Self-closing tags and void elements
5 * don't affect depth. Preserves content inside <pre>, <script>,
6 * <style>, CDATA sections, and comments verbatim.
7 */
8  
9 #ifndef ESHU_XML_H
10 #define ESHU_XML_H
11  
12 #include "eshu.h"
13  
14 /* ══════════════════════════════════════════════════════════════════
15 * Scanner context
16 * ══════════════════════════════════════════════════════════════════ */
17  
18 /* The open-element stack. Depth alone cannot tell a real closer from a
19 * spurious one: an unmatched </tag> must not pull later closers left, and
20 * a closer for an element further down (implied closes, e.g. <li> without
21 * </li>) should pop through to it. Names longer than the slot are
22 * truncated consistently on push and match, so they still pair up. */
23 #define ESHU_XML_STACK_MAX 256
24 #define ESHU_XML_NAME_MAX 32
25  
26 typedef struct {
27 int depth;
28 enum eshu_state state;
29 enum eshu_state saved_state; /* state before entering attr string */
30 int in_tag; /* inside < ... > spanning lines */
31 int tag_is_close;/* current multi-line tag is </... */
32 int in_script; /* collecting <script> content */
33 int script_depth;/* HTML depth for script base indent */
34 eshu_buf_t script_buf; /* collected script content */
35 eshu_config_t cfg;
36 char tag_stack[ESHU_XML_STACK_MAX][ESHU_XML_NAME_MAX];
37 int stack_len;
38 char pending_tag[ESHU_XML_NAME_MAX]; /* multi-line open */
39 int pending_tag_len;
40 } eshu_xml_ctx_t;
41  
42 197 static void eshu_xml_ctx_init(eshu_xml_ctx_t *ctx, const eshu_config_t *cfg) {
43 197 ctx->depth = 0;
44 197 ctx->state = ESHU_CODE;
45 197 ctx->saved_state = ESHU_CODE;
46 197 ctx->in_tag = 0;
47 197 ctx->tag_is_close = 0;
48 197 ctx->in_script = 0;
49 197 ctx->script_depth = 0;
50 197 ctx->script_buf.data = NULL;
51 197 ctx->script_buf.len = 0;
52 197 ctx->script_buf.cap = 0;
53 197 ctx->cfg = *cfg;
54 197 ctx->stack_len = 0;
55 197 ctx->pending_tag_len = 0;
56 197 }
57  
58 /* ══════════════════════════════════════════════════════════════════
59 * Case-insensitive prefix match
60 * ══════════════════════════════════════════════════════════════════ */
61  
62 4614 static int eshu_xml_ci_prefix(const char *str, const char *prefix, size_t plen) {
63 size_t i;
64 15536 ▶ for (i = 0; i < plen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < plen; i++) { 13717 1819 15536
65 13717 ▶ if (!str[i]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!str[i]) return 0; 0 13717 13717
66 13717 ▶ if (tolower((unsigned char)str[i]) != tolower((unsigned char)prefix[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (tolower((unsigned char)str[i]) != tolower((unsigned char)prefix[i])) 2795 10922 13717
67 2795 return 0;
68 }
69 1819 return 1;
70 }
71  
72 1204 static void eshu_xml_stack_push(eshu_xml_ctx_t *ctx,
73 const char *name, int nlen) {
74 1204 ▶ if (ctx->stack_len >= ESHU_XML_STACK_MAX) return; /* overflow: count-only */
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->stack_len >= ESHU_XML_STACK_MAX) return; /* overflow: count-only */ 0 1204 1204
75 1204 ▶ if (nlen > ESHU_XML_NAME_MAX - 1) nlen = ESHU_XML_NAME_MAX - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (nlen > ESHU_XML_NAME_MAX - 1) nlen = ESHU_XML_NAME_MAX - 1; 0 1204 1204
76 1204 memcpy(ctx->tag_stack[ctx->stack_len], name, (size_t)nlen);
77 1204 ctx->tag_stack[ctx->stack_len][nlen] = '\0';
78 1204 ctx->stack_len++;
79 }
80  
81 /* The depth adjustment a </name> earns: 0 when nothing on the stack
82 * matches (a spurious closer changes nothing), else minus the number of
83 * elements popped to reach the match (1 for the well-formed case).
84 * do_pop actually pops; the peek form drives the line's pre-indent. */
85 1781 static int eshu_xml_stack_close(eshu_xml_ctx_t *ctx,
86 const char *name, int nlen, int do_pop) {
87 1781 int i, cmp = nlen > ESHU_XML_NAME_MAX - 1 ? ESHU_XML_NAME_MAX - 1 : nlen;
88 1791 ▶ for (i = ctx->stack_len - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = ctx->stack_len - 1; i >= 0; i--) { 1789 2 1791
89 1789 const char *have = ctx->tag_stack[i];
90 1789 ▶ if ((int)strlen(have) == cmp
Branch: 1 branch
BranchTrueFalseTotal
if ((int)strlen(have) == cmp 1787 2 1789
91 1787 ▶ && eshu_xml_ci_prefix(have, name, (size_t)cmp)) {
Branch: 1 branch
BranchTrueFalseTotal
&& eshu_xml_ci_prefix(have, name, (size_t)cmp)) { 1779 8 1787
92 1779 int drop = ctx->stack_len - i;
93 1779 ▶ if (do_pop) ctx->stack_len = i;
Branch: 1 branch
BranchTrueFalseTotal
if (do_pop) ctx->stack_len = i; 1201 578 1779
94 1779 return -drop;
95 }
96 }
97 2 return 0;
98 }
99  
100 /* ══════════════════════════════════════════════════════════════════
101 * Extract tag name from position after '<' or '</'
102 * Returns length of tag name (0 if none found)
103 * ══════════════════════════════════════════════════════════════════ */
104  
105 3137 static int eshu_xml_tag_name_len(const char *p) {
106 3137 int n = 0;
107 /* tag names: [a-zA-Z][a-zA-Z0-9:_-]* */
108 3137 ▶ if (!isalpha((unsigned char)*p)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!isalpha((unsigned char)*p)) return 0; 0 3137 3137
109 16902 ▶ while (isalnum((unsigned char)p[n]) || p[n] == ':' || p[n] == '_' || p[n] == '-')
Branch: 4 branches
BranchTrueFalseTotal
while (isalnum((unsigned char)p[n]) || p[n] == ':' || p[n] == '_' || p[n] == '-') 13661 3241 16902
while (isalnum((unsigned char)p[n]) || p[n] == ':' || p[n] == '_' || p[n] == '-') 94 3147 3241
while (isalnum((unsigned char)p[n]) || p[n] == ':' || p[n] == '_' || p[n] == '-') 0 3147 3147
while (isalnum((unsigned char)p[n]) || p[n] == ':' || p[n] == '_' || p[n] == '-') 10 3137 3147
110 13765 n++;
111 3137 return n;
112 }
113  
114 /* ══════════════════════════════════════════════════════════════════
115 * HTML void elements (no closing tag needed)
116 * ══════════════════════════════════════════════════════════════════ */
117  
118 static const char *eshu_html_void_elements[] = {
119 "area", "base", "br", "col", "embed", "hr", "img", "input",
120 "link", "meta", "param", "source", "track", "wbr", NULL
121 };
122  
123 761 static int eshu_xml_is_void(const char *name, int name_len) {
124 int i;
125 10967 ▶ for (i = 0; eshu_html_void_elements[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; eshu_html_void_elements[i]; i++) { 10273 694 10967
126 10273 const char *ve = eshu_html_void_elements[i];
127 10273 int ve_len = (int)strlen(ve);
128 10273 ▶ if (ve_len == name_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (ve_len == name_len) { 1552 8721 10273
129 1552 int match = 1, j;
130 1943 ▶ for (j = 0; j < name_len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < name_len; j++) { 1876 67 1943
131 1876 ▶ if (tolower((unsigned char)name[j]) != ve[j]) {
Branch: 1 branch
BranchTrueFalseTotal
if (tolower((unsigned char)name[j]) != ve[j]) { 1485 391 1876
132 1485 match = 0;
133 1485 break;
134 }
135 }
136 1552 ▶ if (match) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (match) return 1; 67 1485 1552
137 }
138 }
139 694 return 0;
140 }
141  
142 /* ══════════════════════════════════════════════════════════════════
143 * HTML verbatim elements (<pre>, <script>, <style>)
144 * ══════════════════════════════════════════════════════════════════ */
145  
146 static const char *eshu_html_verbatim_elements[] = {
147 "pre", "script", "style", NULL
148 };
149  
150 761 static int eshu_xml_is_verbatim(const char *name, int name_len) {
151 int i;
152 3005 ▶ for (i = 0; eshu_html_verbatim_elements[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; eshu_html_verbatim_elements[i]; i++) { 2263 742 3005
153 2263 const char *ve = eshu_html_verbatim_elements[i];
154 2263 int ve_len = (int)strlen(ve);
155 2263 ▶ if (ve_len == name_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (ve_len == name_len) { 302 1961 2263
156 302 int match = 1, j;
157 410 ▶ for (j = 0; j < name_len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < name_len; j++) { 391 19 410
158 391 ▶ if (tolower((unsigned char)name[j]) != ve[j]) {
Branch: 1 branch
BranchTrueFalseTotal
if (tolower((unsigned char)name[j]) != ve[j]) { 283 108 391
159 283 match = 0;
160 283 break;
161 }
162 }
163 302 ▶ if (match) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (match) return 1; 19 283 302
164 }
165 }
166 742 return 0;
167 }
168  
169 /* verbatim_tag stores the tag we're waiting to close */
170 static char eshu_xml_verbatim_tag[32];
171 static int eshu_xml_verbatim_tag_len;
172  
173 /* A same-line close for a verbatim element: from p, find </name ws* > and
174 * return the pointer just past its '>', or NULL. Per the HTML spec the
175 * closing sequence ends the element regardless of what surrounds it, so
176 * no string-awareness is needed (or wanted). */
177 19 static const char *eshu_xml_inline_close(const char *p, const char *eol,
178 const char *name, int nlen) {
179 116 ▶ while (p + 1 < eol) {
Branch: 1 branch
BranchTrueFalseTotal
while (p + 1 < eol) { 102 14 116
180 102 ▶ if (p[0] == '<' && p[1] == '/'
Branch: 2 branches
BranchTrueFalseTotal
if (p[0] == '<' && p[1] == '/' 9 93 102
if (p[0] == '<' && p[1] == '/' 7 2 9
181 7 ▶ && eshu_xml_ci_prefix(p + 2, name, (size_t)nlen)) {
Branch: 1 branch
BranchTrueFalseTotal
&& eshu_xml_ci_prefix(p + 2, name, (size_t)nlen)) { 5 2 7
182 5 const char *after = p + 2 + nlen;
183 5 ▶ while (after < eol && (*after == ' ' || *after == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (after < eol && (*after == ' ' || *after == '\t')) 5 0 5
while (after < eol && (*after == ' ' || *after == '\t')) 0 5 5
while (after < eol && (*after == ' ' || *after == '\t')) 0 5 5
184 0 after++;
185 5 ▶ if (after < eol && *after == '>')
Branch: 2 branches
BranchTrueFalseTotal
if (after < eol && *after == '>') 5 0 5
if (after < eol && *after == '>') 5 0 5
186 5 return after + 1;
187 }
188 97 p++;
189 }
190 14 return NULL;
191 }
192  
193 /* Check if line contains the closing tag for current verbatim section */
194 46 static int eshu_xml_verbatim_end(const char *line, const char *eol) {
195 46 const char *p = line;
196 413 ▶ while (p < eol) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < eol) { 381 32 413
197 381 ▶ if (p[0] == '<' && p[1] == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (p[0] == '<' && p[1] == '/') { 16 365 381
if (p[0] == '<' && p[1] == '/') { 14 2 16
198 14 p += 2;
199 14 ▶ if (eshu_xml_ci_prefix(p, eshu_xml_verbatim_tag,
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_xml_ci_prefix(p, eshu_xml_verbatim_tag, 14 0 14
200 (size_t)eshu_xml_verbatim_tag_len)) {
201 14 const char *after = p + eshu_xml_verbatim_tag_len;
202 14 ▶ while (after < eol && (*after == ' ' || *after == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (after < eol && (*after == ' ' || *after == '\t')) 14 0 14
while (after < eol && (*after == ' ' || *after == '\t')) 0 14 14
while (after < eol && (*after == ' ' || *after == '\t')) 0 14 14
203 0 after++;
204 14 ▶ if (after < eol && *after == '>')
Branch: 2 branches
BranchTrueFalseTotal
if (after < eol && *after == '>') 14 0 14
if (after < eol && *after == '>') 14 0 14
205 14 return 1;
206 }
207 } else {
208 367 p++;
209 }
210 }
211 32 return 0;
212 }
213  
214 /* ══════════════════════════════════════════════════════════════════
215 * Scan a line for tag opens/closes to compute depth changes
216 *
217 * Returns pre_adjust (depth change to apply BEFORE indenting)
218 * and sets *post_adjust (depth change to apply AFTER indenting).
219 *
220 * Also handles state transitions for comments, CDATA, PI, DOCTYPE,
221 * and attribute strings.
222 * ══════════════════════════════════════════════════════════════════ */
223  
224 1927 static void eshu_xml_scan_line(eshu_xml_ctx_t *ctx,
225 const char *content, const char *eol,
226 int *pre_adj, int *post_adj) {
227 1927 const char *p = content;
228 1927 int is_html = (ctx->cfg.lang == ESHU_LANG_HTML);
229 1927 *pre_adj = 0;
230 1927 *post_adj = 0;
231  
232 /* If inside a multi-line tag, continue scanning for > or /> */
233 1927 ▶ if (ctx->in_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->in_tag) { 17 1910 1927
234 470 ▶ while (p < eol) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < eol) { 459 11 470
235 459 ▶ if (ctx->state == ESHU_XML_ATTR_DQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_XML_ATTR_DQ) { 308 151 459
236 308 ▶ if (*p == '"') ctx->state = ESHU_XML_TAG;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '"') ctx->state = ESHU_XML_TAG; 15 293 308
237 308 p++;
238 308 continue;
239 }
240 151 ▶ if (ctx->state == ESHU_XML_ATTR_SQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_XML_ATTR_SQ) { 0 151 151
241 0 ▶ if (*p == '\'') ctx->state = ESHU_XML_TAG;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') ctx->state = ESHU_XML_TAG; 0 0 0
242 0 p++;
243 0 continue;
244 }
245 151 ▶ if (*p == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '"') { 15 136 151
246 15 ctx->state = ESHU_XML_ATTR_DQ;
247 15 p++;
248 15 continue;
249 }
250 136 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 0 136 136
251 0 ctx->state = ESHU_XML_ATTR_SQ;
252 0 p++;
253 0 continue;
254 }
255 136 ▶ if (p + 1 < eol && p[0] == '/' && p[1] == '>') {
Branch: 3 branches
BranchTrueFalseTotal
if (p + 1 < eol && p[0] == '/' && p[1] == '>') { 131 5 136
if (p + 1 < eol && p[0] == '/' && p[1] == '>') { 1 130 131
if (p + 1 < eol && p[0] == '/' && p[1] == '>') { 1 0 1
256 /* self-closing end of multi-line tag */
257 1 ctx->in_tag = 0;
258 1 ctx->state = ESHU_CODE;
259 1 p += 2;
260 1 break;
261 }
262 135 ▶ if (*p == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '>') { 5 130 135
263 5 ctx->in_tag = 0;
264 5 ctx->state = ESHU_CODE;
265 5 ▶ if (!ctx->tag_is_close) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->tag_is_close) { 5 0 5
266 /* opening tag completed — depth++ */
267 5 *post_adj += 1;
268 5 eshu_xml_stack_push(ctx, ctx->pending_tag,
269 ctx->pending_tag_len);
270 }
271 5 p++;
272 5 break;
273 }
274 130 p++;
275 }
276 17 ▶ if (ctx->in_tag) return; /* still inside tag */
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->in_tag) return; /* still inside tag */ 11 6 17
277 }
278  
279 10006 ▶ while (p < eol) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < eol) { 8110 1896 10006
280 /* ── Comment state ── */
281 8110 ▶ if (ctx->state == ESHU_XML_COMMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_XML_COMMENT) { 119 7991 8110
282 119 ▶ if (p + 2 < eol && p[0] == '-' && p[1] == '-' && p[2] == '>') {
Branch: 4 branches
BranchTrueFalseTotal
if (p + 2 < eol && p[0] == '-' && p[1] == '-' && p[2] == '>') { 115 4 119
if (p + 2 < eol && p[0] == '-' && p[1] == '-' && p[2] == '>') { 7 108 115
if (p + 2 < eol && p[0] == '-' && p[1] == '-' && p[2] == '>') { 7 0 7
if (p + 2 < eol && p[0] == '-' && p[1] == '-' && p[2] == '>') { 7 0 7
283 7 ctx->state = ESHU_CODE;
284 7 p += 3;
285 7 continue;
286 }
287 112 p++;
288 112 continue;
289 }
290  
291 /* ── CDATA state ── */
292 7991 ▶ if (ctx->state == ESHU_XML_CDATA) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_XML_CDATA) { 159 7832 7991
293 159 ▶ if (p + 2 < eol && p[0] == ']' && p[1] == ']' && p[2] == '>') {
Branch: 4 branches
BranchTrueFalseTotal
if (p + 2 < eol && p[0] == ']' && p[1] == ']' && p[2] == '>') { 147 12 159
if (p + 2 < eol && p[0] == ']' && p[1] == ']' && p[2] == '>') { 7 140 147
if (p + 2 < eol && p[0] == ']' && p[1] == ']' && p[2] == '>') { 7 0 7
if (p + 2 < eol && p[0] == ']' && p[1] == ']' && p[2] == '>') { 7 0 7
294 7 ctx->state = ESHU_CODE;
295 7 p += 3;
296 7 continue;
297 }
298 152 p++;
299 152 continue;
300 }
301  
302 /* ── Processing instruction state ── */
303 7832 ▶ if (ctx->state == ESHU_XML_PI) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_XML_PI) { 435 7397 7832
304 435 ▶ if (p + 1 < eol && p[0] == '?' && p[1] == '>') {
Branch: 3 branches
BranchTrueFalseTotal
if (p + 1 < eol && p[0] == '?' && p[1] == '>') { 435 0 435
if (p + 1 < eol && p[0] == '?' && p[1] == '>') { 14 421 435
if (p + 1 < eol && p[0] == '?' && p[1] == '>') { 14 0 14
305 14 ctx->state = ESHU_CODE;
306 14 p += 2;
307 14 continue;
308 }
309 421 p++;
310 421 continue;
311 }
312  
313 /* ── DOCTYPE state ── */
314 7397 ▶ if (ctx->state == ESHU_XML_DOCTYPE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_XML_DOCTYPE) { 24 7373 7397
315 24 ▶ if (*p == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '>') { 4 20 24
316 4 ctx->state = ESHU_CODE;
317 4 p++;
318 4 continue;
319 }
320 20 p++;
321 20 continue;
322 }
323  
324 /* ── Normal CODE state ── */
325 7373 ▶ if (*p != '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '<') { 4783 2590 7373
326 4783 p++;
327 4783 continue;
328 }
329  
330 /* We have '<' */
331 /* Comment: <!-- */
332 2590 ▶ if (p + 3 < eol && p[1] == '!' && p[2] == '-' && p[3] == '-') {
Branch: 4 branches
BranchTrueFalseTotal
if (p + 3 < eol && p[1] == '!' && p[2] == '-' && p[3] == '-') { 2579 11 2590
if (p + 3 < eol && p[1] == '!' && p[2] == '-' && p[3] == '-') { 18 2561 2579
if (p + 3 < eol && p[1] == '!' && p[2] == '-' && p[3] == '-') { 7 11 18
if (p + 3 < eol && p[1] == '!' && p[2] == '-' && p[3] == '-') { 7 0 7
333 7 ctx->state = ESHU_XML_COMMENT;
334 7 p += 4;
335 7 continue;
336 }
337  
338 /* CDATA: <![CDATA[ */
339 2583 ▶ if (p + 8 < eol && eshu_xml_ci_prefix(p, "<![CDATA[", 9)) {
Branch: 2 branches
BranchTrueFalseTotal
if (p + 8 < eol && eshu_xml_ci_prefix(p, "<![CDATA[", 9)) { 1501 1082 2583
if (p + 8 < eol && eshu_xml_ci_prefix(p, "<![CDATA[", 9)) { 7 1494 1501
340 7 ctx->state = ESHU_XML_CDATA;
341 7 p += 9;
342 7 continue;
343 }
344  
345 /* DOCTYPE: <!DOCTYPE */
346 2576 ▶ if (p + 9 < eol && eshu_xml_ci_prefix(p, "<!DOCTYPE", 9)) {
Branch: 2 branches
BranchTrueFalseTotal
if (p + 9 < eol && eshu_xml_ci_prefix(p, "<!DOCTYPE", 9)) { 1295 1281 2576
if (p + 9 < eol && eshu_xml_ci_prefix(p, "<!DOCTYPE", 9)) { 4 1291 1295
347 4 ctx->state = ESHU_XML_DOCTYPE;
348 4 p += 9;
349 4 continue;
350 }
351  
352 /* Processing instruction: <? */
353 2572 ▶ if (p + 1 < eol && p[1] == '?') {
Branch: 2 branches
BranchTrueFalseTotal
if (p + 1 < eol && p[1] == '?') { 2572 0 2572
if (p + 1 < eol && p[1] == '?') { 14 2558 2572
354 14 ctx->state = ESHU_XML_PI;
355 14 p += 2;
356 14 continue;
357 }
358  
359 /* Closing tag: </ */
360 2558 ▶ if (p + 1 < eol && p[1] == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (p + 1 < eol && p[1] == '/') { 2558 0 2558
if (p + 1 < eol && p[1] == '/') { 1188 1370 2558
361 const char *name;
362 1188 p += 2;
363 1188 name = p;
364 1188 int nlen = eshu_xml_tag_name_len(p);
365 1188 ▶ if (nlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nlen > 0) { 1188 0 1188
366 1188 p += nlen;
367 /* Skip to > */
368 1188 ▶ while (p < eol && *p != '>') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < eol && *p != '>') p++; 1188 0 1188
while (p < eol && *p != '>') p++; 0 1188 1188
369 1188 ▶ if (p < eol) p++; /* skip > */
Branch: 1 branch
BranchTrueFalseTotal
if (p < eol) p++; /* skip > */ 1188 0 1188
370 /* Ask the stack: an unmatched closer adjusts
371 nothing, a matched one pops to its element
372 (through any implied closes). The line's
373 pre-indent already peeked the same answer for
374 a line-initial closer; the caller cancels it. */
375 1188 *post_adj += eshu_xml_stack_close(ctx, name, nlen, 1);
376 }
377 1188 continue;
378 }
379  
380 /* Opening tag: <tagname */
381 {
382 1370 const char *tag_start = p + 1;
383 1370 int nlen = eshu_xml_tag_name_len(tag_start);
384 1370 ▶ if (nlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nlen > 0) { 1370 0 1370
385 /* Check for void element in HTML mode */
386 1370 ▶ int is_void_el = is_html && eshu_xml_is_void(tag_start, nlen);
Branch: 2 branches
BranchTrueFalseTotal
int is_void_el = is_html && eshu_xml_is_void(tag_start, nlen); 761 609 1370
int is_void_el = is_html && eshu_xml_is_void(tag_start, nlen); 67 694 761
387 1370 ▶ int is_verbatim_el = is_html && eshu_xml_is_verbatim(tag_start, nlen);
Branch: 2 branches
BranchTrueFalseTotal
int is_verbatim_el = is_html && eshu_xml_is_verbatim(tag_start, nlen); 761 609 1370
int is_verbatim_el = is_html && eshu_xml_is_verbatim(tag_start, nlen); 19 742 761
388  
389 1370 p = tag_start + nlen;
390  
391 /* Scan to end of tag (> or />) handling attr strings */
392 1370 int self_closing = 0;
393 1370 int tag_complete = 0;
394 8752 ▶ while (p < eol) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < eol) { 8746 6 8752
395 8746 ▶ if (*p == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '"') { 892 7854 8746
396 892 p++;
397 7925 ▶ while (p < eol && *p != '"') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < eol && *p != '"') p++; 7925 0 7925
while (p < eol && *p != '"') p++; 7033 892 7925
398 892 ▶ if (p < eol) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < eol) p++; 892 0 892
399 892 continue;
400 }
401 7854 ▶ if (*p == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { 2 7852 7854
402 2 p++;
403 4 ▶ while (p < eol && *p != '\'') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < eol && *p != '\'') p++; 4 0 4
while (p < eol && *p != '\'') p++; 2 2 4
404 2 ▶ if (p < eol) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < eol) p++; 2 0 2
405 2 continue;
406 }
407 7852 ▶ if (p + 1 < eol && p[0] == '/' && p[1] == '>') {
Branch: 3 branches
BranchTrueFalseTotal
if (p + 1 < eol && p[0] == '/' && p[1] == '>') { 7211 641 7852
if (p + 1 < eol && p[0] == '/' && p[1] == '>') { 93 7118 7211
if (p + 1 < eol && p[0] == '/' && p[1] == '>') { 93 0 93
408 93 self_closing = 1;
409 93 tag_complete = 1;
410 93 p += 2;
411 93 break;
412 }
413 7759 ▶ if (*p == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '>') { 1271 6488 7759
414 1271 tag_complete = 1;
415 1271 p++;
416 1271 break;
417 }
418 6488 p++;
419 }
420  
421 1370 ▶ if (!tag_complete) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tag_complete) { 6 1364 1370
422 /* Tag spans multiple lines */
423 6 ctx->in_tag = 1;
424 6 ctx->tag_is_close = 0;
425 6 ctx->state = ESHU_XML_TAG;
426 /* Remember the name for the push when the
427 '>' lands on a later line */
428 6 ctx->pending_tag_len =
429 nlen > ESHU_XML_NAME_MAX - 1
430 6 ? ESHU_XML_NAME_MAX - 1 : nlen;
431 6 memcpy(ctx->pending_tag, tag_start,
432 6 (size_t)ctx->pending_tag_len);
433 6 ctx->pending_tag[ctx->pending_tag_len] = '\0';
434 /* Don't increment depth yet — wait for > */
435 6 return;
436 }
437  
438 1364 ▶ if (!self_closing && !is_void_el) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self_closing && !is_void_el) { 1271 93 1364
if (!self_closing && !is_void_el) { 1204 67 1271
439 1204 ▶ if (is_verbatim_el) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_verbatim_el) { 19 1185 1204
440 /* Closed on the same line? Then this is an
441 inline pair - no state, no net depth; keep
442 scanning the rest of the line rather than
443 swallowing it into the verbatim buffer. */
444 19 const char *past = eshu_xml_inline_close(
445 p, eol, tag_start, nlen);
446 19 ▶ if (past) {
Branch: 1 branch
BranchTrueFalseTotal
if (past) { 5 14 19
447 5 p = past;
448 5 continue;
449 }
450 /* Enter verbatim — depth++ for the open tag */
451 14 *post_adj += 1;
452 14 eshu_xml_stack_push(ctx, tag_start, nlen);
453 14 ctx->state = ESHU_XML_VERBATIM;
454 /* Store verbatim tag name */
455 14 eshu_xml_verbatim_tag_len = nlen > 31 ? 31 : nlen;
456 14 memcpy(eshu_xml_verbatim_tag, tag_start,
457 (size_t)eshu_xml_verbatim_tag_len);
458 14 eshu_xml_verbatim_tag[eshu_xml_verbatim_tag_len] = '\0';
459 /* If HTML <script>, set up JS collection */
460 24 ▶ if (is_html && nlen == 6 &&
Branch: 1 branch
BranchTrueFalseTotal
if (is_html && nlen == 6 && 14 0 14
461 10 eshu_xml_ci_prefix(tag_start, "script", 6)) {
462 10 ctx->in_script = 1;
463 10 ctx->script_depth = ctx->depth + *post_adj;
464 10 eshu_buf_init(&ctx->script_buf, 1024);
465 } else {
466 4 ctx->in_script = 0;
467 }
468 14 return;
469 }
470 1185 *post_adj += 1;
471 1185 eshu_xml_stack_push(ctx, tag_start, nlen);
472 }
473 1345 continue;
474 }
475 }
476  
477 0 p++;
478 }
479 }
480  
481 /* ══════════════════════════════════════════════════════════════════
482 * Determine pre-indent adjustment for closing tags at line start
483 * ══════════════════════════════════════════════════════════════════ */
484  
485 /* A line-initial closer indents at the depth of the element it closes: a
486 * peek at the stack, so a spurious closer (no match) stays where it is
487 * and an implied-close pop lands on the right column. */
488 1898 static int eshu_xml_line_pre_adjust(eshu_xml_ctx_t *ctx,
489 const char *content, const char *eol) {
490 1898 ▶ if (content < eol && content[0] == '<'
Branch: 2 branches
BranchTrueFalseTotal
if (content < eol && content[0] == '<' 1898 0 1898
if (content < eol && content[0] == '<' 1891 7 1898
491 1891 ▶ && content + 1 < eol && content[1] == '/') {
Branch: 2 branches
BranchTrueFalseTotal
&& content + 1 < eol && content[1] == '/') { 1891 0 1891
&& content + 1 < eol && content[1] == '/') { 579 1312 1891
492 579 int nlen = eshu_xml_tag_name_len(content + 2);
493 579 ▶ if (nlen > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (nlen > 0) 579 0 579
494 579 return eshu_xml_stack_close(ctx, content + 2, nlen, 0);
495 }
496 1319 return 0;
497 }
498  
499 /* Pre-adjust for multi-line tag continuation: '>' or '/>' first */
500 0 static int eshu_xml_multiline_tag_closing(eshu_xml_ctx_t *ctx,
501 const char *content,
502 const char *eol) {
503 (void)eol;
504 0 ▶ if (ctx->in_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->in_tag) { 0 0 0
505 /* continuation lines of multi-line tags indent at depth+1 */
506 0 return 0;
507 }
508 (void)content;
509 0 return 0;
510 }
511  
512 /* ══════════════════════════════════════════════════════════════════
513 * Process a single line
514 * ══════════════════════════════════════════════════════════════════ */
515  
516 1975 static void eshu_xml_process_line(eshu_xml_ctx_t *ctx,
517 const char *line, const char *eol,
518 eshu_buf_t *out) {
519 1975 const char *content = eshu_skip_leading_ws(line);
520 1975 int content_len = (int)(eol - content);
521 1975 ▶ int is_blank = (content == eol || *content == '\n');
Branch: 2 branches
BranchTrueFalseTotal
int is_blank = (content == eol || *content == '\n'); 1973 2 1975
int is_blank = (content == eol || *content == '\n'); 0 1973 1973
522  
523 /* ── Verbatim state: pass through until closing tag ── */
524 1975 ▶ if (ctx->state == ESHU_XML_VERBATIM) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_XML_VERBATIM) { 46 1929 1975
525 46 ▶ if (eshu_xml_verbatim_end(content, eol)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_xml_verbatim_end(content, eol)) { 14 32 46
526 /* Closing tag of verbatim section */
527 14 ▶ if (ctx->in_script && ctx->script_buf.data) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->in_script && ctx->script_buf.data) { 10 4 14
if (ctx->in_script && ctx->script_buf.data) { 10 0 10
528 /* Run collected script through JS indenter */
529 10 eshu_config_t js_cfg = ctx->cfg;
530 10 js_cfg.lang = ESHU_LANG_JS;
531 10 js_cfg.range_start = 0;
532 10 js_cfg.range_end = 0;
533 {
534 size_t js_out_len;
535 char *js_result;
536 const char *jp;
537 const char *je;
538 /* NUL-terminate script buffer */
539 10 eshu_buf_putc(&ctx->script_buf, '\0');
540 10 ctx->script_buf.len--;
541  
542 10 js_result = eshu_indent_js(
543 10 ctx->script_buf.data, ctx->script_buf.len,
544 &js_cfg, &js_out_len);
545  
546 /* Emit each JS output line with base indent */
547 10 jp = js_result;
548 10 je = js_result + js_out_len;
549 35 ▶ while (jp < je) {
Branch: 1 branch
BranchTrueFalseTotal
while (jp < je) { 25 10 35
550 25 const char *jnl = eshu_find_eol(jp);
551 25 const char *jc = eshu_skip_leading_ws(jp);
552 25 ▶ if (jc < jnl) {
Branch: 1 branch
BranchTrueFalseTotal
if (jc < jnl) { 25 0 25
553 /* Non-empty: base indent + JS line (with JS indent) */
554 25 eshu_emit_indent(out, ctx->script_depth, &ctx->cfg);
555 25 eshu_buf_write_trimmed(out, jp, (int)(jnl - jp));
556 }
557 25 eshu_buf_putc(out, '\n');
558 25 ▶ jp = (*jnl == '\n') ? jnl + 1 : jnl;
Branch: 1 branch
BranchTrueFalseTotal
jp = (*jnl == '\n') ? jnl + 1 : jnl; 25 0 25
559 25 ▶ if (jp > je) jp = je;
Branch: 1 branch
BranchTrueFalseTotal
if (jp > je) jp = je; 0 25 25
560 }
561  
562 10 free(js_result);
563 }
564 10 eshu_buf_free(&ctx->script_buf);
565 10 ctx->in_script = 0;
566 }
567  
568 { /* pop the verbatim element off the stack too */
569 14 int adj = eshu_xml_stack_close(ctx,
570 eshu_xml_verbatim_tag,
571 eshu_xml_verbatim_tag_len, 1);
572 14 ▶ ctx->depth += adj ? adj : -1;
Branch: 1 branch
BranchTrueFalseTotal
ctx->depth += adj ? adj : -1; 14 0 14
573 }
574 14 ▶ if (ctx->depth < 0) ctx->depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->depth < 0) ctx->depth = 0; 0 14 14
575 14 ctx->state = ESHU_CODE;
576 14 eshu_emit_indent(out, ctx->depth, &ctx->cfg);
577 14 eshu_buf_write_trimmed(out, content, content_len);
578 14 ▶ if (*eol == '\n') eshu_buf_putc(out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (*eol == '\n') eshu_buf_putc(out, '\n'); 14 0 14
579 32 ▶ } else if (ctx->in_script) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ctx->in_script) { 25 7 32
580 /* Collect script content for later JS indentation */
581 25 eshu_buf_write(&ctx->script_buf, content, (size_t)content_len);
582 25 eshu_buf_putc(&ctx->script_buf, '\n');
583 } else {
584 /* Pass through verbatim content unchanged (pre, style) */
585 7 eshu_buf_write_trimmed(out, line, (int)(eol - line));
586 7 ▶ if (*eol == '\n') eshu_buf_putc(out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (*eol == '\n') eshu_buf_putc(out, '\n'); 7 0 7
587 }
588 46 return;
589 }
590  
591 /* ── Blank line ── */
592 1929 ▶ if (is_blank) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_blank) { 2 1927 1929
593 2 ▶ if (*eol == '\n') eshu_buf_putc(out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (*eol == '\n') eshu_buf_putc(out, '\n'); 2 0 2
594 2 return;
595 }
596  
597 /* ── Multi-line comment/CDATA/PI/DOCTYPE: indent and scan for end ── */
598 1927 ▶ if (ctx->state == ESHU_XML_COMMENT || ctx->state == ESHU_XML_CDATA ||
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->state == ESHU_XML_COMMENT || ctx->state == ESHU_XML_CDATA || 1924 3 1927
if (ctx->state == ESHU_XML_COMMENT || ctx->state == ESHU_XML_CDATA || 1915 9 1924
599 1915 ▶ ctx->state == ESHU_XML_PI || ctx->state == ESHU_XML_DOCTYPE) {
Branch: 2 branches
BranchTrueFalseTotal
ctx->state == ESHU_XML_PI || ctx->state == ESHU_XML_DOCTYPE) { 1915 0 1915
ctx->state == ESHU_XML_PI || ctx->state == ESHU_XML_DOCTYPE) { 0 1915 1915
600 12 int scan_pre = 0, scan_post = 0;
601 12 eshu_emit_indent(out, ctx->depth, &ctx->cfg);
602 12 eshu_buf_write_trimmed(out, content, content_len);
603 12 ▶ if (*eol == '\n') eshu_buf_putc(out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (*eol == '\n') eshu_buf_putc(out, '\n'); 12 0 12
604 12 eshu_xml_scan_line(ctx, content, eol, &scan_pre, &scan_post);
605 12 ctx->depth += scan_post;
606 12 ▶ if (ctx->depth < 0) ctx->depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->depth < 0) ctx->depth = 0; 0 12 12
607 12 return;
608 }
609  
610 /* ── Multi-line tag continuation ── */
611 1915 ▶ if (ctx->in_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->in_tag) { 17 1898 1915
612 17 int scan_pre = 0, scan_post = 0;
613 17 eshu_emit_indent(out, ctx->depth + 1, &ctx->cfg);
614 17 eshu_buf_write_trimmed(out, content, content_len);
615 17 ▶ if (*eol == '\n') eshu_buf_putc(out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (*eol == '\n') eshu_buf_putc(out, '\n'); 17 0 17
616 17 eshu_xml_scan_line(ctx, content, eol, &scan_pre, &scan_post);
617 17 ctx->depth += scan_post;
618 17 ▶ if (ctx->depth < 0) ctx->depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->depth < 0) ctx->depth = 0; 0 17 17
619 17 return;
620 }
621  
622 /* ── Normal line ── */
623 {
624 1898 int line_pre, scan_pre = 0, scan_post = 0;
625  
626 /* Check if line starts with closing tag (a stack peek) */
627 1898 line_pre = eshu_xml_line_pre_adjust(ctx, content, eol);
628  
629 /* Indent */
630 1898 eshu_emit_indent(out, ctx->depth + line_pre, &ctx->cfg);
631 1898 eshu_buf_write_trimmed(out, content, content_len);
632 1898 ▶ if (*eol == '\n') eshu_buf_putc(out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (*eol == '\n') eshu_buf_putc(out, '\n'); 1898 0 1898
633  
634 /* Scan line for depth changes */
635 1898 eshu_xml_scan_line(ctx, content, eol, &scan_pre, &scan_post);
636  
637 /* Cancel double-count: the first closing tag on the line
638 * was already applied via line_pre, and scan_line's pop of
639 * the same closer produced the same adjustment. */
640 1898 scan_post -= line_pre;
641  
642 1898 ctx->depth += line_pre + scan_post;
643 1898 ▶ if (ctx->depth < 0) ctx->depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->depth < 0) ctx->depth = 0; 0 1898 1898
644 }
645 }
646  
647 /* ══════════════════════════════════════════════════════════════════
648 * Public API
649 * ══════════════════════════════════════════════════════════════════ */
650  
651 197 static char * eshu_indent_xml(const char *src, size_t src_len,
652 const eshu_config_t *cfg, size_t *out_len) {
653 eshu_xml_ctx_t ctx;
654 eshu_buf_t out;
655 197 const char *p = src;
656 197 const char *end = src + src_len;
657 char *result;
658  
659 197 eshu_xml_ctx_init(&ctx, cfg);
660 197 eshu_buf_init(&out, src_len + 256);
661  
662 2172 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 1975 197 2172
663 1975 const char *eol = eshu_find_eol(p);
664 1975 eshu_xml_process_line(&ctx, p, eol, &out);
665 1975 ▶ p = (*eol == '\n') ? eol + 1 : eol;
Branch: 1 branch
BranchTrueFalseTotal
p = (*eol == '\n') ? eol + 1 : eol; 1975 0 1975
666 1975 ▶ if (p > end) p = end;
Branch: 1 branch
BranchTrueFalseTotal
if (p > end) p = end; 0 1975 1975
667 }
668  
669 /* Clean up script buffer if still allocated (e.g. unclosed <script>) */
670 197 ▶ if (ctx.script_buf.data)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.script_buf.data) 0 197 197
671 0 eshu_buf_free(&ctx.script_buf);
672  
673 197 *out_len = out.len;
674 197 result = out.data;
675 197 return result;
676 }
677  
678  
679 #include "eshu_hl_util.h"
680  
681 /* ══════════════════════════════════════════════════════════════════
682 * XML / HTML highlighter
683 * ══════════════════════════════════════════════════════════════════ */
684  
685 typedef enum {
686 XHL_TEXT, /* text content */
687 XHL_TAG, /* inside <...> after tag name */
688 XHL_ATTR_DQ, /* inside attribute value "..." */
689 XHL_ATTR_SQ /* inside attribute value '...' */
690 } eshu_xhl_state_t;
691  
692 40 static char *eshu_highlight_xml(const char *src, size_t src_len, size_t *out_len) {
693 eshu_buf_t out;
694 40 const char *p = src;
695 40 const char *end = src + src_len;
696 40 const char *plain = p;
697  
698 40 eshu_buf_init(&out, src_len * 2 + 64);
699  
700 #define X_SPAN(cls, ts, te) do { \
701 eshu_hl_flush(&out, plain, (ts)); \
702 eshu_hl_span(&out, (cls), (ts), (te)); \
703 p = (te); plain = p; \
704 } while(0)
705  
706 210 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 170 40 210
707 170 char c = *p;
708  
709 /* XML / HTML comment: <!-- ... --> */
710 170 ▶ if (c == '<' && p + 3 < end && *(p+1) == '!' &&
Branch: 3 branches
BranchTrueFalseTotal
if (c == '<' && p + 3 < end && *(p+1) == '!' && 62 108 170
if (c == '<' && p + 3 < end && *(p+1) == '!' && 62 0 62
if (c == '<' && p + 3 < end && *(p+1) == '!' && 10 52 62
711 10 ▶ *(p+2) == '-' && *(p+3) == '-') {
Branch: 2 branches
BranchTrueFalseTotal
*(p+2) == '-' && *(p+3) == '-') { 6 4 10
*(p+2) == '-' && *(p+3) == '-') { 6 0 6
712 6 const char *ts = p; p += 4;
713 87 ▶ while (p + 2 < end && !(*(p)=='-' && *(p+1)=='-' && *(p+2)=='>')) p++;
Branch: 4 branches
BranchTrueFalseTotal
while (p + 2 < end && !(*(p)=='-' && *(p+1)=='-' && *(p+2)=='>')) p++; 87 0 87
while (p + 2 < end && !(*(p)=='-' && *(p+1)=='-' && *(p+2)=='>')) p++; 81 6 87
while (p + 2 < end && !(*(p)=='-' && *(p+1)=='-' && *(p+2)=='>')) p++; 0 6 6
while (p + 2 < end && !(*(p)=='-' && *(p+1)=='-' && *(p+2)=='>')) p++; 0 6 6
714 6 ▶ if (p + 2 < end) p += 3;
Branch: 1 branch
BranchTrueFalseTotal
if (p + 2 < end) p += 3; 6 0 6
715 6 X_SPAN("esh-c", ts, p);
716 6 continue;
717 }
718  
719 /* CDATA */
720 164 ▶ if (c == '<' && p + 8 < end && memcmp(p, "<![CDATA[", 9) == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '<' && p + 8 < end && memcmp(p, "<![CDATA[", 9) == 0) { 56 108 164
if (c == '<' && p + 8 < end && memcmp(p, "<![CDATA[", 9) == 0) { 42 14 56
if (c == '<' && p + 8 < end && memcmp(p, "<![CDATA[", 9) == 0) { 2 40 42
721 2 const char *ts = p; p += 9;
722 42 ▶ while (p + 2 < end && !(*(p)==']' && *(p+1)==']' && *(p+2)=='>')) p++;
Branch: 4 branches
BranchTrueFalseTotal
while (p + 2 < end && !(*(p)==']' && *(p+1)==']' && *(p+2)=='>')) p++; 42 0 42
while (p + 2 < end && !(*(p)==']' && *(p+1)==']' && *(p+2)=='>')) p++; 40 2 42
while (p + 2 < end && !(*(p)==']' && *(p+1)==']' && *(p+2)=='>')) p++; 0 2 2
while (p + 2 < end && !(*(p)==']' && *(p+1)==']' && *(p+2)=='>')) p++; 0 2 2
723 2 ▶ if (p + 2 < end) p += 3;
Branch: 1 branch
BranchTrueFalseTotal
if (p + 2 < end) p += 3; 2 0 2
724 2 X_SPAN("esh-s", ts, p);
725 2 continue;
726 }
727  
728 /* doctype */
729 162 ▶ if (c == '<' && p + 1 < end && *(p+1) == '!') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '<' && p + 1 < end && *(p+1) == '!') { 54 108 162
if (c == '<' && p + 1 < end && *(p+1) == '!') { 54 0 54
if (c == '<' && p + 1 < end && *(p+1) == '!') { 2 52 54
730 2 const char *ts = p;
731 30 ▶ while (p < end && *p != '>') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '>') p++; 30 0 30
while (p < end && *p != '>') p++; 28 2 30
732 2 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 2 0 2
733 2 X_SPAN("esh-p", ts, p);
734 2 continue;
735 }
736  
737 /* opening or closing tag */
738 160 ▶ if (c == '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '<') { 52 108 160
739 52 const char *ts = p++;
740 /* skip '/' for closing tags */
741 52 ▶ if (p < end && *p == '/') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '/') p++; 52 0 52
if (p < end && *p == '/') p++; 14 38 52
742 /* tag name */
743 52 const char *tag_start = p;
744 224 ▶ while (p < end && (isalnum((unsigned char)*p) || *p == '-' ||
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (isalnum((unsigned char)*p) || *p == '-' || 224 0 224
while (p < end && (isalnum((unsigned char)*p) || *p == '-' || 170 54 224
while (p < end && (isalnum((unsigned char)*p) || *p == '-' || 0 54 54
745 224 ▶ *p == ':' || *p == '_' || *p == '.')) p++;
Branch: 3 branches
BranchTrueFalseTotal
*p == ':' || *p == '_' || *p == '.')) p++; 2 52 54
*p == ':' || *p == '_' || *p == '.')) p++; 0 52 52
*p == ':' || *p == '_' || *p == '.')) p++; 0 52 52
746 /* emit '<' or '</' as plain, then tag name as esh-g */
747 52 eshu_hl_flush(&out, plain, ts);
748 52 plain = p;
749 /* emit the '<[/]' literal */
750 52 eshu_hl_write_html(&out, ts, (size_t)(tag_start - ts));
751 /* tag name span */
752 52 ▶ if (p > tag_start)
Branch: 1 branch
BranchTrueFalseTotal
if (p > tag_start) 50 2 52
753 50 eshu_hl_span(&out, "esh-g", tag_start, p);
754 /* scan attributes */
755 173 ▶ while (p < end && *p != '>') {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '>') { 173 0 173
while (p < end && *p != '>') { 121 52 173
756 121 ▶ if (*p == '/') { eshu_hl_putc_html(&out, *p++); plain = p; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '/') { eshu_hl_putc_html(&out, *p++); plain = p; continue; } 5 116 121
757 /* attribute name */
758 116 ▶ if (isalpha((unsigned char)*p) || *p == '_' || *p == ':') {
Branch: 3 branches
BranchTrueFalseTotal
if (isalpha((unsigned char)*p) || *p == '_' || *p == ':') { 87 29 116
if (isalpha((unsigned char)*p) || *p == '_' || *p == ':') { 87 0 87
if (isalpha((unsigned char)*p) || *p == '_' || *p == ':') { 0 87 87
759 29 const char *an = p;
760 156 ▶ while (p < end && (isalnum((unsigned char)*p) || *p == '-' ||
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (isalnum((unsigned char)*p) || *p == '-' || 156 0 156
while (p < end && (isalnum((unsigned char)*p) || *p == '-' || 124 32 156
while (p < end && (isalnum((unsigned char)*p) || *p == '-' || 1 31 32
761 156 ▶ *p == '_' || *p == ':' || *p == '.')) p++;
Branch: 3 branches
BranchTrueFalseTotal
*p == '_' || *p == ':' || *p == '.')) p++; 0 31 31
*p == '_' || *p == ':' || *p == '.')) p++; 2 29 31
*p == '_' || *p == ':' || *p == '.')) p++; 0 29 29
762 29 eshu_hl_span(&out, "esh-a", an, p);
763 29 plain = p;
764 29 continue;
765 }
766 /* attribute value */
767 87 ▶ if (*p == '"' || *p == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '"' || *p == '\'') { 64 23 87
if (*p == '"' || *p == '\'') { 3 61 64
768 26 char q = *p;
769 26 const char *vs = p++;
770 179 ▶ while (p < end && *p != q) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != q) p++; 179 0 179
while (p < end && *p != q) p++; 153 26 179
771 26 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 26 0 26
772 26 eshu_hl_span(&out, "esh-s", vs, p);
773 26 plain = p;
774 26 continue;
775 }
776 61 eshu_hl_putc_html(&out, *p++);
777 61 plain = p;
778 }
779 52 ▶ if (p < end && *p == '>') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '>') { 52 0 52
if (p < end && *p == '>') { 52 0 52
780 52 eshu_hl_putc_html(&out, '>');
781 52 p++;
782 }
783 52 plain = p;
784 52 continue;
785 }
786  
787 108 p++;
788 }
789  
790 40 eshu_hl_flush(&out, plain, end);
791 40 eshu_buf_putc(&out, '\0');
792 40 *out_len = out.len - 1;
793 40 return out.data;
794  
795 #undef X_SPAN
796 }
797  
798 #endif /* ESHU_XML_H */
« include/eshu_c.h ↑ Summary include/eshu_js.h »