hparser.c

filter: Statementstmt 91.3% 930 / 1018 Branchbran 83.7% 807 / 964 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.6% 1737 / 1982 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/HTML/LinkExtor.pm ↑ Summary Parser.xs »
1 /*
2 * Copyright 1999-2016, Gisle Aas
3 * Copyright 1999-2000, Michael A. Chase
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the same terms as Perl itself.
7 */
8  
9 #ifndef EXTERN
10 #define EXTERN extern
11 #endif
12  
13 #include "hctype.h" /* isH...() macros */
14 #include "tokenpos.h" /* dTOKEN; PUSH_TOKEN() */
15  
16  
17 const static
18 struct literal_tag {
19 int len;
20 char* str;
21 int is_cdata;
22 }
23 literal_mode_elem[] =
24 {
25 {6, "script", 1},
26 {5, "style", 1},
27 {3, "xmp", 1},
28 {6, "iframe", 1},
29 {9, "plaintext", 1},
30 {5, "title", 0},
31 {8, "textarea", 0},
32 {0, 0, 0}
33 };
34  
35 enum argcode {
36 ARG_SELF = 1, /* need to avoid '\0' in argspec string */
37 ARG_TOKENS,
38 ARG_TOKENPOS,
39 ARG_TOKEN0,
40 ARG_TAGNAME,
41 ARG_TAG,
42 ARG_ATTR,
43 ARG_ATTRARR,
44 ARG_ATTRSEQ,
45 ARG_TEXT,
46 ARG_DTEXT,
47 ARG_IS_CDATA,
48 ARG_SKIPPED_TEXT,
49 ARG_OFFSET,
50 ARG_OFFSET_END,
51 ARG_LENGTH,
52 ARG_LINE,
53 ARG_COLUMN,
54 ARG_EVENT,
55 ARG_UNDEF,
56 ARG_LITERAL, /* Always keep last */
57  
58 /* extra flags always encoded first */
59 ARG_FLAG_FLAT_ARRAY
60 };
61  
62 static const char * const argname[] = {
63 /* Must be in the same order as enum argcode */
64 "self", /* ARG_SELF */
65 "tokens", /* ARG_TOKENS */
66 "tokenpos", /* ARG_TOKENPOS */
67 "token0", /* ARG_TOKEN0 */
68 "tagname", /* ARG_TAGNAME */
69 "tag", /* ARG_TAG */
70 "attr", /* ARG_ATTR */
71 "@attr", /* ARG_ATTRARR */
72 "attrseq", /* ARG_ATTRSEQ */
73 "text", /* ARG_TEXT */
74 "dtext", /* ARG_DTEXT */
75 "is_cdata", /* ARG_IS_CDATA */
76 "skipped_text", /* ARG_SKIPPED_TEXT */
77 "offset", /* ARG_OFFSET */
78 "offset_end", /* ARG_OFFSET_END */
79 "length", /* ARG_LENGTH */
80 "line", /* ARG_LINE */
81 "column", /* ARG_COLUMN */
82 "event", /* ARG_EVENT */
83 "undef", /* ARG_UNDEF */
84 /* ARG_LITERAL (not compared) */
85 /* ARG_FLAG_FLAT_ARRAY */
86 };
87  
88 #define CASE_SENSITIVE(p_state) \
89 ((p_state)->xml_mode || (p_state)->case_sensitive)
90 #define STRICT_NAMES(p_state) \
91 ((p_state)->xml_mode || (p_state)->strict_names)
92 #define ALLOW_EMPTY_TAG(p_state) \
93 ((p_state)->xml_mode || (p_state)->empty_element_tags)
94  
95 static void flush_pending_text(PSTATE* p_state, SV* self);
96  
97 /*
98 * Parser functions.
99 *
100 * parse() - top level entry point.
101 * deals with text and calls one of its
102 * subordinate parse_*() routines after
103 * looking at the first char after "<"
104 * parse_decl() - deals with declarations <!...>
105 * parse_comment() - deals with <!-- ... -->
106 * parse_marked_section - deals with <![ ... [ ... ]]>
107 * parse_end() - deals with end tags </...>
108 * parse_start() - deals with start tags <A...>
109 * parse_process() - deals with process instructions <?...>
110 * parse_null() - deals with anything else <....>
111 *
112 * report_event() - called whenever any of the parse*() routines
113 * has recongnized something.
114 */
115  
116 static void
117 31707 report_event(PSTATE* p_state,
118 event_id_t event,
119 char *beg, char *end, U32 utf8,
120 token_pos_t *tokens, int num_tokens,
121 SV* self
122 )
123 {
124 struct p_handler *h;
125 dTHX;
126 31707 dSP;
127 AV *array;
128 STRLEN my_na;
129 char *argspec;
130 char *s;
131 STRLEN offset;
132 STRLEN line;
133 STRLEN column;
134  
135 #define CHR_DIST(a,b) (utf8 ? utf8_distance((U8*)(a),(U8*)(b)) : (a) - (b))
136  
137 /* some events might still fire after a handler has signaled eof
138 * so suppress them here.
139 */
140 31707 ▶ if (p_state->eof)
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->eof) 4 31703 31707
141 4 return;
142  
143 /* capture offsets */
144 31703 offset = p_state->offset;
145 31703 line = p_state->line;
146 31703 column = p_state->column;
147  
148 #if 0
149 { /* used for debugging at some point */
150 char *s = beg;
151 int i;
152  
153 /* print debug output */
154 switch(event) {
155 case E_DECLARATION: printf("DECLARATION"); break;
156 case E_COMMENT: printf("COMMENT"); break;
157 case E_START: printf("START"); break;
158 case E_END: printf("END"); break;
159 case E_TEXT: printf("TEXT"); break;
160 case E_PROCESS: printf("PROCESS"); break;
161 case E_NONE: printf("NONE"); break;
162 default: printf("EVENT #%d", event); break;
163 }
164  
165 printf(" [");
166 while (s < end) {
167 if (*s == '\n') {
168 putchar('\\'); putchar('n');
169 }
170 else
171 putchar(*s);
172 s++;
173 }
174 printf("] %d\n", end - beg);
175 for (i = 0; i < num_tokens; i++) {
176 printf(" token %d: %d %d\n",
177 i,
178 tokens[i].beg - beg,
179 tokens[i].end - tokens[i].beg);
180 }
181 }
182 #endif
183  
184 31703 ▶ if (p_state->pending_end_tag && event != E_TEXT && event != E_COMMENT) {
Branch: 3 branches
BranchTrueFalseTotal
if (p_state->pending_end_tag && event != E_TEXT && event != E_COMMENT) { 0 31703 31703
if (p_state->pending_end_tag && event != E_TEXT && event != E_COMMENT) { 0 0 0
if (p_state->pending_end_tag && event != E_TEXT && event != E_COMMENT) { 0 0 0
185 token_pos_t t;
186 char dummy;
187 0 t.beg = p_state->pending_end_tag;
188 0 t.end = p_state->pending_end_tag + strlen(p_state->pending_end_tag);
189 0 p_state->pending_end_tag = 0;
190 0 report_event(p_state, E_END, &dummy, &dummy, 0, &t, 1, self);
191 0 SPAGAIN;
192 }
193  
194 /* update offsets */
195 31703 ▶ p_state->offset += CHR_DIST(end, beg);
Branch: 1 branch
BranchTrueFalseTotal
p_state->offset += CHR_DIST(end, beg); 154 31549 31703
196 31703 ▶ if (line) {
Branch: 1 branch
BranchTrueFalseTotal
if (line) { 122 31581 31703
197 122 char *s = beg;
198 122 char *nl = NULL;
199 840 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 718 122 840
200 718 ▶ if (*s == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '\n') { 31 687 718
201 31 p_state->line++;
202 31 nl = s;
203 }
204 718 s++;
205 }
206 122 ▶ if (nl)
Branch: 1 branch
BranchTrueFalseTotal
if (nl) 27 95 122
207 27 ▶ p_state->column = CHR_DIST(end, nl) - 1;
Branch: 1 branch
BranchTrueFalseTotal
p_state->column = CHR_DIST(end, nl) - 1; 10 17 27
208 else
209 95 ▶ p_state->column += CHR_DIST(end, beg);
Branch: 1 branch
BranchTrueFalseTotal
p_state->column += CHR_DIST(end, beg); 22 73 95
210 }
211  
212 31703 ▶ if (event == E_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if (event == E_NONE) 32 31671 31703
213 32 goto IGNORE_EVENT;
214  
215 #ifdef MARKED_SECTION
216 31671 ▶ if (p_state->ms == MS_IGNORE)
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->ms == MS_IGNORE) 4 31667 31671
217 4 goto IGNORE_EVENT;
218 #endif
219  
220 /* tag filters */
221 31667 ▶ if (p_state->ignore_tags || p_state->report_tags || p_state->ignore_elements) {
Branch: 3 branches
BranchTrueFalseTotal
if (p_state->ignore_tags || p_state->report_tags || p_state->ignore_elements) { 31537 130 31667
if (p_state->ignore_tags || p_state->report_tags || p_state->ignore_elements) { 31451 86 31537
if (p_state->ignore_tags || p_state->report_tags || p_state->ignore_elements) { 197 31254 31451
222  
223 533 ▶ if (event == E_START || event == E_END) {
Branch: 2 branches
BranchTrueFalseTotal
if (event == E_START || event == E_END) { 308 105 413
if (event == E_START || event == E_END) { 62 246 308
224 167 SV* tagname = p_state->tmp;
225  
226 assert(num_tokens >= 1);
227 167 sv_setpvn(tagname, tokens[0].beg, tokens[0].end - tokens[0].beg);
228 167 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 38 129 167
229 38 SvUTF8_on(tagname);
230 else
231 129 SvUTF8_off(tagname);
232 167 ▶ if (!CASE_SENSITIVE(p_state))
Branch: 2 branches
BranchTrueFalseTotal
if (!CASE_SENSITIVE(p_state)) 159 8 167
if (!CASE_SENSITIVE(p_state)) 155 4 159
233 155 sv_lower(aTHX_ tagname);
234  
235 167 ▶ if (p_state->ignoring_element) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->ignoring_element) { 7 160 167
236 7 ▶ if (sv_eq(p_state->ignoring_element, tagname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(p_state->ignoring_element, tagname)) { 7 0 7
237 7 ▶ if (event == E_START)
Branch: 1 branch
BranchTrueFalseTotal
if (event == E_START) 0 7 7
238 0 p_state->ignore_depth++;
239 7 ▶ else if (--p_state->ignore_depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (--p_state->ignore_depth == 0) { 7 0 7
240 7 SvREFCNT_dec(p_state->ignoring_element);
241 7 p_state->ignoring_element = 0;
242 }
243 }
244 7 goto IGNORE_EVENT;
245 }
246  
247 244 if (p_state->ignore_elements &&
248 84 hv_fetch_ent(p_state->ignore_elements, tagname, 0, 0))
249 {
250 8 ▶ if (event == E_START) {
Branch: 1 branch
BranchTrueFalseTotal
if (event == E_START) { 7 1 8
251 7 p_state->ignoring_element = newSVsv(tagname);
252 7 p_state->ignore_depth = 1;
253 }
254 8 goto IGNORE_EVENT;
255 }
256  
257 205 if (p_state->ignore_tags &&
258 53 hv_fetch_ent(p_state->ignore_tags, tagname, 0, 0))
259 {
260 20 goto IGNORE_EVENT;
261 }
262 174 if (p_state->report_tags &&
263 42 !hv_fetch_ent(p_state->report_tags, tagname, 0, 0))
264 {
265 12 goto IGNORE_EVENT;
266 }
267 }
268 246 ▶ else if (p_state->ignoring_element) {
Branch: 1 branch
BranchTrueFalseTotal
else if (p_state->ignoring_element) { 7 239 246
269 7 goto IGNORE_EVENT;
270 }
271 }
272  
273 31613 h = &p_state->handlers[event];
274 31613 ▶ if (!h->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!h->cb) { 3286 28327 31613
275 /* event = E_DEFAULT; */
276 3286 h = &p_state->handlers[E_DEFAULT];
277 3286 ▶ if (!h->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (!h->cb) 2178 1108 3286
278 2178 goto IGNORE_EVENT;
279 }
280  
281 29435 ▶ if (SvTYPE(h->cb) != SVt_PVAV && !SvTRUE(h->cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(h->cb) != SVt_PVAV && !SvTRUE(h->cb)) { 15992 13443 29435
if (SvTYPE(h->cb) != SVt_PVAV && !SvTRUE(h->cb)) { 7 15985 15992
282 /* FALSE scalar ('' or 0) means IGNORE this event */
283 7 return;
284 }
285  
286 29428 ▶ if (p_state->unbroken_text && event == E_TEXT) {
Branch: 2 branches
BranchTrueFalseTotal
if (p_state->unbroken_text && event == E_TEXT) { 8981 20447 29428
if (p_state->unbroken_text && event == E_TEXT) { 4510 4471 8981
287 /* should buffer text */
288 4510 ▶ if (!p_state->pend_text)
Branch: 1 branch
BranchTrueFalseTotal
if (!p_state->pend_text) 16 4494 4510
289 16 p_state->pend_text = newSV(256);
290 4510 ▶ if (SvOK(p_state->pend_text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(p_state->pend_text)) { 4356 154 4510
291 154 ▶ if (p_state->is_cdata != p_state->pend_text_is_cdata) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->is_cdata != p_state->pend_text_is_cdata) { 2 152 154
292 2 flush_pending_text(p_state, self);
293 2 SPAGAIN;
294 2 goto INIT_PEND_TEXT;
295 }
296 }
297 else {
298 4356 INIT_PEND_TEXT:
299 4358 p_state->pend_text_offset = offset;
300 4358 p_state->pend_text_line = line;
301 4358 p_state->pend_text_column = column;
302 4358 p_state->pend_text_is_cdata = p_state->is_cdata;
303 4358 sv_setpvs(p_state->pend_text, "");
304 4358 ▶ if (!utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (!utf8) 4358 0 4358
305 4358 SvUTF8_off(p_state->pend_text);
306 }
307 4510 ▶ if (utf8 && !SvUTF8(p_state->pend_text))
Branch: 2 branches
BranchTrueFalseTotal
if (utf8 && !SvUTF8(p_state->pend_text)) 0 4510 4510
if (utf8 && !SvUTF8(p_state->pend_text)) 0 0 0
308 0 sv_utf8_upgrade(p_state->pend_text);
309 4510 ▶ if (utf8 || !SvUTF8(p_state->pend_text)) {
Branch: 2 branches
BranchTrueFalseTotal
if (utf8 || !SvUTF8(p_state->pend_text)) { 4510 0 4510
if (utf8 || !SvUTF8(p_state->pend_text)) { 4510 0 4510
310 4510 sv_catpvn(p_state->pend_text, beg, end - beg);
311 }
312 else {
313 0 SV *tmp = newSVpvn(beg, end - beg);
314 0 sv_utf8_upgrade(tmp);
315 0 sv_catsv(p_state->pend_text, tmp);
316 0 SvREFCNT_dec(tmp);
317 }
318 4510 return;
319 }
320 24918 ▶ else if (p_state->pend_text && SvOK(p_state->pend_text)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (p_state->pend_text && SvOK(p_state->pend_text)) { 4458 20460 24918
else if (p_state->pend_text && SvOK(p_state->pend_text)) { 4294 164 4458
321 4294 flush_pending_text(p_state, self);
322 4294 SPAGAIN;
323 }
324  
325 /* At this point we have decided to generate an event callback */
326  
327 49835 ▶ argspec = h->argspec ? SvPV(h->argspec, my_na) : "";
Branch: 1 branch
BranchTrueFalseTotal
argspec = h->argspec ? SvPV(h->argspec, my_na) : ""; 24917 1 24918
328  
329 24918 ▶ if (SvTYPE(h->cb) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(h->cb) == SVt_PVAV) { 8945 15973 24918
330  
331 8945 ▶ if (*argspec == ARG_FLAG_FLAT_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (*argspec == ARG_FLAG_FLAT_ARRAY) { 502 8443 8945
332 502 argspec++;
333 502 array = (AV*)h->cb;
334 }
335 else {
336 /* start sub-array for accumulator array */
337 8443 array = newAV();
338 }
339 }
340 else {
341 15973 array = 0;
342 15973 ▶ if (*argspec == ARG_FLAG_FLAT_ARRAY)
Branch: 1 branch
BranchTrueFalseTotal
if (*argspec == ARG_FLAG_FLAT_ARRAY) 0 15973 15973
343 0 argspec++;
344  
345 /* start argument stack for callback */
346 15973 ENTER;
347 15973 SAVETMPS;
348 15973 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 15973 15973
349 }
350  
351 107696 ▶ for (s = argspec; *s; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = argspec; *s; s++) { 82778 24918 107696
352 82778 SV* arg = 0;
353 82778 int push_arg = 1;
354 82778 enum argcode argcode = (enum argcode)*s;
355  
356 82778 switch( argcode ) {
357  
358 15606 case ARG_SELF:
359 15606 arg = sv_mortalcopy(self);
360 15606 break;
361  
362 94 case ARG_TOKENS:
363 94 ▶ if (num_tokens >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_tokens >= 1) { 77 17 94
364 77 AV* av = newAV();
365 77 SV* prev_token = &PL_sv_undef;
366 int i;
367 77 av_extend(av, num_tokens);
368 491 ▶ for (i = 0; i < num_tokens; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_tokens; i++) { 414 77 491
369 414 ▶ if (tokens[i].beg) {
Branch: 1 branch
BranchTrueFalseTotal
if (tokens[i].beg) { 414 0 414
370 414 prev_token = newSVpvn(tokens[i].beg, tokens[i].end-tokens[i].beg);
371 414 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 0 414 414
372 0 SvUTF8_on(prev_token);
373 414 av_push(av, prev_token);
374 }
375 else { /* boolean */
376 0 ▶ av_push(av, p_state->bool_attr_val
Branch: 1 branch
BranchTrueFalseTotal
av_push(av, p_state->bool_attr_val 0 0 0
377 ? newSVsv(p_state->bool_attr_val)
378 : newSVsv(prev_token));
379 }
380 }
381 77 arg = sv_2mortal(newRV_noinc((SV*)av));
382 }
383 94 break;
384  
385 84 case ARG_TOKENPOS:
386 84 ▶ if (num_tokens >= 1 && tokens[0].beg >= beg) {
Branch: 2 branches
BranchTrueFalseTotal
if (num_tokens >= 1 && tokens[0].beg >= beg) { 33 51 84
if (num_tokens >= 1 && tokens[0].beg >= beg) { 32 1 33
387 32 AV* av = newAV();
388 int i;
389 32 av_extend(av, num_tokens*2);
390 401 ▶ for (i = 0; i < num_tokens; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_tokens; i++) { 369 32 401
391 369 ▶ if (tokens[i].beg) {
Branch: 1 branch
BranchTrueFalseTotal
if (tokens[i].beg) { 367 2 369
392 367 ▶ av_push(av, newSViv(CHR_DIST(tokens[i].beg, beg)));
Branch: 1 branch
BranchTrueFalseTotal
av_push(av, newSViv(CHR_DIST(tokens[i].beg, beg))); 13 354 367
393 367 ▶ av_push(av, newSViv(CHR_DIST(tokens[i].end, tokens[i].beg)));
Branch: 1 branch
BranchTrueFalseTotal
av_push(av, newSViv(CHR_DIST(tokens[i].end, tokens[i].beg))); 13 354 367
394 }
395 else { /* boolean tag value */
396 2 av_push(av, newSViv(0));
397 2 av_push(av, newSViv(0));
398 }
399 }
400 32 arg = sv_2mortal(newRV_noinc((SV*)av));
401 }
402 84 break;
403  
404 6715 case ARG_TOKEN0:
405 case ARG_TAGNAME:
406 /* fall through */
407  
408 case ARG_TAG:
409 6715 ▶ if (num_tokens >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_tokens >= 1) { 6647 68 6715
410 6647 arg = sv_2mortal(newSVpvn(tokens[0].beg,
411 tokens[0].end - tokens[0].beg));
412 6647 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 29 6618 6647
413 29 SvUTF8_on(arg);
414 6647 ▶ if (!CASE_SENSITIVE(p_state) && argcode != ARG_TOKEN0)
Branch: 3 branches
BranchTrueFalseTotal
if (!CASE_SENSITIVE(p_state) && argcode != ARG_TOKEN0) 6586 61 6647
if (!CASE_SENSITIVE(p_state) && argcode != ARG_TOKEN0) 6581 5 6586
if (!CASE_SENSITIVE(p_state) && argcode != ARG_TOKEN0) 6521 60 6581
415 6521 sv_lower(aTHX_ arg);
416 6647 ▶ if (argcode == ARG_TAG && event != E_START) {
Branch: 2 branches
BranchTrueFalseTotal
if (argcode == ARG_TAG && event != E_START) { 21 6626 6647
if (argcode == ARG_TAG && event != E_START) { 10 11 21
417 10 char *e_type = "!##/#?#";
418 10 sv_insert(arg, 0, 0, &e_type[event], 1);
419 }
420 }
421 6715 break;
422  
423 4440 case ARG_ATTR:
424 case ARG_ATTRARR:
425 4440 ▶ if (event == E_START) {
Branch: 1 branch
BranchTrueFalseTotal
if (event == E_START) { 4348 92 4440
426 HV* hv;
427 int i;
428 4348 ▶ if (argcode == ARG_ATTR) {
Branch: 1 branch
BranchTrueFalseTotal
if (argcode == ARG_ATTR) { 4339 9 4348
429 4339 hv = newHV();
430 4339 arg = sv_2mortal(newRV_noinc((SV*)hv));
431 }
432 else {
433 #ifdef __GNUC__
434 /* gcc -Wall reports this variable as possibly used uninitialized */
435 9 hv = 0;
436 #endif
437 9 push_arg = 0; /* deal with argument pushing here */
438 }
439  
440 16412 ▶ for (i = 1; i < num_tokens; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < num_tokens; i += 2) { 12064 4348 16412
441 12064 SV* attrname = newSVpvn(tokens[i].beg,
442 tokens[i].end-tokens[i].beg);
443 SV* attrval;
444  
445 12064 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 37 12027 12064
446 37 SvUTF8_on(attrname);
447 12064 ▶ if (tokens[i+1].beg) {
Branch: 1 branch
BranchTrueFalseTotal
if (tokens[i+1].beg) { 3414 8650 12064
448 3414 char *beg = tokens[i+1].beg;
449 3414 STRLEN len = tokens[i+1].end - beg;
450 3414 ▶ if (*beg == '"' || *beg == '\'' || (*beg == '`' && p_state->backquote)) {
Branch: 4 branches
BranchTrueFalseTotal
if (*beg == '"' || *beg == '\'' || (*beg == '`' && p_state->backquote)) { 3255 159 3414
if (*beg == '"' || *beg == '\'' || (*beg == '`' && p_state->backquote)) { 1212 2043 3255
if (*beg == '"' || *beg == '\'' || (*beg == '`' && p_state->backquote)) { 7 1205 1212
if (*beg == '"' || *beg == '\'' || (*beg == '`' && p_state->backquote)) { 1 6 7
451 assert(len >= 2 && *beg == beg[len-1]);
452 2203 beg++; len -= 2;
453 }
454 3414 attrval = newSVpvn(beg, len);
455 3414 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 34 3380 3414
456 34 SvUTF8_on(attrval);
457 3414 ▶ if (!p_state->attr_encoded) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p_state->attr_encoded) { 3412 2 3414
458 3412 ▶ if (p_state->utf8_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->utf8_mode) { 20 3392 3412
459 20 sv_utf8_decode(attrval);
460 20 sv_utf8_upgrade(attrval);
461 }
462 3412 decode_entities(aTHX_ attrval, p_state->entity2char, 0);
463 3412 ▶ if (p_state->utf8_mode)
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->utf8_mode) 20 3392 3412
464 20 SvUTF8_off(attrval);
465 }
466 }
467 else { /* boolean */
468 8650 ▶ if (p_state->bool_attr_val)
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->bool_attr_val) 0 8650 8650
469 0 attrval = newSVsv(p_state->bool_attr_val);
470 else
471 8650 attrval = newSVsv(attrname);
472 }
473  
474 12064 ▶ if (!CASE_SENSITIVE(p_state))
Branch: 2 branches
BranchTrueFalseTotal
if (!CASE_SENSITIVE(p_state)) 12051 13 12064
if (!CASE_SENSITIVE(p_state)) 12047 4 12051
475 12047 sv_lower(aTHX_ attrname);
476  
477 12064 ▶ if (argcode == ARG_ATTR) {
Branch: 1 branch
BranchTrueFalseTotal
if (argcode == ARG_ATTR) { 12056 8 12064
478 24109 if (hv_exists_ent(hv, attrname, 0) ||
479 12053 !hv_store_ent(hv, attrname, attrval, 0)) {
480 3 SvREFCNT_dec(attrval);
481 }
482 12056 SvREFCNT_dec(attrname);
483 }
484 else { /* ARG_ATTRARR */
485 8 ▶ if (array) {
Branch: 1 branch
BranchTrueFalseTotal
if (array) { 4 4 8
486 4 av_push(array, attrname);
487 4 av_push(array, attrval);
488 }
489 else {
490 4 ▶ mXPUSHs(attrname);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(attrname); 0 4 4
491 4 ▶ mXPUSHs(attrval);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(attrval); 0 4 4
492 }
493 }
494 }
495 }
496 92 ▶ else if (argcode == ARG_ATTRARR) {
Branch: 1 branch
BranchTrueFalseTotal
else if (argcode == ARG_ATTRARR) { 18 74 92
497 18 push_arg = 0;
498 }
499 4440 break;
500  
501 4263 case ARG_ATTRSEQ: /* (v2 compatibility stuff) */
502 4263 ▶ if (event == E_START) {
Branch: 1 branch
BranchTrueFalseTotal
if (event == E_START) { 4238 25 4263
503 4238 AV* av = newAV();
504 int i;
505 16189 ▶ for (i = 1; i < num_tokens; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < num_tokens; i += 2) { 11951 4238 16189
506 11951 SV* attrname = newSVpvn(tokens[i].beg,
507 tokens[i].end-tokens[i].beg);
508 11951 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 0 11951 11951
509 0 SvUTF8_on(attrname);
510 11951 ▶ if (!CASE_SENSITIVE(p_state))
Branch: 2 branches
BranchTrueFalseTotal
if (!CASE_SENSITIVE(p_state)) 11941 10 11951
if (!CASE_SENSITIVE(p_state)) 11937 4 11941
511 11937 sv_lower(aTHX_ attrname);
512 11951 av_push(av, attrname);
513 }
514 4238 arg = sv_2mortal(newRV_noinc((SV*)av));
515 }
516 4263 break;
517  
518 24616 case ARG_TEXT:
519 24616 arg = sv_2mortal(newSVpvn(beg, end - beg));
520 24616 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 115 24501 24616
521 115 SvUTF8_on(arg);
522 24616 break;
523  
524 156 case ARG_DTEXT:
525 156 ▶ if (event == E_TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (event == E_TEXT) { 79 77 156
526 79 arg = sv_2mortal(newSVpvn(beg, end - beg));
527 79 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 23 56 79
528 23 SvUTF8_on(arg);
529 79 ▶ if (!p_state->is_cdata) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p_state->is_cdata) { 71 8 79
530 71 ▶ if (p_state->utf8_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->utf8_mode) { 8 63 71
531 8 sv_utf8_decode(arg);
532 8 sv_utf8_upgrade(arg);
533 }
534 71 decode_entities(aTHX_ arg, p_state->entity2char, 1);
535 71 ▶ if (p_state->utf8_mode)
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->utf8_mode) 8 63 71
536 8 SvUTF8_off(arg);
537 }
538 }
539 156 break;
540  
541 17332 case ARG_IS_CDATA:
542 17332 ▶ if (event == E_TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (event == E_TEXT) { 17279 53 17332
543 17279 ▶ arg = boolSV(p_state->is_cdata);
Branch: 1 branch
BranchTrueFalseTotal
arg = boolSV(p_state->is_cdata); 7 17272 17279
544 }
545 17332 break;
546  
547 14 case ARG_SKIPPED_TEXT:
548 14 arg = sv_2mortal(p_state->skipped_text);
549 14 p_state->skipped_text = newSVpvs("");
550 14 break;
551  
552 122 case ARG_OFFSET:
553 122 arg = sv_2mortal(newSViv(offset));
554 122 break;
555  
556 74 case ARG_OFFSET_END:
557 74 ▶ arg = sv_2mortal(newSViv(offset + CHR_DIST(end, beg)));
Branch: 1 branch
BranchTrueFalseTotal
arg = sv_2mortal(newSViv(offset + CHR_DIST(end, beg))); 16 58 74
558 74 break;
559  
560 101 case ARG_LENGTH:
561 101 ▶ arg = sv_2mortal(newSViv(CHR_DIST(end, beg)));
Branch: 1 branch
BranchTrueFalseTotal
arg = sv_2mortal(newSViv(CHR_DIST(end, beg))); 16 85 101
562 101 break;
563  
564 39 case ARG_LINE:
565 39 arg = sv_2mortal(newSViv(line));
566 39 break;
567  
568 95 case ARG_COLUMN:
569 95 arg = sv_2mortal(newSViv(column));
570 95 break;
571  
572 708 case ARG_EVENT:
573 assert(event >= 0 && event < EVENT_COUNT);
574 708 arg = sv_2mortal(newSVpv(event_id_str[event], 0));
575 708 break;
576  
577 8319 case ARG_LITERAL:
578 {
579 8319 int len = (unsigned char)s[1];
580 8319 arg = sv_2mortal(newSVpvn(s+2, len));
581 8319 ▶ if (SvUTF8(h->argspec))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(h->argspec)) 0 8319 8319
582 0 SvUTF8_on(arg);
583 8319 s += len + 1;
584 }
585 8319 break;
586  
587 0 case ARG_UNDEF:
588 0 arg = sv_mortalcopy(&PL_sv_undef);
589 0 break;
590  
591 0 default:
592 0 arg = sv_2mortal(newSVpvf("Bad argspec %d", *s));
593 0 break;
594 }
595  
596 82778 ▶ if (push_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (push_arg) { 82751 27 82778
597 82751 ▶ if (!arg)
Branch: 1 branch
BranchTrueFalseTotal
if (!arg) 366 82385 82751
598 366 arg = sv_mortalcopy(&PL_sv_undef);
599  
600 82751 ▶ if (array) {
Branch: 1 branch
BranchTrueFalseTotal
if (array) { 31113 51638 82751
601 /* have to fix mortality here or add mortality to
602 * XPUSHs after removing it from the switch cases.
603 */
604 31113 av_push(array, SvREFCNT_inc(arg));
605 }
606 else {
607 51638 ▶ XPUSHs(arg);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(arg); 0 51638 51638
608 }
609 }
610 }
611  
612 24918 ▶ if (array) {
Branch: 1 branch
BranchTrueFalseTotal
if (array) { 8945 15973 24918
613 8945 ▶ if (array != (AV*)h->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (array != (AV*)h->cb) 8443 502 8945
614 8443 av_push((AV*)h->cb, newRV_noinc((SV*)array));
615 }
616 else {
617 15973 PUTBACK;
618  
619 31470 ▶ if ((enum argcode)*argspec == ARG_SELF && !SvROK(h->cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((enum argcode)*argspec == ARG_SELF && !SvROK(h->cb)) { 15576 397 15973
if ((enum argcode)*argspec == ARG_SELF && !SvROK(h->cb)) { 15497 79 15576
620 15497 char *method = SvPV(h->cb, my_na);
621 15497 call_method(method, G_DISCARD | G_EVAL | G_VOID);
622 }
623 else {
624 476 call_sv(h->cb, G_DISCARD | G_EVAL | G_VOID);
625 }
626  
627 15973 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 15973 0 15973
if (SvTRUE(ERRSV)) { 1 15972 15973
628 1 RETHROW;
629 }
630  
631 15972 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 15968 4 15972
632 15972 LEAVE;
633 }
634 24917 ▶ if (p_state->skipped_text)
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->skipped_text) 14 24903 24917
635 14 SvCUR_set(p_state->skipped_text, 0);
636 24917 return;
637  
638 2268 IGNORE_EVENT:
639 2268 ▶ if (p_state->skipped_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->skipped_text) { 38 2230 2268
640 38 ▶ if (event != E_TEXT && p_state->pend_text && SvOK(p_state->pend_text))
Branch: 3 branches
BranchTrueFalseTotal
if (event != E_TEXT && p_state->pend_text && SvOK(p_state->pend_text)) 20 18 38
if (event != E_TEXT && p_state->pend_text && SvOK(p_state->pend_text)) 2 18 20
if (event != E_TEXT && p_state->pend_text && SvOK(p_state->pend_text)) 2 0 2
641 2 flush_pending_text(p_state, self);
642 38 ▶ if (utf8 && !SvUTF8(p_state->skipped_text))
Branch: 2 branches
BranchTrueFalseTotal
if (utf8 && !SvUTF8(p_state->skipped_text)) 2 36 38
if (utf8 && !SvUTF8(p_state->skipped_text)) 1 1 2
643 1 sv_utf8_upgrade(p_state->skipped_text);
644 38 ▶ if (utf8 || !SvUTF8(p_state->skipped_text)) {
Branch: 2 branches
BranchTrueFalseTotal
if (utf8 || !SvUTF8(p_state->skipped_text)) { 36 2 38
if (utf8 || !SvUTF8(p_state->skipped_text)) { 35 1 36
645 37 sv_catpvn(p_state->skipped_text, beg, end - beg);
646 }
647 else {
648 1 SV *tmp = newSVpvn(beg, end - beg);
649 1 sv_utf8_upgrade(tmp);
650 1 sv_catsv(p_state->skipped_text, tmp);
651 1 SvREFCNT_dec(tmp);
652 }
653 }
654 #undef CHR_DIST
655 2268 return;
656 }
657  
658  
659 EXTERN SV*
660 463 argspec_compile(SV* src, PSTATE* p_state)
661 {
662 dTHX;
663 463 SV* argspec = newSVpvs("");
664 STRLEN len;
665 463 char *s = SvPV(src, len);
666 463 char *end = s + len;
667  
668 463 ▶ if (SvUTF8(src))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) 0 463 463
669 0 SvUTF8_on(argspec);
670  
671 463 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 0 463 463
672 0 s++;
673  
674 463 ▶ if (*s == '@') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '@') { 7 456 463
675 /* try to deal with '@{ ... }' wrapping */
676 7 char *tmp = s + 1;
677 7 ▶ while (isHSPACE(*tmp))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*tmp)) 0 7 7
678 0 tmp++;
679 7 ▶ if (*tmp == '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (*tmp == '{') { 7 0 7
680 7 char c = ARG_FLAG_FLAT_ARRAY;
681 7 sv_catpvn(argspec, &c, 1);
682 7 tmp++;
683 7 ▶ while (isHSPACE(*tmp))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*tmp)) 0 7 7
684 0 tmp++;
685 7 s = tmp;
686 }
687 }
688 1271 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 1267 4 1271
689 2465 ▶ if (isHNAME_FIRST(*s) || *s == '@') {
Branch: 2 branches
BranchTrueFalseTotal
if (isHNAME_FIRST(*s) || *s == '@') { 71 1196 1267
if (isHNAME_FIRST(*s) || *s == '@') { 4 67 71
690 1200 char *name = s;
691 1200 int a = ARG_SELF;
692 const char * const *arg_name;
693  
694 1200 s++;
695 6076 ▶ while (isHNAME_CHAR(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHNAME_CHAR(*s)) 4876 1200 6076
696 4876 s++;
697  
698 /* check identifier */
699 7622 ▶ for ( arg_name = argname; a < ARG_LITERAL ; ++a, ++arg_name ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( arg_name = argname; a < ARG_LITERAL ; ++a, ++arg_name ) { 7620 2 7622
700 7620 ▶ if (strnEQ(*arg_name, name, s - name) &&
Branch: 1 branch
BranchTrueFalseTotal
if (strnEQ(*arg_name, name, s - name) && 1202 6418 7620
701 1202 ▶ (*arg_name)[s - name] == '\0')
Branch: 1 branch
BranchTrueFalseTotal
(*arg_name)[s - name] == '\0') 1198 4 1202
702 1198 break;
703 }
704 1200 ▶ if (a < ARG_LITERAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (a < ARG_LITERAL) { 1198 2 1200
705 1198 char c = (unsigned char) a;
706 1198 sv_catpvn(argspec, &c, 1);
707  
708 1198 ▶ if (a == ARG_LINE || a == ARG_COLUMN) {
Branch: 2 branches
BranchTrueFalseTotal
if (a == ARG_LINE || a == ARG_COLUMN) { 1194 4 1198
if (a == ARG_LINE || a == ARG_COLUMN) { 5 1189 1194
709 9 ▶ if (!p_state->line)
Branch: 1 branch
BranchTrueFalseTotal
if (!p_state->line) 5 4 9
710 5 p_state->line = 1; /* enable tracing of line/column */
711 }
712 1198 ▶ if (a == ARG_SKIPPED_TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (a == ARG_SKIPPED_TEXT) { 7 1191 1198
713 7 ▶ if (!p_state->skipped_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p_state->skipped_text) { 5 2 7
714 5 p_state->skipped_text = newSVpvs("");
715 }
716 }
717 1198 ▶ if (a == ARG_ATTR || a == ARG_ATTRARR) {
Branch: 2 branches
BranchTrueFalseTotal
if (a == ARG_ATTR || a == ARG_ATTRARR) { 1120 78 1198
if (a == ARG_ATTR || a == ARG_ATTRARR) { 4 1116 1120
718 82 ▶ if (p_state->argspec_entity_decode != ARG_DTEXT)
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->argspec_entity_decode != ARG_DTEXT) 79 3 82
719 79 p_state->argspec_entity_decode = ARG_ATTR;
720 }
721 1116 ▶ else if (a == ARG_DTEXT) {
Branch: 1 branch
BranchTrueFalseTotal
else if (a == ARG_DTEXT) { 10 1106 1116
722 10 p_state->argspec_entity_decode = ARG_DTEXT;
723 }
724 }
725 else {
726 2 croak("Unrecognized identifier %.*s in argspec", (int) (s - name), name);
727 }
728 }
729 131 ▶ else if (*s == '"' || *s == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
else if (*s == '"' || *s == '\'') { 61 6 67
else if (*s == '"' || *s == '\'') { 61 0 61
730 67 char *string_beg = s;
731 67 s++;
732 656 ▶ while (s < end && *s != *string_beg && *s != '\\')
Branch: 3 branches
BranchTrueFalseTotal
while (s < end && *s != *string_beg && *s != '\\') 655 1 656
while (s < end && *s != *string_beg && *s != '\\') 590 65 655
while (s < end && *s != *string_beg && *s != '\\') 589 1 590
733 589 s++;
734 67 ▶ if (*s == *string_beg) {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == *string_beg) { 65 2 67
735 /* literal */
736 65 int len = s - string_beg - 1;
737 unsigned char buf[2];
738 65 ▶ if (len > 255)
Branch: 1 branch
BranchTrueFalseTotal
if (len > 255) 1 64 65
739 1 croak("Literal string is longer than 255 chars in argspec");
740 64 buf[0] = ARG_LITERAL;
741 64 buf[1] = len;
742 64 sv_catpvn(argspec, (char*)buf, 2);
743 64 sv_catpvn(argspec, string_beg+1, len);
744 64 s++;
745 }
746 2 ▶ else if (*s == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '\\') { 1 1 2
747 1 croak("Backslash reserved for literal string in argspec");
748 }
749 else {
750 1 croak("Unterminated literal string in argspec");
751 }
752 }
753 else {
754 0 croak("Bad argspec (%s)", s);
755 }
756  
757 1263 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 1 1262 1263
758 1 s++;
759  
760 1262 ▶ if (*s == '}' && SvPVX(argspec)[0] == ARG_FLAG_FLAT_ARRAY) {
Branch: 2 branches
BranchTrueFalseTotal
if (*s == '}' && SvPVX(argspec)[0] == ARG_FLAG_FLAT_ARRAY) { 7 1255 1262
if (*s == '}' && SvPVX(argspec)[0] == ARG_FLAG_FLAT_ARRAY) { 7 0 7
761 /* end of '@{ ... }' */
762 7 s++;
763 7 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 0 7 7
764 0 s++;
765 7 ▶ if (s < end)
Branch: 1 branch
BranchTrueFalseTotal
if (s < end) 0 7 7
766 0 croak("Bad argspec: stuff after @{...} (%s)", s);
767 }
768  
769 1262 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 453 809 1262
770 453 break;
771 809 ▶ if (*s != ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s != ',') { 1 808 809
772 1 croak("Missing comma separator in argspec");
773 }
774 808 s++;
775 840 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 32 808 840
776 32 s++;
777 }
778 457 return argspec;
779 }
780  
781  
782 static void
783 4358 flush_pending_text(PSTATE* p_state, SV* self)
784 {
785 dTHX;
786 4358 bool old_unbroken_text = p_state->unbroken_text;
787 4358 SV* old_pend_text = p_state->pend_text;
788 4358 bool old_is_cdata = p_state->is_cdata;
789 4358 STRLEN old_offset = p_state->offset;
790 4358 STRLEN old_line = p_state->line;
791 4358 STRLEN old_column = p_state->column;
792  
793 assert(p_state->pend_text && SvOK(p_state->pend_text));
794  
795 4358 p_state->unbroken_text = 0;
796 4358 p_state->pend_text = 0;
797 4358 p_state->is_cdata = p_state->pend_text_is_cdata;
798 4358 p_state->offset = p_state->pend_text_offset;
799 4358 p_state->line = p_state->pend_text_line;
800 4358 p_state->column = p_state->pend_text_column;
801  
802 4358 report_event(p_state, E_TEXT,
803 4358 SvPVX(old_pend_text), SvEND(old_pend_text),
804 4358 SvUTF8(old_pend_text), 0, 0, self);
805 4358 ▶ SvOK_off(old_pend_text);
Branch: 4 branches
BranchTrueFalseTotal
SvOK_off(old_pend_text); 0 4358 4358
SvOK_off(old_pend_text); 0 0 0
SvOK_off(old_pend_text); 0 0 0
SvOK_off(old_pend_text); 0 4358 4358
806  
807 4358 p_state->unbroken_text = old_unbroken_text;
808 4358 p_state->pend_text = old_pend_text;
809 4358 p_state->is_cdata = old_is_cdata;
810 4358 p_state->offset = old_offset;
811 4358 p_state->line = old_line;
812 4358 p_state->column = old_column;
813 4358 }
814  
815 static char*
816 2947 skip_until_gt(char *beg, char *end)
817 {
818 /* tries to emulate quote skipping behaviour observed in MSIE */
819 2947 char *s = beg;
820 2947 char quote = '\0';
821 2947 char prev = ' ';
822 13358 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 13294 64 13358
823 13294 ▶ if (!quote && *s == '>')
Branch: 2 branches
BranchTrueFalseTotal
if (!quote && *s == '>') 11859 1435 13294
if (!quote && *s == '>') 2883 8976 11859
824 2883 return s;
825 10411 ▶ if (*s == '"' || *s == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if (*s == '"' || *s == '\'') { 10380 31 10411
if (*s == '"' || *s == '\'') { 47 10333 10380
826 78 ▶ if (*s == quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == quote) { 8 70 78
827 8 quote = '\0'; /* end of quoted string */
828 }
829 70 ▶ else if (!quote && (prev == ' ' || prev == '=')) {
Branch: 3 branches
BranchTrueFalseTotal
else if (!quote && (prev == ' ' || prev == '=')) { 68 2 70
else if (!quote && (prev == ' ' || prev == '=')) { 62 6 68
else if (!quote && (prev == ' ' || prev == '=')) { 3 59 62
830 9 quote = *s;
831 }
832 }
833 10411 prev = *s++;
834 }
835 64 return end;
836 }
837  
838 static char*
839 177 parse_comment(PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
840 {
841 177 char *s = beg;
842  
843 177 ▶ if (p_state->strict_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->strict_comment) { 6 171 177
844 6 dTOKENS(4);
845 6 char *start_com = s; /* also used to signal inside/outside */
846  
847 while (1) {
848 /* try to locate "--" */
849 15 FIND_DASH_DASH:
850 /* printf("find_dash_dash: [%s]\n", s); */
851 125 ▶ while (s < end && *s != '-' && *s != '>')
Branch: 3 branches
BranchTrueFalseTotal
while (s < end && *s != '-' && *s != '>') 125 0 125
while (s < end && *s != '-' && *s != '>') 112 13 125
while (s < end && *s != '-' && *s != '>') 104 8 112
852 104 s++;
853  
854 21 ▶ if (s == end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) { 0 21 21
855 0 ▶ FREE_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
FREE_TOKENS; 0 0 0
856 0 return beg;
857 }
858  
859 21 ▶ if (*s == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '>') { 8 13 21
860 8 s++;
861 8 ▶ if (start_com)
Branch: 1 branch
BranchTrueFalseTotal
if (start_com) 2 6 8
862 2 goto FIND_DASH_DASH;
863  
864 /* we are done recognizing all comments, make callbacks */
865 6 report_event(p_state, E_COMMENT,
866 beg - 4, s, utf8,
867 tokens, num_tokens,
868 self);
869 6 ▶ FREE_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
FREE_TOKENS; 0 6 6
870  
871 6 return s;
872 }
873  
874 13 s++;
875 13 ▶ if (s == end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) { 0 13 13
876 0 ▶ FREE_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
FREE_TOKENS; 0 0 0
877 0 return beg;
878 }
879  
880 13 ▶ if (*s == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') { 12 1 13
881 /* two dashes in a row seen */
882 12 s++;
883 /* do something */
884 12 ▶ if (start_com) {
Branch: 1 branch
BranchTrueFalseTotal
if (start_com) { 9 3 12
885 9 ▶ PUSH_TOKEN(start_com, s-2);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_TOKEN(start_com, s-2); 0 9 9
886 9 start_com = 0;
887 }
888 else {
889 3 start_com = s;
890 }
891 }
892 }
893 }
894 171 ▶ else if (p_state->no_dash_dash_comment_end) {
Branch: 1 branch
BranchTrueFalseTotal
else if (p_state->no_dash_dash_comment_end) { 2 169 171
895 token_pos_t token;
896 2 token.beg = beg;
897 /* a lone '>' signals end-of-comment */
898 16 ▶ while (s < end && *s != '>')
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && *s != '>') 16 0 16
while (s < end && *s != '>') 14 2 16
899 14 s++;
900 2 token.end = s;
901 2 ▶ if (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s < end) { 2 0 2
902 2 s++;
903 2 report_event(p_state, E_COMMENT, beg-4, s, utf8, &token, 1, self);
904 2 return s;
905 }
906 else {
907 0 return beg;
908 }
909 }
910 else { /* non-strict comment */
911 token_pos_t token;
912 169 token.beg = beg;
913 /* try to locate /--\s*>/ which signals end-of-comment */
914 199 LOCATE_END:
915 2330 ▶ while (s < end && *s != '-')
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && *s != '-') 2226 104 2330
while (s < end && *s != '-') 2131 95 2226
916 2131 s++;
917 199 token.end = s;
918 199 ▶ if (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s < end) { 95 104 199
919 95 s++;
920 95 ▶ if (*s == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') { 78 17 95
921 78 s++;
922 93 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 15 78 93
923 15 s++;
924 78 ▶ if (*s == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '>') { 48 30 78
925 48 s++;
926 /* yup */
927 48 report_event(p_state, E_COMMENT, beg-4, s, utf8, &token, 1, self);
928 169 return s;
929 }
930 }
931 47 ▶ if (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s < end) { 30 17 47
932 30 s = token.end + 1;
933 30 goto LOCATE_END;
934 }
935 }
936  
937 121 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 121 0 121
938 121 return beg;
939 }
940  
941 0 return 0;
942 }
943  
944  
945 #ifdef MARKED_SECTION
946  
947 static void
948 32 marked_section_update(PSTATE* p_state)
949 {
950 dTHX;
951 /* we look at p_state->ms_stack to determine p_state->ms */
952 32 AV* ms_stack = p_state->ms_stack;
953 32 p_state->ms = MS_NONE;
954  
955 32 ▶ if (ms_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (ms_stack) { 32 0 32
956 32 int stack_len = av_len(ms_stack);
957 int stack_idx;
958 50 ▶ for (stack_idx = 0; stack_idx <= stack_len; stack_idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (stack_idx = 0; stack_idx <= stack_len; stack_idx++) { 18 32 50
959 18 SV** svp = av_fetch(ms_stack, stack_idx, 0);
960 18 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 18 0 18
961 18 AV* tokens = (AV*)SvRV(*svp);
962 18 int tokens_len = av_len(tokens);
963 int i;
964 assert(SvTYPE(tokens) == SVt_PVAV);
965 43 ▶ for (i = 0; i <= tokens_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= tokens_len; i++) { 25 18 43
966 25 SV** svp = av_fetch(tokens, i, 0);
967 25 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 25 0 25
968 STRLEN len;
969 25 char *token_str = SvPV(*svp, len);
970 enum marked_section_t token;
971 25 ▶ if (strEQ(token_str, "include"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(token_str, "include")) 11 14 25
972 11 token = MS_INCLUDE;
973 14 ▶ else if (strEQ(token_str, "rcdata"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(token_str, "rcdata")) 4 10 14
974 4 token = MS_RCDATA;
975 10 ▶ else if (strEQ(token_str, "cdata"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(token_str, "cdata")) 7 3 10
976 7 token = MS_CDATA;
977 3 ▶ else if (strEQ(token_str, "ignore"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(token_str, "ignore")) 2 1 3
978 2 token = MS_IGNORE;
979 else
980 1 token = MS_NONE;
981 25 ▶ if (p_state->ms < token)
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->ms < token) 24 1 25
982 24 p_state->ms = token;
983 }
984 }
985 }
986 }
987 }
988 /* printf("MS %d\n", p_state->ms); */
989 32 p_state->is_cdata = (p_state->ms == MS_CDATA);
990 32 return;
991 }
992  
993  
994 static char*
995 16 parse_marked_section(PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
996 {
997 dTHX;
998 char *s;
999 16 AV* tokens = 0;
1000  
1001 16 ▶ if (!p_state->marked_sections)
Branch: 1 branch
BranchTrueFalseTotal
if (!p_state->marked_sections) 0 16 16
1002 0 return 0;
1003  
1004 assert(beg[0] == '<');
1005 assert(beg[1] == '!');
1006 assert(beg[2] == '[');
1007 16 s = beg + 3;
1008  
1009 17 FIND_NAMES:
1010 22 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 5 17 22
1011 5 s++;
1012 37 ▶ while (isHNAME_FIRST(*s)) {
Branch: 1 branch
BranchTrueFalseTotal
while (isHNAME_FIRST(*s)) { 20 17 37
1013 20 char *name_start = s;
1014 char *name_end;
1015 SV *name;
1016 20 s++;
1017 117 ▶ while (isHNAME_CHAR(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHNAME_CHAR(*s)) 97 20 117
1018 97 s++;
1019 20 name_end = s;
1020 39 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 19 20 39
1021 19 s++;
1022 20 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 0 20 20
1023 0 goto PREMATURE;
1024  
1025 20 ▶ if (!tokens)
Branch: 1 branch
BranchTrueFalseTotal
if (!tokens) 13 7 20
1026 13 tokens = newAV();
1027 20 name = newSVpvn(name_start, name_end - name_start);
1028 20 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 1 19 20
1029 1 SvUTF8_on(name);
1030 20 av_push(tokens, sv_lower(aTHX_ name));
1031 }
1032 17 ▶ if (*s == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') { 1 16 17
1033 1 s++;
1034 1 ▶ if (*s == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') { 1 0 1
1035 /* comment */
1036 1 s++;
1037 while (1) {
1038 9 ▶ while (s < end && *s != '-')
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && *s != '-') 9 0 9
while (s < end && *s != '-') 8 1 9
1039 8 s++;
1040 1 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 0 1 1
1041 0 goto PREMATURE;
1042  
1043 1 s++; /* skip first '-' */
1044 1 ▶ if (*s == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') { 1 0 1
1045 1 s++;
1046 /* comment finished */
1047 1 goto FIND_NAMES;
1048 }
1049 }
1050 }
1051 else
1052 0 goto FAIL;
1053  
1054 }
1055 16 ▶ if (*s == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '[') { 16 0 16
1056 16 s++;
1057 /* yup */
1058  
1059 16 ▶ if (!tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tokens) { 3 13 16
1060 3 tokens = newAV();
1061 3 av_push(tokens, newSVpvs("include"));
1062 }
1063  
1064 16 ▶ if (!p_state->ms_stack)
Branch: 1 branch
BranchTrueFalseTotal
if (!p_state->ms_stack) 4 12 16
1065 4 p_state->ms_stack = newAV();
1066 16 av_push(p_state->ms_stack, newRV_noinc((SV*)tokens));
1067 16 marked_section_update(p_state);
1068 16 report_event(p_state, E_NONE, beg, s, utf8, 0, 0, self);
1069 16 return s;
1070 }
1071  
1072 0 FAIL:
1073 0 SvREFCNT_dec(tokens);
1074 0 return 0; /* not yet implemented */
1075  
1076 0 PREMATURE:
1077 0 SvREFCNT_dec(tokens);
1078 0 return beg;
1079 }
1080 #endif
1081  
1082  
1083 static char*
1084 550 parse_decl(PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
1085 {
1086 550 char *s = beg + 2;
1087  
1088 550 ▶ if (*s == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') { 185 365 550
1089 /* comment? */
1090  
1091 char *tmp;
1092 185 s++;
1093 185 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 6 179 185
1094 6 return beg;
1095  
1096 179 ▶ if (*s != '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*s != '-') 2 177 179
1097 2 goto DECL_FAIL; /* nope, illegal */
1098  
1099 /* yes, two dashes seen */
1100 177 s++;
1101  
1102 177 tmp = parse_comment(p_state, s, end, utf8, self);
1103 177 ▶ return (tmp == s) ? beg : tmp;
Branch: 1 branch
BranchTrueFalseTotal
return (tmp == s) ? beg : tmp; 121 56 177
1104 }
1105  
1106 #ifdef MARKED_SECTION
1107 365 ▶ if (*s == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '[') { 16 349 365
1108 /* marked section */
1109 char *tmp;
1110 16 tmp = parse_marked_section(p_state, beg, end, utf8, self);
1111 16 ▶ if (!tmp)
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp) 0 16 16
1112 0 goto DECL_FAIL;
1113 16 return tmp;
1114 }
1115 #endif
1116  
1117 349 ▶ if (*s == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '>') { 1 348 349
1118 /* make <!> into empty comment <SGML Handbook 36:32> */
1119 token_pos_t token;
1120 1 token.beg = s;
1121 1 token.end = s;
1122 1 s++;
1123 1 report_event(p_state, E_COMMENT, beg, s, utf8, &token, 1, self);
1124 1 return s;
1125 }
1126  
1127 348 ▶ if (isALPHA(*s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isALPHA(*s)) { 298 50 348
1128 298 dTOKENS(8);
1129 298 char *decl_id = s;
1130 STRLEN decl_id_len;
1131  
1132 298 s++;
1133 /* declaration */
1134 1970 ▶ while (s < end && isHNAME_CHAR(*s))
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isHNAME_CHAR(*s)) 1954 16 1970
while (s < end && isHNAME_CHAR(*s)) 1672 282 1954
1135 1672 s++;
1136 298 decl_id_len = s - decl_id;
1137 298 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 16 282 298
1138 16 goto PREMATURE;
1139  
1140 /* just hardcode a few names as the recognized declarations */
1141 548 ▶ if (!((decl_id_len == 7 &&
Branch: 1 branch
BranchTrueFalseTotal
if (!((decl_id_len == 7 && 4 13 17
1142 266 ▶ strnEQx(decl_id, "DOCTYPE", 7, !CASE_SENSITIVE(p_state))) ||
Branch: 2 branches
BranchTrueFalseTotal
strnEQx(decl_id, "DOCTYPE", 7, !CASE_SENSITIVE(p_state))) || 266 0 266
strnEQx(decl_id, "DOCTYPE", 7, !CASE_SENSITIVE(p_state))) || 266 0 266
1143 4 ▶ (decl_id_len == 6 &&
Branch: 1 branch
BranchTrueFalseTotal
(decl_id_len == 6 && 0 4 4
1144 4 ▶ strnEQx(decl_id, "ENTITY", 6, !CASE_SENSITIVE(p_state)))
Branch: 2 branches
BranchTrueFalseTotal
strnEQx(decl_id, "ENTITY", 6, !CASE_SENSITIVE(p_state))) 3 1 4
strnEQx(decl_id, "ENTITY", 6, !CASE_SENSITIVE(p_state))) 3 0 3
1145 )
1146 )
1147 {
1148 13 goto FAIL;
1149 }
1150  
1151 /* first word available */
1152 269 ▶ PUSH_TOKEN(decl_id, s);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_TOKEN(decl_id, s); 0 269 269
1153  
1154 while (1) {
1155 50732 ▶ while (s < end && isHSPACE(*s))
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isHSPACE(*s)) 50567 165 50732
while (s < end && isHSPACE(*s)) 25413 25154 50567
1156 25413 s++;
1157  
1158 25319 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 165 25154 25319
1159 165 goto PREMATURE;
1160  
1161 25154 ▶ if (*s == '"' || *s == '\'' || (*s == '`' && p_state->backquote)) {
Branch: 4 branches
BranchTrueFalseTotal
if (*s == '"' || *s == '\'' || (*s == '`' && p_state->backquote)) { 25025 129 25154
if (*s == '"' || *s == '\'' || (*s == '`' && p_state->backquote)) { 25025 0 25025
if (*s == '"' || *s == '\'' || (*s == '`' && p_state->backquote)) { 0 25025 25025
if (*s == '"' || *s == '\'' || (*s == '`' && p_state->backquote)) { 0 0 0
1162 129 char *str_beg = s;
1163 129 s++;
1164 2914 ▶ while (s < end && *s != *str_beg)
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && *s != *str_beg) 2849 65 2914
while (s < end && *s != *str_beg) 2785 64 2849
1165 2785 s++;
1166 129 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 65 64 129
1167 65 goto PREMATURE;
1168 64 s++;
1169 64 ▶ PUSH_TOKEN(str_beg, s);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_TOKEN(str_beg, s); 0 64 64
1170 }
1171 25025 ▶ else if (*s == '-') {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '-') { 12405 12620 25025
1172 /* comment */
1173 12405 char *com_beg = s;
1174 12405 s++;
1175 12405 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 0 12405 12405
1176 0 goto PREMATURE;
1177 12405 ▶ if (*s != '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*s != '-') 0 12405 12405
1178 0 goto FAIL;
1179 12405 s++;
1180  
1181 while (1) {
1182 62054 ▶ while (s < end && *s != '-')
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && *s != '-') 62054 0 62054
while (s < end && *s != '-') 49647 12407 62054
1183 49647 s++;
1184 12407 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 0 12407 12407
1185 0 goto PREMATURE;
1186 12407 s++;
1187 12407 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 0 12407 12407
1188 0 goto PREMATURE;
1189 12407 ▶ if (*s == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') { 12405 2 12407
1190 12405 s++;
1191 12405 ▶ PUSH_TOKEN(com_beg, s);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_TOKEN(com_beg, s); 0 12405 12405
1192 12405 break;
1193 }
1194 }
1195 }
1196 12620 ▶ else if (*s != '>') {
Branch: 1 branch
BranchTrueFalseTotal
else if (*s != '>') { 12598 22 12620
1197 /* plain word */
1198 12598 char *word_beg = s;
1199 12598 s++;
1200 25722 ▶ while (s < end && isHNOT_SPACE_GT(*s))
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isHNOT_SPACE_GT(*s)) 25705 17 25722
while (s < end && isHNOT_SPACE_GT(*s)) 13124 12581 25705
1201 13124 s++;
1202 12598 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 17 12581 12598
1203 17 goto PREMATURE;
1204 12581 ▶ PUSH_TOKEN(word_beg, s);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_TOKEN(word_beg, s); 696 11885 12581
1205 }
1206 else {
1207 22 break;
1208 }
1209 }
1210  
1211 22 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 0 22 22
1212 0 goto PREMATURE;
1213 22 ▶ if (*s == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '>') { 22 0 22
1214 22 s++;
1215 22 report_event(p_state, E_DECLARATION, beg, s, utf8, tokens, num_tokens, self);
1216 22 ▶ FREE_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
FREE_TOKENS; 1 21 22
1217 285 return s;
1218 }
1219  
1220 0 FAIL:
1221 13 ▶ FREE_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
FREE_TOKENS; 0 13 13
1222 13 goto DECL_FAIL;
1223  
1224 263 PREMATURE:
1225 263 ▶ FREE_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
FREE_TOKENS; 153 110 263
1226 263 return beg;
1227  
1228 }
1229  
1230 50 DECL_FAIL:
1231 65 ▶ if (p_state->strict_comment)
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->strict_comment) 0 65 65
1232 0 return 0;
1233  
1234 /* consider everything up to the first '>' a comment */
1235 13622 ▶ while (s < end && *s != '>')
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && *s != '>') 13605 17 13622
while (s < end && *s != '>') 13557 48 13605
1236 13557 s++;
1237 65 ▶ if (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s < end) { 48 17 65
1238 token_pos_t token;
1239 48 token.beg = beg + 2;
1240 48 token.end = s;
1241 48 s++;
1242 48 report_event(p_state, E_COMMENT, beg, s, utf8, &token, 1, self);
1243 48 return s;
1244 }
1245 else {
1246 17 return beg;
1247 }
1248 }
1249  
1250  
1251 static char*
1252 6838 parse_start(PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
1253 {
1254 6838 char *s = beg;
1255 6838 int empty_tag = 0;
1256 6838 dTOKENS(16);
1257  
1258 hctype_t tag_name_char;
1259 hctype_t attr_name_first, attr_name_char;
1260  
1261 6838 ▶ if (STRICT_NAMES(p_state)) {
Branch: 2 branches
BranchTrueFalseTotal
if (STRICT_NAMES(p_state)) { 6818 20 6838
if (STRICT_NAMES(p_state)) { 0 6818 6818
1262 20 attr_name_first = HCTYPE_NAME_FIRST;
1263 20 tag_name_char = attr_name_char = HCTYPE_NAME_CHAR;
1264 }
1265 else {
1266 6818 tag_name_char = HCTYPE_NOT_SPACE_GT;
1267 6818 attr_name_first = HCTYPE_NOT_SPACE_GT;
1268 6818 attr_name_char = HCTYPE_NOT_SPACE_EQ_GT;
1269 }
1270  
1271 6838 s += 2;
1272  
1273 110778 ▶ while (s < end && isHCTYPE(*s, tag_name_char)) {
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isHCTYPE(*s, tag_name_char)) { 110555 223 110778
while (s < end && isHCTYPE(*s, tag_name_char)) { 103940 6615 110555
1274 103940 ▶ if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) {
Branch: 3 branches
BranchTrueFalseTotal
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 370 103570 103940
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 370 0 370
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 0 370 370
1275 0 ▶ if ((s + 1) == end)
Branch: 1 branch
BranchTrueFalseTotal
if ((s + 1) == end) 0 0 0
1276 0 goto PREMATURE;
1277 0 ▶ if (*(s + 1) == '>')
Branch: 1 branch
BranchTrueFalseTotal
if (*(s + 1) == '>') 0 0 0
1278 0 break;
1279 }
1280 103940 s++;
1281 }
1282 6838 ▶ PUSH_TOKEN(beg+1, s); /* tagname */
Branch: 1 branch
BranchTrueFalseTotal
PUSH_TOKEN(beg+1, s); /* tagname */ 0 6838 6838
1283  
1284 12552 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 5714 6838 12552
1285 5714 s++;
1286 6838 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 250 6588 6838
1287 250 goto PREMATURE;
1288  
1289 35066 ▶ while (isHCTYPE(*s, attr_name_first)) {
Branch: 1 branch
BranchTrueFalseTotal
while (isHCTYPE(*s, attr_name_first)) { 29911 5155 35066
1290 /* attribute */
1291 29911 char *attr_name_beg = s;
1292 char *attr_name_end;
1293 29911 ▶ if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) {
Branch: 3 branches
BranchTrueFalseTotal
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 70 29841 29911
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 70 0 70
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 4 66 70
1294 4 ▶ if ((s + 1) == end)
Branch: 1 branch
BranchTrueFalseTotal
if ((s + 1) == end) 0 4 4
1295 0 goto PREMATURE;
1296 4 ▶ if (*(s + 1) == '>')
Branch: 1 branch
BranchTrueFalseTotal
if (*(s + 1) == '>') 4 0 4
1297 4 break;
1298 }
1299 29907 s++;
1300 480622 ▶ while (s < end && isHCTYPE(*s, attr_name_char)) {
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isHCTYPE(*s, attr_name_char)) { 479814 808 480622
while (s < end && isHCTYPE(*s, attr_name_char)) { 450715 29099 479814
1301 450715 ▶ if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) {
Branch: 3 branches
BranchTrueFalseTotal
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 1686 449029 450715
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 1686 0 1686
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 0 1686 1686
1302 0 ▶ if ((s + 1) == end)
Branch: 1 branch
BranchTrueFalseTotal
if ((s + 1) == end) 0 0 0
1303 0 goto PREMATURE;
1304 0 ▶ if (*(s + 1) == '>')
Branch: 1 branch
BranchTrueFalseTotal
if (*(s + 1) == '>') 0 0 0
1305 0 break;
1306 }
1307 450715 s++;
1308 }
1309 29907 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 808 29099 29907
1310 808 goto PREMATURE;
1311  
1312 29099 attr_name_end = s;
1313 29099 ▶ PUSH_TOKEN(attr_name_beg, attr_name_end); /* attr name */
Branch: 1 branch
BranchTrueFalseTotal
PUSH_TOKEN(attr_name_beg, attr_name_end); /* attr name */ 1311 27788 29099
1314  
1315 39803 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 10704 29099 39803
1316 10704 s++;
1317 29099 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 15 29084 29099
1318 15 goto PREMATURE;
1319  
1320 29084 ▶ if (*s == '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '=') { 17197 11887 29084
1321 /* with a value */
1322 17197 s++;
1323 17256 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 59 17197 17256
1324 59 s++;
1325 17197 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 31 17166 17197
1326 31 goto PREMATURE;
1327 17166 ▶ if (*s == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '>') { 2 17164 17166
1328 /* parse it similar to ="" */
1329 2 ▶ PUSH_TOKEN(s, s);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_TOKEN(s, s); 0 2 2
1330 2 break;
1331 }
1332 17164 ▶ if (*s == '"' || *s == '\'' || (*s == '`' && p_state->backquote)) {
Branch: 4 branches
BranchTrueFalseTotal
if (*s == '"' || *s == '\'' || (*s == '`' && p_state->backquote)) { 16334 830 17164
if (*s == '"' || *s == '\'' || (*s == '`' && p_state->backquote)) { 14184 2150 16334
if (*s == '"' || *s == '\'' || (*s == '`' && p_state->backquote)) { 11 14173 14184
if (*s == '"' || *s == '\'' || (*s == '`' && p_state->backquote)) { 1 10 11
1333 2981 char *str_beg = s;
1334 2981 s++;
1335 33455 ▶ while (s < end && *s != *str_beg)
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && *s != *str_beg) 33176 279 33455
while (s < end && *s != *str_beg) 30474 2702 33176
1336 30474 s++;
1337 2981 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 279 2702 2981
1338 279 goto PREMATURE;
1339 2702 s++;
1340 2702 ▶ PUSH_TOKEN(str_beg, s);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_TOKEN(str_beg, s); 0 2702 2702
1341 }
1342 else {
1343 14183 char *word_start = s;
1344 85745 ▶ while (s < end && isHNOT_SPACE_GT(*s)) {
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isHNOT_SPACE_GT(*s)) { 85646 99 85745
while (s < end && isHNOT_SPACE_GT(*s)) { 71564 14082 85646
1345 71564 ▶ if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) {
Branch: 3 branches
BranchTrueFalseTotal
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 269 71295 71564
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 268 1 269
if (*s == '/' && ALLOW_EMPTY_TAG(p_state)) { 1 267 268
1346 2 ▶ if ((s + 1) == end)
Branch: 1 branch
BranchTrueFalseTotal
if ((s + 1) == end) 0 2 2
1347 0 goto PREMATURE;
1348 2 ▶ if (*(s + 1) == '>')
Branch: 1 branch
BranchTrueFalseTotal
if (*(s + 1) == '>') 2 0 2
1349 2 break;
1350 }
1351 71562 s++;
1352 }
1353 14183 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 99 14084 14183
1354 99 goto PREMATURE;
1355 14084 ▶ PUSH_TOKEN(word_start, s);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_TOKEN(word_start, s); 0 14084 14084
1356 }
1357 30929 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 14143 16786 30929
1358 14143 s++;
1359 16786 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 195 16591 16786
1360 195 goto PREMATURE;
1361 }
1362 else {
1363 11887 ▶ PUSH_TOKEN(0, 0); /* boolean attr value */
Branch: 1 branch
BranchTrueFalseTotal
PUSH_TOKEN(0, 0); /* boolean attr value */ 0 11887 11887
1364 }
1365 }
1366  
1367 5161 ▶ if (ALLOW_EMPTY_TAG(p_state) && *s == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (ALLOW_EMPTY_TAG(p_state) && *s == '/') { 5141 20 5161
if (ALLOW_EMPTY_TAG(p_state) && *s == '/') { 605 4536 5141
if (ALLOW_EMPTY_TAG(p_state) && *s == '/') { 7 618 625
1368 7 s++;
1369 7 ▶ if (s == end)
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) 0 7 7
1370 0 goto PREMATURE;
1371 7 empty_tag = 1;
1372 }
1373  
1374 5161 ▶ if (*s == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '>') { 5161 0 5161
1375 5161 s++;
1376 /* done */
1377 5161 report_event(p_state, E_START, beg, s, utf8, tokens, num_tokens, self);
1378 5161 ▶ if (empty_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (empty_tag) { 7 5154 5161
1379 7 report_event(p_state, E_END, s, s, utf8, tokens, 1, self);
1380 }
1381 5154 ▶ else if (!p_state->xml_mode) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!p_state->xml_mode) { 5137 17 5154
1382 /* find out if this start tag should put us into literal_mode
1383 */
1384 int i;
1385 5137 int tag_len = tokens[0].end - tokens[0].beg;
1386  
1387 40808 ▶ for (i = 0; literal_mode_elem[i].len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; literal_mode_elem[i].len; i++) { 35776 5032 40808
1388 35776 ▶ if (tag_len == literal_mode_elem[i].len) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag_len == literal_mode_elem[i].len) { 598 35178 35776
1389 /* try to match it */
1390 598 char *s = beg + 1;
1391 598 char *t = literal_mode_elem[i].str;
1392 598 int len = tag_len;
1393 1055 ▶ while (len) {
Branch: 1 branch
BranchTrueFalseTotal
while (len) { 1055 0 1055
1394 1055 ▶ if (toLOWER(*s) != *t)
Branch: 2 branches
BranchTrueFalseTotal
if (toLOWER(*s) != *t) 195 860 1055
if (toLOWER(*s) != *t) 493 562 1055
1395 493 break;
1396 562 s++;
1397 562 t++;
1398 562 ▶ if (!--len) {
Branch: 1 branch
BranchTrueFalseTotal
if (!--len) { 105 457 562
1399 /* found it */
1400 105 p_state->literal_mode = literal_mode_elem[i].str;
1401 105 p_state->is_cdata = literal_mode_elem[i].is_cdata;
1402 /* printf("Found %s\n", p_state->literal_mode); */
1403 105 goto END_OF_LITERAL_SEARCH;
1404 }
1405 }
1406 }
1407 }
1408 5137 END_OF_LITERAL_SEARCH:
1409 ;
1410 }
1411  
1412 5161 ▶ FREE_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
FREE_TOKENS; 452 4709 5161
1413 5161 return s;
1414 }
1415  
1416 0 ▶ FREE_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
FREE_TOKENS; 0 0 0
1417 0 return 0;
1418  
1419 1677 PREMATURE:
1420 1677 ▶ FREE_TOKENS;
Branch: 1 branch
BranchTrueFalseTotal
FREE_TOKENS; 319 1358 1677
1421 1677 return beg;
1422 }
1423  
1424  
1425 static char*
1426 2947 parse_end(PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
1427 {
1428 2947 char *s = beg+2;
1429 hctype_t name_first, name_char;
1430  
1431 2947 ▶ if (STRICT_NAMES(p_state)) {
Branch: 2 branches
BranchTrueFalseTotal
if (STRICT_NAMES(p_state)) { 2930 17 2947
if (STRICT_NAMES(p_state)) { 0 2930 2930
1432 17 name_first = HCTYPE_NAME_FIRST;
1433 17 name_char = HCTYPE_NAME_CHAR;
1434 }
1435 else {
1436 2930 name_first = name_char = HCTYPE_NOT_SPACE_GT;
1437 }
1438  
1439 2947 ▶ if (isHCTYPE(*s, name_first)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isHCTYPE(*s, name_first)) { 2945 2 2947
1440 token_pos_t tagname;
1441 2945 tagname.beg = s;
1442 2945 s++;
1443 5591 ▶ while (s < end && isHCTYPE(*s, name_char))
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isHCTYPE(*s, name_char)) 5543 48 5591
while (s < end && isHCTYPE(*s, name_char)) 2646 2897 5543
1444 2646 s++;
1445 2945 tagname.end = s;
1446  
1447 2945 ▶ if (p_state->strict_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->strict_end) { 0 2945 2945
1448 0 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 0 0 0
1449 0 s++;
1450 }
1451 else {
1452 2945 s = skip_until_gt(s, end);
1453 }
1454 2945 ▶ if (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s < end) { 2881 64 2945
1455 2881 ▶ if (*s == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '>') { 2881 0 2881
1456 2881 s++;
1457 /* a complete end tag has been recognized */
1458 2881 report_event(p_state, E_END, beg, s, utf8, &tagname, 1, self);
1459 2944 return s;
1460 }
1461 }
1462 else {
1463 64 return beg;
1464 }
1465 }
1466 2 ▶ else if (!p_state->strict_comment) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!p_state->strict_comment) { 2 0 2
1467 2 s = skip_until_gt(s, end);
1468 2 ▶ if (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s < end) { 2 0 2
1469 token_pos_t token;
1470 2 token.beg = beg + 2;
1471 2 token.end = s;
1472 2 s++;
1473 2 report_event(p_state, E_COMMENT, beg, s, utf8, &token, 1, self);
1474 2 return s;
1475 }
1476 else {
1477 0 return beg;
1478 }
1479 }
1480 0 return 0;
1481 }
1482  
1483  
1484 static char*
1485 83 parse_process(PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
1486 {
1487 83 char *s = beg + 2; /* skip '<?' */
1488 /* processing instruction */
1489 token_pos_t token_pos;
1490 83 token_pos.beg = s;
1491  
1492 14067 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 14040 27 14067
1493 14040 ▶ if (*s == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '>') { 58 13982 14040
1494 58 token_pos.end = s;
1495 58 s++;
1496  
1497 58 ▶ if (p_state->xml_mode || p_state->xml_pic) {
Branch: 2 branches
BranchTrueFalseTotal
if (p_state->xml_mode || p_state->xml_pic) { 52 6 58
if (p_state->xml_mode || p_state->xml_pic) { 2 50 52
1498 /* XML processing instructions are ended by "?>" */
1499 8 ▶ if (s - beg < 4 || s[-2] != '?')
Branch: 2 branches
BranchTrueFalseTotal
if (s - beg < 4 || s[-2] != '?') 8 0 8
if (s - beg < 4 || s[-2] != '?') 2 6 8
1500 2 continue;
1501 6 token_pos.end = s - 2;
1502 }
1503  
1504 /* a complete processing instruction seen */
1505 56 report_event(p_state, E_PROCESS, beg, s, utf8,
1506 &token_pos, 1, self);
1507 56 return s;
1508 }
1509 13982 s++;
1510 }
1511 27 return beg; /* could not find end */
1512 }
1513  
1514  
1515 #ifdef USE_PFUNC
1516 static char*
1517 parse_null(PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
1518 {
1519 return 0;
1520 }
1521  
1522  
1523  
1524 #include "pfunc.h" /* declares the parsefunc[] */
1525 #endif /* USE_PFUNC */
1526  
1527 static char*
1528 7257 parse_buf(pTHX_ PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
1529 {
1530 7257 char *s = beg;
1531 7257 char *t = beg;
1532 char *new_pos;
1533  
1534 23534 ▶ while (!p_state->eof) {
Branch: 1 branch
BranchTrueFalseTotal
while (!p_state->eof) { 23528 6 23534
1535 /*
1536 * At the start of this loop we will always be ready for eating text
1537 * or a new tag. We will never be inside some tag. The 't' points
1538 * to where we started and the 's' is advanced as we go.
1539 */
1540  
1541 47224 ▶ while (p_state->literal_mode) {
Branch: 1 branch
BranchTrueFalseTotal
while (p_state->literal_mode) { 287 23409 23696
1542 287 char *l = p_state->literal_mode;
1543 char *end_text;
1544  
1545 2705 ▶ while (s < end && *s != '<') {
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && *s != '<') { 2586 119 2705
while (s < end && *s != '<') { 2418 168 2586
1546 2418 s++;
1547 }
1548  
1549 287 ▶ if (s == end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) { 119 168 287
1550 119 s = t;
1551 119 goto DONE;
1552 }
1553  
1554 168 end_text = s;
1555 168 s++;
1556  
1557 /* here we rely on '\0' termination of perl svpv buffers */
1558 168 ▶ if (*s == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') { 147 21 168
1559 147 s++;
1560 777 ▶ while (*l && toLOWER(*s) == *l) {
Branch: 3 branches
BranchTrueFalseTotal
while (*l && toLOWER(*s) == *l) { 666 111 777
while (*l && toLOWER(*s) == *l) { 15 651 666
while (*l && toLOWER(*s) == *l) { 630 36 666
1561 630 s++;
1562 630 l++;
1563 }
1564  
1565 147 ▶ if (!*l && (strNE(p_state->literal_mode, "plaintext") || p_state->closing_plaintext)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!*l && (strNE(p_state->literal_mode, "plaintext") || p_state->closing_plaintext)) { 111 36 147
if (!*l && (strNE(p_state->literal_mode, "plaintext") || p_state->closing_plaintext)) { 2 109 111
if (!*l && (strNE(p_state->literal_mode, "plaintext") || p_state->closing_plaintext)) { 1 1 2
1566 /* matched it all */
1567 token_pos_t end_token;
1568 110 end_token.beg = end_text + 2;
1569 110 end_token.end = s;
1570  
1571 110 ▶ while (isHSPACE(*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isHSPACE(*s)) 0 110 110
1572 0 s++;
1573 110 ▶ if (*s == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '>') { 101 9 110
1574 101 s++;
1575 101 ▶ if (t != end_text)
Branch: 1 branch
BranchTrueFalseTotal
if (t != end_text) 101 0 101
1576 101 report_event(p_state, E_TEXT, t, end_text, utf8,
1577 0, 0, self);
1578 101 report_event(p_state, E_END, end_text, s, utf8,
1579 &end_token, 1, self);
1580 101 p_state->literal_mode = 0;
1581 101 p_state->is_cdata = 0;
1582 101 t = s;
1583 }
1584 }
1585 }
1586 }
1587  
1588 #ifdef MARKED_SECTION
1589 23418 ▶ while (p_state->ms == MS_CDATA || p_state->ms == MS_RCDATA) {
Branch: 2 branches
BranchTrueFalseTotal
while (p_state->ms == MS_CDATA || p_state->ms == MS_RCDATA) { 8 23410 23418
while (p_state->ms == MS_CDATA || p_state->ms == MS_RCDATA) { 2 23408 23410
1590 116 ▶ while (s < end && *s != ']')
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && *s != ']') 115 1 116
while (s < end && *s != ']') 106 9 115
1591 106 s++;
1592 10 ▶ if (*s == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == ']') { 9 1 10
1593 9 char *end_text = s;
1594 9 s++;
1595 9 ▶ if (*s == ']' && *(s + 1) == '>') {
Branch: 2 branches
BranchTrueFalseTotal
if (*s == ']' && *(s + 1) == '>') { 9 0 9
if (*s == ']' && *(s + 1) == '>') { 8 1 9
1596 8 s += 2;
1597 /* marked section end */
1598 8 ▶ if (t != end_text)
Branch: 1 branch
BranchTrueFalseTotal
if (t != end_text) 8 0 8
1599 8 report_event(p_state, E_TEXT, t, end_text, utf8,
1600 0, 0, self);
1601 8 report_event(p_state, E_NONE, end_text, s, utf8, 0, 0, self);
1602 8 t = s;
1603 8 SvREFCNT_dec(av_pop(p_state->ms_stack));
1604 8 marked_section_update(p_state);
1605 8 continue;
1606 }
1607 }
1608 2 ▶ if (s == end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s == end) { 1 1 2
1609 1 s = t;
1610 1 goto DONE;
1611 }
1612 }
1613 #endif
1614  
1615 /* first we try to match as much text as possible */
1616 2212685 ▶ while (s < end && *s != '<') {
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && *s != '<') { 2207937 4748 2212685
while (s < end && *s != '<') { 2189277 18660 2207937
1617 #ifdef MARKED_SECTION
1618 2189277 ▶ if (p_state->ms && *s == ']') {
Branch: 2 branches
BranchTrueFalseTotal
if (p_state->ms && *s == ']') { 65 2189212 2189277
if (p_state->ms && *s == ']') { 8 57 65
1619 8 char *end_text = s;
1620 8 s++;
1621 8 ▶ if (*s == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == ']') { 8 0 8
1622 8 s++;
1623 8 ▶ if (*s == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '>') { 8 0 8
1624 8 s++;
1625 8 report_event(p_state, E_TEXT, t, end_text, utf8,
1626 0, 0, self);
1627 8 report_event(p_state, E_NONE, end_text, s, utf8,
1628 0, 0, self);
1629 8 t = s;
1630 8 SvREFCNT_dec(av_pop(p_state->ms_stack));
1631 8 marked_section_update(p_state);
1632 8 continue;
1633 }
1634 }
1635 }
1636 #endif
1637 2189269 s++;
1638 }
1639 23408 ▶ if (s != t) {
Branch: 1 branch
BranchTrueFalseTotal
if (s != t) { 19431 3977 23408
1640 19431 ▶ if (*s == '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '<') { 14845 4586 19431
1641 14845 report_event(p_state, E_TEXT, t, s, utf8, 0, 0, self);
1642 14845 t = s;
1643 }
1644 else {
1645 4586 s--;
1646 4586 ▶ if (isHSPACE(*s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isHSPACE(*s)) { 349 4237 4586
1647 /* wait with white space at end */
1648 760 ▶ while (s >= t && isHSPACE(*s))
Branch: 2 branches
BranchTrueFalseTotal
while (s >= t && isHSPACE(*s)) 627 133 760
while (s >= t && isHSPACE(*s)) 411 216 627
1649 411 s--;
1650 }
1651 else {
1652 /* might be a chopped up entities/words */
1653 143719 ▶ while (s >= t && !isHSPACE(*s))
Branch: 2 branches
BranchTrueFalseTotal
while (s >= t && !isHSPACE(*s)) 142977 742 143719
while (s >= t && !isHSPACE(*s)) 139482 3495 142977
1654 139482 s--;
1655 7825 ▶ while (s >= t && isHSPACE(*s))
Branch: 2 branches
BranchTrueFalseTotal
while (s >= t && isHSPACE(*s)) 6835 990 7825
while (s >= t && isHSPACE(*s)) 3588 3247 6835
1656 3588 s--;
1657 }
1658 4586 s++;
1659 4586 ▶ if (s != t)
Branch: 1 branch
BranchTrueFalseTotal
if (s != t) 3463 1123 4586
1660 3463 report_event(p_state, E_TEXT, t, s, utf8, 0, 0, self);
1661 4586 break;
1662 }
1663 }
1664  
1665 18822 ▶ if (end - s < 3)
Branch: 1 branch
BranchTrueFalseTotal
if (end - s < 3) 369 18453 18822
1666 369 break;
1667  
1668 /* next char is known to be '<' and pointed to by 't' as well as 's' */
1669 18453 s++;
1670  
1671 #ifdef USE_PFUNC
1672 new_pos = parsefunc[(unsigned char)*s](p_state, t, end, utf8, self);
1673 #else
1674 18453 ▶ if (isHNAME_FIRST(*s))
Branch: 1 branch
BranchTrueFalseTotal
if (isHNAME_FIRST(*s)) 6838 11615 18453
1675 6838 new_pos = parse_start(p_state, t, end, utf8, self);
1676 11615 ▶ else if (*s == '/')
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '/') 2947 8668 11615
1677 2947 new_pos = parse_end(p_state, t, end, utf8, self);
1678 8668 ▶ else if (*s == '!')
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '!') 550 8118 8668
1679 550 new_pos = parse_decl(p_state, t, end, utf8, self);
1680 8118 ▶ else if (*s == '?')
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '?') 83 8035 8118
1681 83 new_pos = parse_process(p_state, t, end, utf8, self);
1682 else
1683 8035 new_pos = 0;
1684 #endif /* USE_PFUNC */
1685  
1686 18452 ▶ if (new_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_pos) { 10417 8035 18452
1687 10417 ▶ if (new_pos == t) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_pos == t) { 2175 8242 10417
1688 /* no progress, need more data to know what it is */
1689 2175 s = t;
1690 2175 break;
1691 }
1692 8242 t = s = new_pos;
1693 }
1694  
1695 /* if we get out here then this was not a conforming tag, so
1696 * treat it is plain text at the top of the loop again (we
1697 * have already skipped past the "<").
1698 */
1699 }
1700  
1701 6 DONE:
1702 7256 return s;
1703  
1704 }
1705  
1706 EXTERN void
1707 7455 parse(pTHX_
1708 PSTATE* p_state,
1709 SV* chunk,
1710 SV* self)
1711 {
1712 char *s, *beg, *end;
1713 7455 U32 utf8 = 0;
1714 STRLEN len;
1715  
1716 7455 ▶ if (!p_state->start_document) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p_state->start_document) { 211 7244 7455
1717 char dummy[1];
1718 211 report_event(p_state, E_START_DOCUMENT, dummy, dummy, 0, 0, 0, self);
1719 211 p_state->start_document = 1;
1720 }
1721  
1722 7455 ▶ if (!chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if (!chunk) { 197 7258 7455
1723 /* eof */
1724 char empty[1];
1725 197 ▶ if (p_state->buf && SvOK(p_state->buf)) {
Branch: 2 branches
BranchTrueFalseTotal
if (p_state->buf && SvOK(p_state->buf)) { 151 46 197
if (p_state->buf && SvOK(p_state->buf)) { 149 2 151
1726 /* flush it */
1727 149 s = SvPV(p_state->buf, len);
1728 149 end = s + len;
1729 149 utf8 = SvUTF8(p_state->buf);
1730 assert(len);
1731  
1732 152 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 152 0 152
1733 152 ▶ if (p_state->literal_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->literal_mode) { 2 150 152
1734 2 ▶ if (strEQ(p_state->literal_mode, "plaintext") ||
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(p_state->literal_mode, "plaintext") || 0 2 2
1735 0 ▶ strEQ(p_state->literal_mode, "xmp") ||
Branch: 1 branch
BranchTrueFalseTotal
strEQ(p_state->literal_mode, "xmp") || 0 0 0
1736 0 ▶ strEQ(p_state->literal_mode, "iframe") ||
Branch: 1 branch
BranchTrueFalseTotal
strEQ(p_state->literal_mode, "iframe") || 0 0 0
1737 0 ▶ strEQ(p_state->literal_mode, "textarea"))
Branch: 1 branch
BranchTrueFalseTotal
strEQ(p_state->literal_mode, "textarea")) 0 0 0
1738 {
1739 /* rest is considered text */
1740 break;
1741 }
1742 0 ▶ if (strEQ(p_state->literal_mode, "script") ||
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(p_state->literal_mode, "script") || 0 0 0
1743 0 ▶ strEQ(p_state->literal_mode, "style"))
Branch: 1 branch
BranchTrueFalseTotal
strEQ(p_state->literal_mode, "style")) 0 0 0
1744 0 {
1745 /* effectively make it an empty element */
1746 token_pos_t t;
1747 char dummy;
1748 0 t.beg = p_state->literal_mode;
1749 0 t.end = p_state->literal_mode + strlen(p_state->literal_mode);
1750 0 report_event(p_state, E_END, &dummy, &dummy, 0, &t, 1, self);
1751 }
1752 else {
1753 0 p_state->pending_end_tag = p_state->literal_mode;
1754 }
1755 0 p_state->literal_mode = 0;
1756 0 s = parse_buf(aTHX_ p_state, s, end, utf8, self);
1757 0 continue;
1758 }
1759  
1760 150 ▶ if (!p_state->strict_comment && !p_state->no_dash_dash_comment_end && *s == '<') {
Branch: 3 branches
BranchTrueFalseTotal
if (!p_state->strict_comment && !p_state->no_dash_dash_comment_end && *s == '<') { 135 15 150
if (!p_state->strict_comment && !p_state->no_dash_dash_comment_end && *s == '<') { 132 3 135
if (!p_state->strict_comment && !p_state->no_dash_dash_comment_end && *s == '<') { 3 129 132
1761 3 p_state->no_dash_dash_comment_end = 1;
1762 3 s = parse_buf(aTHX_ p_state, s, end, utf8, self);
1763 3 continue;
1764 }
1765  
1766 147 ▶ if (!p_state->strict_comment && *s == '<') {
Branch: 2 branches
BranchTrueFalseTotal
if (!p_state->strict_comment && *s == '<') { 132 15 147
if (!p_state->strict_comment && *s == '<') { 3 129 132
1767 3 char *s1 = s + 1;
1768 3 ▶ if (s1 == end || isHNAME_FIRST(*s1) || *s1 == '/' || *s1 == '!' || *s1 == '?') {
Branch: 5 branches
BranchTrueFalseTotal
if (s1 == end || isHNAME_FIRST(*s1) || *s1 == '/' || *s1 == '!' || *s1 == '?') { 2 1 3
if (s1 == end || isHNAME_FIRST(*s1) || *s1 == '/' || *s1 == '!' || *s1 == '?') { 1 1 2
if (s1 == end || isHNAME_FIRST(*s1) || *s1 == '/' || *s1 == '!' || *s1 == '?') { 1 0 1
if (s1 == end || isHNAME_FIRST(*s1) || *s1 == '/' || *s1 == '!' || *s1 == '?') { 1 0 1
if (s1 == end || isHNAME_FIRST(*s1) || *s1 == '/' || *s1 == '!' || *s1 == '?') { 0 1 1
1769 /* some kind of unterminated markup. Report rest as as comment */
1770 token_pos_t token;
1771 2 token.beg = s + 1;
1772 2 token.end = end;
1773 2 report_event(p_state, E_COMMENT, s, end, utf8, &token, 1, self);
1774 2 s = end;
1775 }
1776 }
1777  
1778 147 break;
1779 }
1780  
1781 149 ▶ if (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s < end) { 147 2 149
1782 /* report rest as text */
1783 147 report_event(p_state, E_TEXT, s, end, utf8, 0, 0, self);
1784 }
1785  
1786 149 SvREFCNT_dec(p_state->buf);
1787 149 p_state->buf = 0;
1788 }
1789 197 ▶ if (p_state->pend_text && SvOK(p_state->pend_text))
Branch: 2 branches
BranchTrueFalseTotal
if (p_state->pend_text && SvOK(p_state->pend_text)) 60 137 197
if (p_state->pend_text && SvOK(p_state->pend_text)) 60 0 60
1790 60 flush_pending_text(p_state, self);
1791  
1792 197 ▶ if (p_state->ignoring_element) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->ignoring_element) { 0 197 197
1793 /* document not balanced */
1794 0 SvREFCNT_dec(p_state->ignoring_element);
1795 0 p_state->ignoring_element = 0;
1796 }
1797 197 report_event(p_state, E_END_DOCUMENT, empty, empty, 0, 0, 0, self);
1798  
1799 /* reset state */
1800 197 p_state->offset = 0;
1801 197 ▶ if (p_state->line)
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->line) 10 187 197
1802 10 p_state->line = 1;
1803 197 p_state->column = 0;
1804 197 p_state->start_document = 0;
1805 197 p_state->literal_mode = 0;
1806 197 p_state->is_cdata = 0;
1807 197 return;
1808 }
1809  
1810 7258 ▶ if (p_state->utf8_mode)
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->utf8_mode) 5 7253 7258
1811 5 sv_utf8_downgrade(chunk, 0);
1812  
1813 7258 ▶ if (p_state->buf && SvOK(p_state->buf)) {
Branch: 2 branches
BranchTrueFalseTotal
if (p_state->buf && SvOK(p_state->buf)) { 7027 231 7258
if (p_state->buf && SvOK(p_state->buf)) { 6926 101 7027
1814 6926 sv_catsv(p_state->buf, chunk);
1815 6926 beg = SvPV(p_state->buf, len);
1816 6926 utf8 = SvUTF8(p_state->buf);
1817 }
1818 else {
1819 332 beg = SvPV(chunk, len);
1820 332 utf8 = SvUTF8(chunk);
1821 332 ▶ if (p_state->offset == 0 && DOWARN) {
Branch: 2 branches
BranchTrueFalseTotal
if (p_state->offset == 0 && DOWARN) { 211 121 332
if (p_state->offset == 0 && DOWARN) { 15 196 211
1822 /* Print warnings if we find unexpected Unicode BOM forms */
1823 15 ▶ if (p_state->argspec_entity_decode &&
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->argspec_entity_decode && 14 1 15
1824 14 ▶ !(p_state->attr_encoded && p_state->argspec_entity_decode == ARG_ATTR) &&
Branch: 2 branches
BranchTrueFalseTotal
!(p_state->attr_encoded && p_state->argspec_entity_decode == ARG_ATTR) && 1 13 14
!(p_state->attr_encoded && p_state->argspec_entity_decode == ARG_ATTR) && 0 1 1
1825 13 ▶ !p_state->utf8_mode && (
Branch: 2 branches
BranchTrueFalseTotal
!p_state->utf8_mode && ( 10 3 13
!p_state->utf8_mode && ( 7 3 10
1826 10 ▶ (!utf8 && len >= 3 && strnEQ(beg, "\xEF\xBB\xBF", 3)) ||
Branch: 3 branches
BranchTrueFalseTotal
(!utf8 && len >= 3 && strnEQ(beg, "\xEF\xBB\xBF", 3)) || 7 0 7
(!utf8 && len >= 3 && strnEQ(beg, "\xEF\xBB\xBF", 3)) || 5 2 7
(!utf8 && len >= 3 && strnEQ(beg, "\xEF\xBB\xBF", 3)) || 3 5 8
1827 8 ▶ (utf8 && len >= 6 && strnEQ(beg, "\xC3\xAF\xC2\xBB\xC2\xBF", 6)) ||
Branch: 3 branches
BranchTrueFalseTotal
(utf8 && len >= 6 && strnEQ(beg, "\xC3\xAF\xC2\xBB\xC2\xBF", 6)) || 3 0 3
(utf8 && len >= 6 && strnEQ(beg, "\xC3\xAF\xC2\xBB\xC2\xBF", 6)) || 2 1 3
(utf8 && len >= 6 && strnEQ(beg, "\xC3\xAF\xC2\xBB\xC2\xBF", 6)) || 5 2 7
1828 5 ▶ (!utf8 && probably_utf8_chunk(aTHX_ beg, len))
Branch: 1 branch
BranchTrueFalseTotal
(!utf8 && probably_utf8_chunk(aTHX_ beg, len)) 1 4 5
1829 )
1830 )
1831 {
1832 4 warn("Parsing of undecoded UTF-8 will give garbage when decoding entities");
1833 }
1834 15 ▶ if (utf8 && len >= 2 && strnEQ(beg, "\xFF\xFE", 2)) {
Branch: 3 branches
BranchTrueFalseTotal
if (utf8 && len >= 2 && strnEQ(beg, "\xFF\xFE", 2)) { 3 12 15
if (utf8 && len >= 2 && strnEQ(beg, "\xFF\xFE", 2)) { 3 0 3
if (utf8 && len >= 2 && strnEQ(beg, "\xFF\xFE", 2)) { 0 3 3
1835 0 warn("Parsing string decoded with wrong endianness");
1836 }
1837 15 ▶ if (!utf8 && len >= 4 &&
Branch: 2 branches
BranchTrueFalseTotal
if (!utf8 && len >= 4 && 12 3 15
if (!utf8 && len >= 4 && 12 0 12
1838 12 ▶ (strnEQ(beg, "\x00\x00\xFE\xFF", 4) ||
Branch: 1 branch
BranchTrueFalseTotal
(strnEQ(beg, "\x00\x00\xFE\xFF", 4) || 11 1 12
1839 11 ▶ strnEQ(beg, "\xFE\xFF\x00\x00", 4))
Branch: 1 branch
BranchTrueFalseTotal
strnEQ(beg, "\xFE\xFF\x00\x00", 4)) 1 10 11
1840 )
1841 {
1842 2 warn("Parsing of undecoded UTF-32");
1843 }
1844 13 ▶ else if (!utf8 && len >= 2 &&
Branch: 2 branches
BranchTrueFalseTotal
else if (!utf8 && len >= 2 && 10 3 13
else if (!utf8 && len >= 2 && 10 0 10
1845 10 ▶ (strnEQ(beg, "\xFE\xFF", 2) || strnEQ(beg, "\xFF\xFE", 2))
Branch: 2 branches
BranchTrueFalseTotal
(strnEQ(beg, "\xFE\xFF", 2) || strnEQ(beg, "\xFF\xFE", 2)) 9 1 10
(strnEQ(beg, "\xFE\xFF", 2) || strnEQ(beg, "\xFF\xFE", 2)) 1 8 9
1846 )
1847 {
1848 2 warn("Parsing of undecoded UTF-16");
1849 }
1850 }
1851 }
1852  
1853 7258 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 4 7254 7258
1854 4 return; /* nothing to do */
1855  
1856 7254 end = beg + len;
1857 7254 s = parse_buf(aTHX_ p_state, beg, end, utf8, self);
1858  
1859 7253 ▶ if (s == end || p_state->eof) {
Branch: 2 branches
BranchTrueFalseTotal
if (s == end || p_state->eof) { 7085 168 7253
if (s == end || p_state->eof) { 8 7077 7085
1860 176 ▶ if (p_state->buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->buf) { 106 70 176
1861 106 ▶ SvOK_off(p_state->buf);
Branch: 4 branches
BranchTrueFalseTotal
SvOK_off(p_state->buf); 0 106 106
SvOK_off(p_state->buf); 0 0 0
SvOK_off(p_state->buf); 0 0 0
SvOK_off(p_state->buf); 46 60 106
1862 }
1863 }
1864 else {
1865 /* need to keep rest in buffer */
1866 7077 ▶ if (p_state->buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_state->buf) { 6921 156 7077
1867 /* chop off some chars at the beginning */
1868 6921 ▶ if (SvOK(p_state->buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(p_state->buf)) { 6834 87 6921
1869 6834 sv_chop(p_state->buf, s);
1870 }
1871 else {
1872 87 sv_setpvn(p_state->buf, s, end - s);
1873 87 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 16 71 87
1874 16 SvUTF8_on(p_state->buf);
1875 else
1876 71 SvUTF8_off(p_state->buf);
1877 }
1878 }
1879 else {
1880 156 p_state->buf = newSVpv(s, end - s);
1881 156 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 8 148 156
1882 8 SvUTF8_on(p_state->buf);
1883 }
1884 }
1885 7253 return;
1886 }
« blib/lib/HTML/LinkExtor.pm ↑ Summary Parser.xs »