| 1 |
|
| #ifndef PUNK_ESCAPE_H |
| 2 |
|
| #define PUNK_ESCAPE_H |
| 3 |
|
| |
| 4 |
|
| /* Escape text for HTML: an element's content or an attribute's value, quoted |
| 5 |
|
| * either way. Appends to `out`, copying runs that need nothing in one go. |
| 6 |
|
| * |
| 7 |
|
| * The five characters are the whole job: & < > " ' are the only ones that can |
| 8 |
|
| * end a text node, an attribute value or a tag. Bytes are copied as they are, |
| 9 |
|
| * so UTF-8 passes through untouched (no byte of a multi-byte character is |
| 10 |
|
| * ASCII). The csrf field, the static listing and the markdown mount all write |
| 11 |
|
| * markup with this; $c->escape_html is the same function for Perl. XML is a |
| 12 |
|
| * different escape (the sitemap's ') and has its own. */ |
| 13 |
9179 |
| static void punk_html_escape(pTHX_ SV *out, const char *s, STRLEN len) { |
| 14 |
9179 |
| STRLEN i, run = 0; |
| 15 |
98839 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
89660 |
9179 |
98839 |
|
| 16 |
|
| const char *rep; |
| 17 |
89660 |
| switch (s[i]) { |
| 18 |
9 |
| case '&': rep = "&"; break; |
| 19 |
29 |
| case '<': rep = "<"; break; |
| 20 |
27 |
| case '>': rep = ">"; break; |
| 21 |
22 |
| case '"': rep = """; break; |
| 22 |
3 |
| case '\'': rep = "'"; break; |
| 23 |
89570 |
| default: run++; continue; |
| 24 |
|
| } |
| 25 |
90 |
▶ | if (run) { sv_catpvn(out, s + i - run, run); run = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (run) { sv_catpvn(out, s + i - run, run); run = 0; } |
77 |
13 |
90 |
|
| 26 |
90 |
| sv_catpv(out, rep); |
| 27 |
|
| } |
| 28 |
9179 |
▶ | if (run) sv_catpvn(out, s + len - run, run); |
Branch: 1 branch
| Branch | True | False | Total |
| if (run) sv_catpvn(out, s + len - run, run); |
8747 |
432 |
9179 |
|
| 29 |
9179 |
| } |
| 30 |
|
| |
| 31 |
|
| #endif |