vibe.textfilter.html 0/4(0%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
/** HTML character entity escaping. TODO: Make things @safe once Appender is. Copyright: © 2012-2014 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ module vibe.textfilter.html; import std.array; import std.conv; import std.range; /** Returns the HTML escaped version of a given string. */ string htmlEscape(R)(R str) @trusted if (isInputRange!R) { if (__ctfe) { // appender is a performance/memory hog in ctfe StringAppender dst; filterHTMLEscape(dst, str); return dst.data; } else { auto dst = appender!string(); filterHTMLEscape(dst, str); return dst.data; } } /// unittest { assert(htmlEscape(`"Hello", <World>!`) == `"Hello", <World>!`); } /** Writes the HTML escaped version of a given string to an output range. */ void filterHTMLEscape(R, S)(ref R dst, S str, HTMLEscapeFlags flags = HTMLEscapeFlags.escapeNewline) if (isOutputRange!(R, dchar) && isInputRange!S) { for (;!str.empty;str.popFront()) filterHTMLEscape(dst, str.front, flags); } /** Returns the HTML escaped version of a given string (also escapes double quotes). */ string htmlAttribEscape(R)(R str) @trusted if (isInputRange!R) { if (__ctfe) { // appender is a performance/memory hog in ctfe StringAppender dst; filterHTMLAttribEscape(dst, str); return dst.data; } else { auto dst = appender!string(); filterHTMLAttribEscape(dst, str); return dst.data; } } /// unittest { assert(htmlAttribEscape(`"Hello", <World>!`) == `"Hello", <World>!`); } /** Writes the HTML escaped version of a given string to an output range (also escapes double quotes). */ void filterHTMLAttribEscape(R, S)(ref R dst, S str) if (isOutputRange!(R, dchar) && isInputRange!S) { for (; !str.empty; str.popFront()) filterHTMLEscape(dst, str.front, HTMLEscapeFlags.escapeNewline|HTMLEscapeFlags.escapeQuotes); } /** Returns the HTML escaped version of a given string (escapes every character). */ string htmlAllEscape(R)(R str) @trusted if (isInputRange!R) { if (__ctfe) { // appender is a performance/memory hog in ctfe StringAppender dst; filterHTMLAllEscape(dst, str); return dst.data; } else { auto dst = appender!string(); filterHTMLAllEscape(dst, str); return dst.data; } } /// unittest { assert(htmlAllEscape("Hello!") == "Hello!"); } /** Writes the HTML escaped version of a given string to an output range (escapes every character). */ void filterHTMLAllEscape(R, S)(ref R dst, S str) if (isOutputRange!(R, dchar) && isInputRange!S) { for (; !str.empty; str.popFront()) { dst.put("&#"); dst.put(to!string(cast(uint)str.front)); dst.put(';'); } } /** Minimally escapes a text so that no HTML tags appear in it. */ string htmlEscapeMin(R)(R str) @trusted if (isInputRange!R) { auto dst = appender!string(); for (; !str.empty; str.popFront()) filterHTMLEscape(dst, str.front, HTMLEscapeFlags.escapeMinimal); return dst.data(); } /** Writes the HTML escaped version of a character to an output range. */ void filterHTMLEscape(R)(ref R dst, dchar ch, HTMLEscapeFlags flags = HTMLEscapeFlags.escapeNewline ) { switch (ch) { default: if (flags & HTMLEscapeFlags.escapeUnknown) { dst.put("&#"); dst.put(to!string(cast(uint)ch)); dst.put(';'); } else dst.put(ch); break; case '"': if (flags & HTMLEscapeFlags.escapeQuotes) dst.put("""); else dst.put('"'); break; case '\'': if (flags & HTMLEscapeFlags.escapeQuotes) dst.put("'"); else dst.put('\''); break; case '\r', '\n': if (flags & HTMLEscapeFlags.escapeNewline) { dst.put("&#"); dst.put(to!string(cast(uint)ch)); dst.put(';'); } else dst.put(ch); break; case 'a': .. case 'z': goto case; case 'A': .. case 'Z': goto case; case '0': .. case '9': goto case; case ' ', '\t', '-', '_', '.', ':', ',', ';', '#', '+', '*', '?', '=', '(', ')', '/', '!', '%' , '{', '}', '[', ']', '`', '´', '$', '^', '~': dst.put(cast(char)ch); break; case '<': dst.put("<"); break; case '>': dst.put(">"); break; case '&': dst.put("&"); break; } } enum HTMLEscapeFlags { escapeMinimal = 0, escapeQuotes = 1<<0, escapeNewline = 1<<1, escapeUnknown = 1<<2 } private struct StringAppender { @safe: string data; void put(string s) { data ~= s; } void put(char ch) { data ~= ch; } void put(dchar ch) { import std.utf; char[4] dst; data ~= dst[0 .. encode(dst, ch)]; } }