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