HTML spans (tables in READMEs write code that way): their text, * without inner tags and with the common entities decoded. */
| 1150 | /* HTML <code> spans (tables in READMEs write code that way): their text, |
| 1151 | * without inner tags and with the common entities decoded. */ |
| 1152 | static void md_html_code(md_scan_t *s) { |
| 1153 | int i = 0; |
| 1154 | while (i < s->len) { |
| 1155 | int open = md_find(s->buf, s->len, i, "<code", true); |
| 1156 | if (open < 0) { |
| 1157 | return; |
| 1158 | } |
| 1159 | int after = open + (int)MD_SLEN("<code"); |
| 1160 | int body; |
| 1161 | if (after < s->len && s->buf[after] == '>') { |
| 1162 | body = after + SKIP_ONE; |
| 1163 | } else if (after < s->len && md_blank((unsigned char)s->buf[after])) { |
| 1164 | const char *gt = memchr(s->buf + after, '>', (size_t)(s->len - after)); |
| 1165 | if (!gt) { |
| 1166 | return; |
| 1167 | } |
| 1168 | body = (int)(gt - s->buf) + SKIP_ONE; |
| 1169 | } else { |
| 1170 | i = open + SKIP_ONE; |
| 1171 | continue; |
| 1172 | } |
| 1173 | int close = md_find(s->buf, s->len, body, "</code>", true); |
| 1174 | if (close < 0) { |
| 1175 | return; |
| 1176 | } |
| 1177 | /* text without tags, entities decoded; never longer than the body */ |
| 1178 | char *text = s->span_buf + MD_SPAN_MAX + SKIP_ONE; /* second half of the scratch */ |
| 1179 | int w = 0; |
| 1180 | int k = body; |
| 1181 | while (k < close && w < MD_SPAN_MAX + SKIP_ONE) { |
| 1182 | char c = s->buf[k]; |
| 1183 | if (c == '<') { |
| 1184 | int lim = k + MD_TAG_MAX + PAIR_LEN; /* `<`, up to 200 bytes, `>` */ |
| 1185 | const char *gt = memchr(s->buf + k + SKIP_ONE, '>', |
| 1186 | (size_t)((lim < close ? lim : close) - k - SKIP_ONE)); |
| 1187 | if (gt) { |
| 1188 | k = (int)(gt - s->buf) + SKIP_ONE; |
| 1189 | continue; |
| 1190 | } |
| 1191 | } else if (c == '&') { |
| 1192 | static const struct { |
| 1193 | const char *ent; |
| 1194 | char ch; |
| 1195 | } ents[] = {{"<", '<'}, {">", '>'}, {"&", '&'}, {""", '"'}, |
| 1196 | {"'", '\''}, {"'", '\''}, {"'", '\''}, {" ", ' '}}; |
| 1197 | bool hit = false; |
| 1198 | for (size_t e = 0; e < sizeof(ents) / sizeof(ents[0]); e++) { |
| 1199 | int el = (int)strlen(ents[e].ent); |
| 1200 | if (k + el <= close && memcmp(s->buf + k, ents[e].ent, (size_t)el) == 0) { |
| 1201 | text[w++] = ents[e].ch; |
| 1202 | k += el; |
| 1203 | hit = true; |
| 1204 | break; |
| 1205 | } |
| 1206 | } |
| 1207 | if (hit) { |
| 1208 | continue; |
| 1209 | } |
no test coverage detected