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"
30 // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html
32 namespace metaproxy_1 {
33 class HTMLParser::Rep {
34 friend class HTMLParser;
36 void parse_str(HTMLParserEvent &event, const char *cp);
37 void tagText(HTMLParserEvent &event,
38 const char *text_start, const char *text_end);
39 int tagEnd(HTMLParserEvent &event,
40 const char *tag, int tag_len, const char *cp);
41 int tagAttrs(HTMLParserEvent &event,
42 const char *name, int len,
44 int skipAttribute(HTMLParserEvent &event,
45 const char *cp, int *attr_len,
46 const char **value, int *val_len, int *tr);
53 namespace mp = metaproxy_1;
55 mp::HTMLParser::Rep::Rep()
60 mp::HTMLParser::Rep::~Rep()
64 mp::HTMLParser::HTMLParser() : m_p(new Rep)
68 mp::HTMLParser::~HTMLParser()
72 void mp::HTMLParser::set_verbose(int v)
78 void mp::HTMLParser::parse(mp::HTMLParserEvent & event, const char *str) const
80 m_p->parse_str(event, str);
83 static int isAlpha(int c)
85 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
88 static int skipSpace(const char *cp)
91 while (cp[i] && strchr(SPACECHR, cp[i]))
96 static int skipName(const char *cp)
99 for (i = 0; cp[i] && !strchr(SPACECHR "/><=", cp[i]); i++)
104 int mp::HTMLParser::Rep::skipAttribute(HTMLParserEvent &event,
105 const char *cp, int *attr_len,
106 const char **value, int *val_len,
110 int i = skipName(cp);
114 return skipSpace(cp);
115 i += skipSpace(cp + i);
119 i += skipSpace(cp + i);
120 if (cp[i] == '\"' || cp[i] == '\'')
124 while (cp[i] != *tr && cp[i])
134 while (cp[i] && !strchr(SPACECHR ">", cp[i]))
140 i += skipSpace(cp + i);
145 int mp::HTMLParser::Rep::tagAttrs(HTMLParserEvent &event,
146 const char *name, int len,
149 int i = skipSpace(cp);
150 while (cp[i] && !strchr("/><", cp[i]))
152 const char *attr_name = cp + i;
158 int nor = skipAttribute(event, cp+i, &attr_len, &value, &val_len, &tr);
166 printf ("------ attr %.*s=%.*s\n", attr_len, attr_name,
168 event.attribute(name, len, attr_name, attr_len, value, val_len, x);
173 int mp::HTMLParser::Rep::tagEnd(HTMLParserEvent &event,
174 const char *tag, int tag_len, const char *cp)
178 for (; cp[i] && !strchr("/><", cp[i]); i++)
183 printf("------ text %.*s\n", i, cp);
194 printf("------ any tag %s %.*s\n",
195 close_it ? " close" : "end", tag_len, tag);
196 event.anyTagEnd(tag, tag_len, close_it);
202 void mp::HTMLParser::Rep::tagText(HTMLParserEvent &event,
203 const char *text_start, const char *text_end)
205 if (text_end - text_start) //got text to flush
208 printf("------ text %.*s\n",
209 (int) (text_end - text_start), text_start);
210 event.text(text_start, text_end-text_start);
214 void mp::HTMLParser::Rep::parse_str(HTMLParserEvent &event, const char *cp)
216 const char *text_start = cp;
225 tagText(event, text_start, cp - 1);
226 if (cp[1] == '-' && cp[2] == '-')
228 for (i = 3; cp[i]; i++)
229 if (cp[i] == '-' && cp[i+1] == '-' && cp[i+2] == '>')
232 event.openTagStart(cp, i);
238 for (i = 1; cp[i] && cp[i] != '>'; i++)
240 event.openTagStart(cp, i);
243 printf("------ dtd %.*s\n", i, cp);
244 i += tagEnd(event, cp, i, cp + i);
251 tagText(event, text_start, cp - 1);
252 for (i = 1; cp[i] && cp[i] != '>'; i++)
254 event.openTagStart(cp, i);
256 printf("------ pi %.*s\n", i, cp);
257 i += tagEnd(event, cp, i, cp + i);
261 else if (*cp == '/' && isAlpha(cp[1]))
264 tagText(event, text_start, cp - 1);
267 event.closeTag(cp, i);
269 printf("------ tag close %.*s\n", i, cp);
270 i += tagEnd(event, cp, i, cp + i);
274 else if (isAlpha(*cp))
277 tagText(event, text_start, cp - 1);
279 event.openTagStart(cp, i);
281 printf("------ tag open %.*s\n", i, cp);
282 j = tagAttrs(event, cp, i, cp + i);
283 j += tagEnd(event, cp, i, cp + i + j);
288 tagText(event, text_start, cp);
291 mp::HTMLParserEvent::~HTMLParserEvent()
298 * c-file-style: "Stroustrup"
299 * indent-tabs-mode: nil
301 * vim: shiftwidth=4 tabstop=8 expandtab