X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fhtml_parser.cpp;h=95db7c7fd41993645193ba617e1d5404993a8307;hb=15e63ff49000e082d8387c243941485ccacff13b;hp=8c4426d622aec429df53228378c1180b08c4c08b;hpb=bb6b58cbec0e41a8ba2403e540185e77882e8741;p=metaproxy-moved-to-github.git diff --git a/src/html_parser.cpp b/src/html_parser.cpp index 8c4426d..95db7c7 100644 --- a/src/html_parser.cpp +++ b/src/html_parser.cpp @@ -1,5 +1,5 @@ /* This file is part of Metaproxy. - Copyright (C) 2005-2013 Index Data + Copyright (C) Index Data Metaproxy is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -24,9 +24,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include +#include #define SPACECHR " \t\r\n\f" +// http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html namespace metaproxy_1 { class HTMLParser::Rep { @@ -37,8 +39,6 @@ namespace metaproxy_1 { const char *text_start, const char *text_end); int tagEnd(HTMLParserEvent &event, const char *tag, int tag_len, const char *cp); - int tagStart(HTMLParserEvent &event, - int *tag_len, const char *cp, const char which); int tagAttrs(HTMLParserEvent &event, const char *name, int len, const char *cp); @@ -48,6 +48,7 @@ namespace metaproxy_1 { Rep(); ~Rep(); int m_verbose; + bool nest; }; } @@ -56,6 +57,7 @@ namespace mp = metaproxy_1; mp::HTMLParser::Rep::Rep() { m_verbose = 0; + nest = true; } mp::HTMLParser::Rep::~Rep() @@ -81,6 +83,11 @@ void mp::HTMLParser::parse(mp::HTMLParserEvent & event, const char *str) const m_p->parse_str(event, str); } +static int isAlpha(int c) +{ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + static int skipSpace(const char *cp) { int i = 0; @@ -92,7 +99,7 @@ static int skipSpace(const char *cp) static int skipName(const char *cp) { int i; - for (i = 0; cp[i] && !strchr(SPACECHR "/>=", cp[i]); i++) + for (i = 0; cp[i] && !strchr(SPACECHR "/><=", cp[i]); i++) ; return i; } @@ -143,7 +150,7 @@ int mp::HTMLParser::Rep::tagAttrs(HTMLParserEvent &event, const char *cp) { int i = skipSpace(cp); - while (cp[i] && cp[i] != '>' && cp[i] != '/') + while (cp[i] && !strchr("/><", cp[i])) { const char *attr_name = cp + i; int attr_len; @@ -159,63 +166,23 @@ int mp::HTMLParser::Rep::tagAttrs(HTMLParserEvent &event, x[0] = tr; x[1] = 0; if (m_verbose) - printf ("------ attr %.*s=%.*s\n", attr_len, attr_name, - val_len, value); + { + printf("------ attr %.*s", attr_len, attr_name); + if (value) + printf("=%.*s", val_len, value); + printf("\n"); + } event.attribute(name, len, attr_name, attr_len, value, val_len, x); } return i; } -int mp::HTMLParser::Rep::tagStart(HTMLParserEvent &event, - int *tag_len, - const char *cp, const char which) -{ - int i; - switch (which) - { - case '/': - i = skipName(cp); - *tag_len = i; - if (m_verbose) - printf("------ tag close %.*s\n", i, cp); - event.closeTag(cp, i); - break; - case '!': - for (i = 0; cp[i] && cp[i] != '>'; i++) - ; - *tag_len = i; - event.openTagStart(cp, i); - if (m_verbose) - printf("------ dtd %.*s\n", i, cp); - break; - case '?': - for (i = 0; cp[i] && cp[i] != '>'; i++) - ; - *tag_len = i; - event.openTagStart(cp, i); - if (m_verbose) - printf("------ pi %.*s\n", i, cp); - break; - default: - i = skipName(cp); - *tag_len = i; - if (m_verbose) - printf("------ tag open %.*s\n", i, cp); - event.openTagStart(cp, i); - - i += tagAttrs(event, cp, i, cp + i); - - break; - } - return i; -} - int mp::HTMLParser::Rep::tagEnd(HTMLParserEvent &event, const char *tag, int tag_len, const char *cp) { int i = 0; int close_it = 0; - for (; cp[i] && cp[i] != '/' && cp[i] != '>'; i++) + for (; cp[i] && !strchr("/><", cp[i]); i++) ; if (i > 0) { @@ -232,7 +199,7 @@ int mp::HTMLParser::Rep::tagEnd(HTMLParserEvent &event, { if (m_verbose) printf("------ any tag %s %.*s\n", - close_it ? " close" : "end", tag_len, tag); + close_it ? "close" : "end", tag_len, tag); event.anyTagEnd(tag, tag_len, close_it); i++; } @@ -254,34 +221,98 @@ void mp::HTMLParser::Rep::tagText(HTMLParserEvent &event, void mp::HTMLParser::Rep::parse_str(HTMLParserEvent &event, const char *cp) { const char *text_start = cp; - const char *text_end = cp; while (*cp) { - if (cp[0] == '<' && cp[1]) //tag? + if (*cp++ != '<') + continue; + + if (nest && *cp == '!') { - char which = cp[1]; - if (which == '/') - cp++; - if (!strchr(SPACECHR, cp[1])) //valid tag starts + int i; + tagText(event, text_start, cp - 1); + if (cp[1] == '-' && cp[2] == '-') + { + for (i = 3; cp[i]; i++) + if (cp[i] == '-' && cp[i+1] == '-' && cp[i+2] == '>') + { + i+= 2; + event.openTagStart(cp, i); + break; + } + } + else { - int i = 0; - int tag_len; - - tagText(event, text_start, text_end); //flush any text - cp++; - i += tagStart(event, &tag_len, cp, which); - i += tagEnd(event, cp, tag_len, cp + i); - cp += i; - text_start = cp; - text_end = cp; - continue; + for (i = 1; cp[i] && cp[i] != '>'; i++) + ; + event.openTagStart(cp, i); } + if (m_verbose) + printf("------ dtd %.*s\n", i, cp); + i += tagEnd(event, cp, i, cp + i); + cp += i; + text_start = cp; + } + else if (nest && *cp == '?') + { + int i; + tagText(event, text_start, cp - 1); + for (i = 1; cp[i] && cp[i] != '>'; i++) + ; + event.openTagStart(cp, i); + if (m_verbose) + printf("------ pi %.*s\n", i, cp); + i += tagEnd(event, cp, i, cp + i); + cp += i; + text_start = cp; + } + else if (*cp == '/' && isAlpha(cp[1])) + { + int i; + + i = skipName(++cp); + + if (!nest) + { + if (i == 6 && !yaz_strncasecmp(cp, "script", i)) + { + int ws = skipSpace(cp + 6); + if (cp[ws + 6] == '>') + nest = true; /* really terminated */ + } + if (!nest) + continue; + } + tagText(event, text_start, cp - 2); + event.closeTag(cp, i); + if (m_verbose) + printf("------ tag close %.*s\n", i, cp); + i += tagEnd(event, cp, i, cp + i); + cp += i; + text_start = cp; + } + else if (nest && isAlpha(*cp)) + { + int i, j; + tagText(event, text_start, cp - 1); + i = skipName(cp); + event.openTagStart(cp, i); + if (m_verbose) + printf("------ tag open %.*s\n", i, cp); + j = tagAttrs(event, cp, i, cp + i); + j += tagEnd(event, cp, i, cp + i + j); + + if (i == 6 && !yaz_strncasecmp(cp, "script", i)) + nest = false; + + cp += i + j; + text_start = cp; } - //text - cp++; - text_end = cp; } - tagText(event, text_start, text_end); //flush any text + tagText(event, text_start, cp); +} + +mp::HTMLParserEvent::~HTMLParserEvent() +{ } /*