1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
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
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
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
20 #include "html_parser.hpp"
28 #define SPACECHR " \t\r\n\f"
31 namespace metaproxy_1 {
32 class HTMLParser::Rep {
33 friend class HTMLParser;
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,
45 int skipAttribute(HTMLParserEvent &event,
46 const char *cp, int *attr_len,
47 const char **value, int *val_len, int *tr);
54 namespace mp = metaproxy_1;
56 mp::HTMLParser::Rep::Rep()
61 mp::HTMLParser::Rep::~Rep()
65 mp::HTMLParser::HTMLParser() : m_p(new Rep)
69 mp::HTMLParser::~HTMLParser()
73 void mp::HTMLParser::set_verbose(int v)
79 void mp::HTMLParser::parse(mp::HTMLParserEvent & event, const char *str) const
81 m_p->parse_str(event, str);
84 static int skipSpace(const char *cp)
87 while (cp[i] && strchr(SPACECHR, cp[i]))
92 static int skipName(const char *cp)
95 for (i = 0; cp[i] && !strchr(SPACECHR "/>=", cp[i]); i++)
100 int mp::HTMLParser::Rep::skipAttribute(HTMLParserEvent &event,
101 const char *cp, int *attr_len,
102 const char **value, int *val_len,
106 int i = skipName(cp);
110 return skipSpace(cp);
111 i += skipSpace(cp + i);
116 i += skipSpace(cp + i);
117 if (cp[i] == '\"' || cp[i] == '\'')
121 while (cp[i] != *tr && cp[i])
131 while (cp[i] && !strchr(SPACECHR ">", cp[i]))
138 i += skipSpace(cp + i);
142 int mp::HTMLParser::Rep::tagAttrs(HTMLParserEvent &event,
143 const char *name, int len,
146 int i = skipSpace(cp);
147 while (cp[i] && cp[i] != '>' && cp[i] != '/')
149 const char *attr_name = cp + i;
155 int nor = skipAttribute(event, cp+i, &attr_len, &value, &val_len, &tr);
163 printf ("------ attr %.*s=%.*s\n", attr_len, attr_name,
165 event.attribute(name, len, attr_name, attr_len, value, val_len, x);
170 int mp::HTMLParser::Rep::tagStart(HTMLParserEvent &event,
172 const char *cp, const char which)
181 printf("------ tag close %.*s\n", i, cp);
182 event.closeTag(cp, i);
185 for (i = 0; cp[i] && cp[i] != '>'; i++)
188 event.openTagStart(cp, i);
190 printf("------ dtd %.*s\n", i, cp);
193 for (i = 0; cp[i] && cp[i] != '>'; i++)
196 event.openTagStart(cp, i);
198 printf("------ pi %.*s\n", i, cp);
204 printf("------ tag open %.*s\n", i, cp);
205 event.openTagStart(cp, i);
207 i += tagAttrs(event, cp, i, cp + i);
214 int mp::HTMLParser::Rep::tagEnd(HTMLParserEvent &event,
215 const char *tag, int tag_len, const char *cp)
219 for (; cp[i] && cp[i] != '/' && cp[i] != '>'; i++)
224 printf("------ text %.*s\n", i, cp);
235 printf("------ any tag %s %.*s\n",
236 close_it ? " close" : "end", tag_len, tag);
237 event.anyTagEnd(tag, tag_len, close_it);
243 void mp::HTMLParser::Rep::tagText(HTMLParserEvent &event,
244 const char *text_start, const char *text_end)
246 if (text_end - text_start) //got text to flush
249 printf("------ text %.*s\n",
250 (int) (text_end - text_start), text_start);
251 event.text(text_start, text_end-text_start);
255 void mp::HTMLParser::Rep::parse_str(HTMLParserEvent &event, const char *cp)
257 const char *text_start = cp;
258 const char *text_end = cp;
261 if (cp[0] == '<' && cp[1]) //tag?
266 if (!strchr(SPACECHR, cp[1])) //valid tag starts
271 tagText(event, text_start, text_end); //flush any text
273 i += tagStart(event, &tag_len, cp, which);
274 i += tagEnd(event, cp, tag_len, cp + i);
285 tagText(event, text_start, text_end); //flush any text
291 * c-file-style: "Stroustrup"
292 * indent-tabs-mode: nil
294 * vim: shiftwidth=4 tabstop=8 expandtab