HTMLParserEvent, attributes takes quoting sep
[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, int *tr);
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                                        int *tr)
104 {
105     int i = skipName(cp);
106     *attr_len = i;
107     *value = NULL;
108     if (!i)
109         return skipSpace(cp);
110     i += skipSpace(cp + i);
111     if (cp[i] == '=')
112     {
113         int v0, v1;
114         i++;
115         i += skipSpace(cp + i);
116         if (cp[i] == '\"' || cp[i] == '\'')
117         {
118             *tr = cp[i];
119             v0 = ++i;
120             while (cp[i] != *tr && cp[i])
121                 i++;
122             v1 = i;
123             if (cp[i])
124                 i++;
125         }
126         else
127         {
128             *tr = 0;
129             v0 = i;
130             while (cp[i] && !strchr(SPACECHR ">", cp[i]))
131                 i++;
132             v1 = i;
133         }
134         *value = cp + v0;
135         *val_len = v1 - v0;
136     }
137     i += skipSpace(cp + i);
138     return i;
139 }
140
141 int mp::HTMLParser::Rep::tagAttrs(HTMLParserEvent &event,
142                                   const char *name, int len,
143                                   const char *cp)
144 {
145     int i = skipSpace(cp);
146     while (cp[i] && cp[i] != '>' && cp[i] != '/')
147     {
148         const char *attr_name = cp + i;
149         int attr_len;
150         const char *value;
151         int val_len;
152         int tr;
153         int nor = skipAttribute(event, cp+i, &attr_len, &value, &val_len, &tr);
154         i += nor;
155         if (nor)
156         {
157             char x[2];
158             x[0] = tr;
159             x[1] = 0;
160             if (m_verbose)
161                 printf ("------ attr %.*s=%.*s\n", attr_len, attr_name,
162                         val_len, value);
163             event.attribute(name, len, attr_name, attr_len, value, val_len, x);
164         }
165         else
166         {
167             i++;
168         }
169     }
170     return i;
171 }
172
173 int mp::HTMLParser::Rep::tagStart(HTMLParserEvent &event,
174                                   int *tag_len,
175                                   const char *cp, const char which)
176 {
177     int i;
178     switch (which)
179     {
180     case '/':
181         i = skipName(cp);
182         *tag_len = i;
183         if (m_verbose)
184             printf("------ tag close %.*s\n", i, cp);
185         event.closeTag(cp, i);
186         break;
187     case '!':
188         for (i = 0; cp[i] && cp[i] != '>'; i++)
189             ;
190         *tag_len = i;
191         event.openTagStart(cp, i);
192         if (m_verbose)
193             printf("------ dtd %.*s\n", i, cp);
194         break;
195     case '?':
196         for (i = 0; cp[i] && cp[i] != '>'; i++)
197             ;
198         *tag_len = i;
199         event.openTagStart(cp, i);
200         if (m_verbose)
201             printf("------ pi %.*s\n", i, cp);
202         break;
203     default:
204         i = skipName(cp);
205         *tag_len = i;
206         if (m_verbose)
207             printf("------ tag open %.*s\n", i, cp);
208         event.openTagStart(cp, i);
209
210         i += tagAttrs(event, cp, i, cp + i);
211
212         break;
213     }
214     return i;
215 }
216
217 int mp::HTMLParser::Rep::tagEnd(HTMLParserEvent &event,
218                                 const char *tag, int tag_len, const char *cp)
219 {
220     int i = 0;
221     int close_it = 0;
222     for (; cp[i] && cp[i] != '/' && cp[i] != '>'; i++)
223         ;
224     if (i > 0)
225         event.text(cp, i);
226     if (cp[i] == '/')
227     {
228         close_it = 1;
229         i++;
230     }
231     if (cp[i] == '>')
232     {
233         event.anyTagEnd(tag, tag_len, close_it);
234         i++;
235     }
236     return i;
237 }
238
239 void mp::HTMLParser::Rep::tagText(HTMLParserEvent &event,
240                                   const char *text_start, const char *text_end)
241 {
242     if (text_end - text_start) //got text to flush
243     {
244         if (m_verbose)
245             printf("------ text %.*s\n",
246                    (int) (text_end - text_start), text_start);
247         event.text(text_start, text_end-text_start);
248     }
249 }
250
251 void mp::HTMLParser::Rep::parse_str(HTMLParserEvent &event, const char *cp)
252 {
253     const char *text_start = cp;
254     const char *text_end = cp;
255     while (*cp)
256     {
257         if (cp[0] == '<' && cp[1])  //tag?
258         {
259             char which = cp[1];
260             if (which == '/')
261                 cp++;
262             if (!strchr(SPACECHR, cp[1])) //valid tag starts
263             {
264                 int i = 0;
265                 int tag_len;
266
267                 tagText(event, text_start, text_end); //flush any text
268                 cp++;
269                 i += tagStart(event, &tag_len, cp, which);
270                 i += tagEnd(event, cp, tag_len, cp + i);
271                 cp += i;
272                 text_start = cp;
273                 text_end = cp;
274                 continue;
275             }
276         }
277         //text
278         cp++;
279         text_end = cp;
280     }
281     tagText(event, text_start, text_end); //flush any text
282 }
283
284 /*
285  * Local variables:
286  * c-basic-offset: 4
287  * c-file-style: "Stroustrup"
288  * indent-tabs-mode: nil
289  * End:
290  * vim: shiftwidth=4 tabstop=8 expandtab
291  */
292