Event.text for whitespace in <x/ > tags
[metaproxy-moved-to-github.git] / src / html_parser.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20 #include "html_parser.hpp"
21
22 #include <assert.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26 #include <stdio.h>
27
28 #define SPACECHR " \t\r\n\f"
29
30
31 namespace metaproxy_1 {
32     class HTMLParser::Rep {
33         friend class HTMLParser;
34     public:
35         void parse_str(HTMLParserEvent &event, const char *cp);
36         void tagText(HTMLParserEvent &event,
37                      const char *text_start, const char *text_end);
38         int tagEnd(HTMLParserEvent &event,
39                    const char *tag, int tag_len, const char *cp);
40         int tagStart(HTMLParserEvent &event,
41                      int *tag_len, const char *cp, const char which);
42         int tagAttrs(HTMLParserEvent &event,
43                      const char *name, int len,
44                      const char *cp);
45         int skipAttribute(HTMLParserEvent &event,
46                           const char *cp, int *attr_len,
47                           const char **value, int *val_len);
48         Rep();
49         ~Rep();
50         int m_verbose;
51     };
52 }
53
54 namespace mp = metaproxy_1;
55
56 mp::HTMLParser::Rep::Rep()
57 {
58     m_verbose = 0;
59 }
60
61 mp::HTMLParser::Rep::~Rep()
62 {
63 }
64
65 mp::HTMLParser::HTMLParser() : m_p(new Rep)
66 {
67 }
68
69 mp::HTMLParser::~HTMLParser()
70 {
71 }
72
73 void mp::HTMLParser::set_verbose(int v)
74 {
75     m_p->m_verbose = v;
76 }
77
78
79 void mp::HTMLParser::parse(mp::HTMLParserEvent & event, const char *str) const
80 {
81     m_p->parse_str(event, str);
82 }
83
84 static int skipSpace(const char *cp)
85 {
86     int i = 0;
87     while (cp[i] && strchr(SPACECHR, cp[i]))
88         i++;
89     return i;
90 }
91
92 static int skipName(const char *cp)
93 {
94     int i;
95     for (i = 0; cp[i] && !strchr(SPACECHR "/>=", cp[i]); i++)
96         ;
97     return i;
98 }
99
100 int mp::HTMLParser::Rep::skipAttribute(HTMLParserEvent &event,
101                                        const char *cp, int *attr_len,
102                                        const char **value, int *val_len)
103 {
104     int i = skipName(cp);
105     *attr_len = i;
106     *value = NULL;
107     if (!i)
108         return skipSpace(cp);
109     i += skipSpace(cp + i);
110     if (cp[i] == '=')
111     {
112         int v0, v1;
113         i++;
114         i += skipSpace(cp + i);
115         if (cp[i] == '\"' || cp[i] == '\'')
116         {
117             char tr = cp[i];
118             v0 = ++i;
119             while (cp[i] != tr && cp[i])
120                 i++;
121             v1 = i;
122             if (cp[i])
123                 i++;
124         }
125         else
126         {
127             v0 = i;
128             while (cp[i] && !strchr(SPACECHR ">", cp[i]))
129                 i++;
130             v1 = i;
131         }
132         *value = cp + v0;
133         *val_len = v1 - v0;
134     }
135     i += skipSpace(cp + i);
136     return i;
137 }
138
139 int mp::HTMLParser::Rep::tagAttrs(HTMLParserEvent &event,
140                                   const char *name, int len,
141                                   const char *cp)
142 {
143     int i = skipSpace(cp);
144     while (cp[i] && cp[i] != '>' && cp[i] != '/')
145     {
146         const char *attr_name = cp + i;
147         int attr_len;
148         const char *value;
149         int val_len;
150         int nor = skipAttribute(event, cp+i, &attr_len, &value, &val_len);
151         i += nor;
152         if (nor)
153         {
154             if (m_verbose)
155                 printf ("------ attr %.*s=%.*s\n", attr_len, attr_name,
156                         val_len, value);
157             event.attribute(name, len, attr_name, attr_len, value, val_len);
158         }
159         else
160         {
161             i++;
162         }
163     }
164     return i;
165 }
166
167 int mp::HTMLParser::Rep::tagStart(HTMLParserEvent &event,
168                                   int *tag_len,
169                                   const char *cp, const char which)
170 {
171     int i;
172     switch (which)
173     {
174     case '/':
175         i = skipName(cp);
176         *tag_len = i;
177         if (m_verbose)
178             printf("------ tag close %.*s\n", i, cp);
179         event.closeTag(cp, i);
180         break;
181     case '!':
182         for (i = 0; cp[i] && cp[i] != '>'; i++)
183             ;
184         *tag_len = i;
185         event.openTagStart(cp, i);
186         if (m_verbose)
187             printf("------ dtd %.*s\n", i, cp);
188         break;
189     case '?':
190         for (i = 0; cp[i] && cp[i] != '>'; i++)
191             ;
192         *tag_len = i;
193         event.openTagStart(cp, i);
194         if (m_verbose)
195             printf("------ pi %.*s\n", i, cp);
196         break;
197     default:
198         i = skipName(cp);
199         *tag_len = i;
200         if (m_verbose)
201             printf("------ tag open %.*s\n", i, cp);
202         event.openTagStart(cp, i);
203
204         i += tagAttrs(event, cp, i, cp + i);
205
206         break;
207     }
208     return i;
209 }
210
211 int mp::HTMLParser::Rep::tagEnd(HTMLParserEvent &event,
212                                 const char *tag, int tag_len, const char *cp)
213 {
214     int i = 0;
215     int close_it = 0;
216     for (; cp[i] && cp[i] != '/' && cp[i] != '>'; i++)
217         ;
218     if (i > 0)
219         event.text(cp, i);
220     if (cp[i] == '/')
221     {
222         close_it = 1;
223         i++;
224     }
225     if (cp[i] == '>')
226     {
227         event.anyTagEnd(tag, tag_len, close_it);
228         i++;
229     }
230     return i;
231 }
232
233 void mp::HTMLParser::Rep::tagText(HTMLParserEvent &event,
234                                   const char *text_start, const char *text_end)
235 {
236     if (text_end - text_start) //got text to flush
237     {
238         if (m_verbose)
239             printf("------ text %.*s\n",
240                    (int) (text_end - text_start), text_start);
241         event.text(text_start, text_end-text_start);
242     }
243 }
244
245 void mp::HTMLParser::Rep::parse_str(HTMLParserEvent &event, const char *cp)
246 {
247     const char *text_start = cp;
248     const char *text_end = cp;
249     while (*cp)
250     {
251         if (cp[0] == '<' && cp[1])  //tag?
252         {
253             char which = cp[1];
254             if (which == '/')
255                 cp++;
256             if (!strchr(SPACECHR, cp[1])) //valid tag starts
257             {
258                 int i = 0;
259                 int tag_len;
260
261                 tagText(event, text_start, text_end); //flush any text
262                 cp++;
263                 i += tagStart(event, &tag_len, cp, which);
264                 i += tagEnd(event, cp, tag_len, cp + i);
265                 cp += i;
266                 text_start = cp;
267                 text_end = cp;
268                 continue;
269             }
270         }
271         //text
272         cp++;
273         text_end = cp;
274     }
275     tagText(event, text_start, text_end); //flush any text
276 }
277
278 /*
279  * Local variables:
280  * c-basic-offset: 4
281  * c-file-style: "Stroustrup"
282  * indent-tabs-mode: nil
283  * End:
284  * vim: shiftwidth=4 tabstop=8 expandtab
285  */
286