1 #include "tagfilter.h"
2 #include <parser.h>
3 #include <ctype.h>
4 
5 static const char *blacklist[] = {
6     "title",   "textarea", "style",  "xmp",       "iframe",
7     "noembed", "noframes", "script", "plaintext", NULL,
8 };
9 
is_tag(const unsigned char * tag_data,size_t tag_size,const char * tagname)10 static int is_tag(const unsigned char *tag_data, size_t tag_size,
11                   const char *tagname) {
12   size_t i;
13 
14   if (tag_size < 3 || tag_data[0] != '<')
15     return 0;
16 
17   i = 1;
18 
19   if (tag_data[i] == '/') {
20     i++;
21   }
22 
23   for (; i < tag_size; ++i, ++tagname) {
24     if (*tagname == 0)
25       break;
26 
27     if (tolower(tag_data[i]) != *tagname)
28       return 0;
29   }
30 
31   if (i == tag_size)
32     return 0;
33 
34   if (cmark_isspace(tag_data[i]) || tag_data[i] == '>')
35     return 1;
36 
37   if (tag_data[i] == '/' && tag_size >= i + 2 && tag_data[i + 1] == '>')
38     return 1;
39 
40   return 0;
41 }
42 
filter(cmark_syntax_extension * ext,const unsigned char * tag,size_t tag_len)43 static int filter(cmark_syntax_extension *ext, const unsigned char *tag,
44                   size_t tag_len) {
45   const char **it;
46 
47   for (it = blacklist; *it; ++it) {
48     if (is_tag(tag, tag_len, *it)) {
49       return 0;
50     }
51   }
52 
53   return 1;
54 }
55 
create_tagfilter_extension(void)56 cmark_syntax_extension *create_tagfilter_extension(void) {
57   cmark_syntax_extension *ext = cmark_syntax_extension_new("tagfilter");
58   cmark_syntax_extension_set_html_filter_func(ext, filter);
59   return ext;
60 }
61