/usr/include/htmlcxx/html/ParserSax.tcc is in libhtmlcxx-dev 0.85-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | #include <cctype>
#include <cstring>
#if !defined(WIN32) || defined(__MINGW32__)
#include <strings.h>
#endif
//#define DEBUG
//#include "debug.h"
static
struct literal_tag {
int len;
const char* str;
int is_cdata;
}
literal_mode_elem[] =
{
{6, "script", 1},
{5, "style", 1},
{3, "xmp", 1},
{9, "plaintext", 1},
{8, "textarea", 0},
{0, 0, 0}
};
template <typename _Iterator>
void htmlcxx::HTML::ParserSax::parse(_Iterator begin, _Iterator end)
{
// std::cerr << "Parsing iterator" << std::endl;
parse(begin, end, typename std::iterator_traits<_Iterator>::iterator_category());
}
template <typename _Iterator>
void htmlcxx::HTML::ParserSax::parse(_Iterator &begin, _Iterator &end, std::forward_iterator_tag)
{
typedef _Iterator iterator;
// std::cerr << "Parsing forward_iterator" << std::endl;
mCdata = false;
mpLiteral = 0;
mCurrentOffset = 0;
this->beginParsing();
// DEBUGP("Parsed text\n");
while (begin != end)
{
*begin; // This is for the multi_pass to release the buffer
iterator c(begin);
while (c != end)
{
// For some tags, the text inside it is considered literal and is
// only closed for its </TAG> counterpart
while (mpLiteral)
{
// DEBUGP("Treating literal %s\n", mpLiteral);
while (c != end && *c != '<') ++c;
if (c == end) {
if (c != begin) this->parseContent(begin, c);
goto DONE;
}
iterator end_text(c);
++c;
if (*c == '/')
{
++c;
const char *l = mpLiteral;
while (*l && ::tolower(*c) == *l)
{
++c;
++l;
}
// FIXME: Mozilla stops when it sees a /plaintext. Check
// other browsers and decide what to do
if (!*l && strcmp(mpLiteral, "plaintext"))
{
// matched all and is not tag plaintext
while (isspace(*c)) ++c;
if (*c == '>')
{
++c;
if (begin != end_text)
this->parseContent(begin, end_text);
mpLiteral = 0;
c = end_text;
begin = c;
break;
}
}
}
else if (*c == '!')
{
// we may find a comment and we should support it
iterator e(c);
++e;
if (e != end && *e == '-' && ++e != end && *e == '-')
{
// DEBUGP("Parsing comment\n");
++e;
c = this->skipHtmlComment(e, end);
}
//if (begin != end_text)
//this->parseContent(begin, end_text, end);
//this->parseComment(end_text, c, end);
// continue from the end of the comment
//begin = c;
}
}
if (*c == '<')
{
iterator d(c);
++d;
if (d != end)
{
if (isalpha(*d))
{
// beginning of tag
if (begin != c)
this->parseContent(begin, c);
// DEBUGP("Parsing beginning of tag\n");
d = this->skipHtmlTag(d, end);
this->parseHtmlTag(c, d);
// continue from the end of the tag
c = d;
begin = c;
break;
}
if (*d == '/')
{
if (begin != c)
this->parseContent(begin, c);
iterator e(d);
++e;
if (e != end && isalpha(*e))
{
// end of tag
// DEBUGP("Parsing end of tag\n");
d = this->skipHtmlTag(d, end);
this->parseHtmlTag(c, d);
}
else
{
// not a conforming end of tag, treat as comment
// as Mozilla does
// DEBUGP("Non conforming end of tag\n");
d = this->skipHtmlTag(d, end);
this->parseComment(c, d);
}
// continue from the end of the tag
c = d;
begin = c;
break;
}
if (*d == '!')
{
// comment
if (begin != c)
this->parseContent(begin, c);
iterator e(d);
++e;
if (e != end && *e == '-' && ++e != end && *e == '-')
{
// DEBUGP("Parsing comment\n");
++e;
d = this->skipHtmlComment(e, end);
}
else
{
d = this->skipHtmlTag(d, end);
}
this->parseComment(c, d);
// continue from the end of the comment
c = d;
begin = c;
break;
}
if (*d == '?' || *d == '%')
{
// something like <?xml or <%VBSCRIPT
if (begin != c)
this->parseContent(begin, c);
d = this->skipHtmlTag(d, end);
this->parseComment(c, d);
// continue from the end of the comment
c = d;
begin = c;
break;
}
}
}
c++;
}
// There may be some text in the end of the document
if (begin != c)
{
this->parseContent(begin, c);
begin = c;
}
}
DONE:
this->endParsing();
return;
}
template <typename _Iterator>
void htmlcxx::HTML::ParserSax::parseComment(_Iterator b, _Iterator c)
{
// DEBUGP("Creating comment node %s\n", std::string(b, c).c_str());
htmlcxx::HTML::Node com_node;
//FIXME: set_tagname shouldn't be needed, but first I must check
//legacy code
std::string comment(b, c);
com_node.tagName(comment);
com_node.text(comment);
com_node.offset(mCurrentOffset);
com_node.length((unsigned int)comment.length());
com_node.isTag(false);
com_node.isComment(true);
mCurrentOffset += com_node.length();
// Call callback method
this->foundComment(com_node);
}
template <typename _Iterator>
void htmlcxx::HTML::ParserSax::parseContent(_Iterator b, _Iterator c)
{
// DEBUGP("Creating text node %s\n", (std::string(b, c)).c_str());
htmlcxx::HTML::Node txt_node;
//FIXME: set_tagname shouldn't be needed, but first I must check
//legacy code
std::string text(b, c);
txt_node.tagName(text);
txt_node.text(text);
txt_node.offset(mCurrentOffset);
txt_node.length((unsigned int)text.length());
txt_node.isTag(false);
txt_node.isComment(false);
mCurrentOffset += txt_node.length();
// Call callback method
this->foundText(txt_node);
}
template <typename _Iterator>
void htmlcxx::HTML::ParserSax::parseHtmlTag(_Iterator b, _Iterator c)
{
_Iterator name_begin(b);
++name_begin;
bool is_end_tag = (*name_begin == '/');
if (is_end_tag) ++name_begin;
_Iterator name_end(name_begin);
while (name_end != c && isalnum(*name_end))
{
++name_end;
}
std::string name(name_begin, name_end);
// DEBUGP("Found %s tag %s\n", is_end_tag ? "closing" : "opening", name.c_str());
if (!is_end_tag)
{
std::string::size_type tag_len = name.length();
for (int i = 0; literal_mode_elem[i].len; ++i)
{
if (tag_len == literal_mode_elem[i].len)
{
#if defined(WIN32) && !defined(__MINGW32__)
if (!_stricmp(name.c_str(), literal_mode_elem[i].str))
#else
if (!strcasecmp(name.c_str(), literal_mode_elem[i].str))
#endif
{
mpLiteral = literal_mode_elem[i].str;
break;
}
}
}
}
htmlcxx::HTML::Node tag_node;
//by now, length is just the size of the tag
std::string text(b, c);
tag_node.length(static_cast<unsigned int>(text.length()));
tag_node.tagName(name);
tag_node.text(text);
tag_node.offset(mCurrentOffset);
tag_node.isTag(true);
tag_node.isComment(false);
mCurrentOffset += tag_node.length();
this->foundTag(tag_node, is_end_tag);
}
template <typename _Iterator>
_Iterator
htmlcxx::HTML::ParserSax::skipHtmlComment(_Iterator c, _Iterator end)
{
while ( c != end ) {
if (*c++ == '-' && c != end && *c == '-')
{
_Iterator d(c);
while (++c != end && isspace(*c));
if (c == end || *c++ == '>') break;
c = d;
}
}
return c;
}
namespace htmlcxx { namespace HTML {
template <typename _Iterator>
static inline
_Iterator find_next_quote(_Iterator c, _Iterator end, char quote)
{
// std::cerr << "generic find" << std::endl;
while (c != end && *c != quote) ++c;
return c;
}
template <>
inline
const char *find_next_quote(const char *c, const char *end, char quote)
{
// std::cerr << "fast find" << std::endl;
const char *d = reinterpret_cast<const char*>(memchr(c, quote, end - c));
if (d) return d;
else return end;
}
}}
template <typename _Iterator>
_Iterator htmlcxx::HTML::ParserSax::skipHtmlTag(_Iterator c, _Iterator end)
{
while (c != end && *c != '>')
{
if (*c != '=')
{
++c;
}
else
{ // found an attribute
++c;
while (c != end && isspace(*c)) ++c;
if (c == end) break;
if (*c == '\"' || *c == '\'')
{
_Iterator save(c);
char quote = *c++;
c = find_next_quote(c, end, quote);
// while (c != end && *c != quote) ++c;
// c = static_cast<char*>(memchr(c, quote, end - c));
if (c != end)
{
++c;
}
else
{
c = save;
++c;
}
// DEBUGP("Quotes: %s\n", std::string(save, c).c_str());
}
}
}
if (c != end) ++c;
return c;
}
|