File size: 16,614 Bytes
ef28ed2 070294b ef28ed2 070294b ef28ed2 070294b ef28ed2 | 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | #include "dom/html_parser.hpp"
#include <algorithm>
#include <cctype>
#include <unordered_set>
namespace astra {
namespace {
std::string to_lower(std::string_view s) {
std::string out(s);
std::transform(out.begin(), out.end(), out.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return out;
}
bool is_void_tag(std::string_view tag) {
static const std::unordered_set<std::string_view> kVoid = {
"area", "base", "br", "col", "embed", "hr", "img", "input",
"link", "meta", "param", "source", "track", "wbr"
};
return kVoid.count(tag) > 0;
}
std::string decode_entities(std::string_view s) {
std::string out;
out.reserve(s.size());
for (size_t i = 0; i < s.size(); ++i) {
if (s[i] == '&') {
size_t semi = s.find(';', i + 1);
if (semi != std::string_view::npos && semi - i <= 10) {
std::string_view ent = s.substr(i + 1, semi - i - 1);
if (ent == "amp") { out.push_back('&'); i = semi; continue; }
if (ent == "lt") { out.push_back('<'); i = semi; continue; }
if (ent == "gt") { out.push_back('>'); i = semi; continue; }
if (ent == "quot") { out.push_back('"'); i = semi; continue; }
if (ent == "apos" || ent == "#39") { out.push_back('\''); i = semi; continue; }
if (ent == "nbsp") { out.push_back(' '); i = semi; continue; }
if (ent == "copy") { out += "(c)"; i = semi; continue; }
if (ent == "mdash") { out += "--"; i = semi; continue; }
if (ent == "ndash") { out += "-"; i = semi; continue; }
if (!ent.empty() && ent[0] == '#') {
unsigned code = 0;
if (ent.size() > 1 && (ent[1] == 'x' || ent[1] == 'X')) {
code = std::strtoul(std::string(ent.substr(2)).c_str(), nullptr, 16);
} else {
code = std::strtoul(std::string(ent.substr(1)).c_str(), nullptr, 10);
}
if (code >= 32 && code < 127) {
out.push_back(static_cast<char>(code));
i = semi;
continue;
}
}
}
}
out.push_back(s[i]);
}
return out;
}
bool matches_simple_selector(const DomNode* node, std::string_view sel) {
if (!node || node->type != NodeType::Element || sel.empty()) return false;
while (!sel.empty() && std::isspace(static_cast<unsigned char>(sel.front()))) sel.remove_prefix(1);
while (!sel.empty() && std::isspace(static_cast<unsigned char>(sel.back()))) sel.remove_suffix(1);
if (sel == "*") return true;
if (sel[0] == '#') return node->id() == sel.substr(1);
if (sel[0] == '.') return node->has_class(sel.substr(1));
// Support tag.class or tag#id
size_t dot = sel.find('.');
if (dot != std::string_view::npos) {
return node->tag == to_lower(sel.substr(0, dot)) && node->has_class(sel.substr(dot + 1));
}
size_t hash = sel.find('#');
if (hash != std::string_view::npos) {
return node->tag == to_lower(sel.substr(0, hash)) && node->id() == sel.substr(hash + 1);
}
return node->tag == to_lower(sel);
}
void collect_matches(DomNode* node, std::string_view sel, std::vector<DomNode*>& out, bool first_only) {
if (!node) return;
if (first_only && !out.empty()) return;
if (node->type == NodeType::Element && matches_simple_selector(node, sel)) {
out.push_back(node);
if (first_only) return;
}
for (DomNode* ch : node->children) {
collect_matches(ch, sel, out, first_only);
if (first_only && !out.empty()) return;
}
}
void collect_tags_text(const DomNode* node, std::string_view tag, std::vector<std::string>& out) {
if (!node) return;
if (node->type == NodeType::Element && node->tag == tag) {
out.push_back(node->text_content());
return;
}
for (const DomNode* ch : node->children) {
collect_tags_text(ch, tag, out);
}
}
} // namespace
std::string DomNode::get_attr(std::string_view name) const {
std::string low = to_lower(name);
for (const auto& a : attrs) {
if (a.name == low) return a.value;
}
return {};
}
void DomNode::set_attr(std::string_view name, std::string_view value) {
std::string low = to_lower(name);
for (auto& a : attrs) {
if (a.name == low) {
a.value = std::string(value);
return;
}
}
attrs.push_back(DomAttr{std::move(low), std::string(value)});
}
void DomNode::remove_attr(std::string_view name) {
std::string low = to_lower(name);
attrs.erase(std::remove_if(attrs.begin(), attrs.end(),
[&](const DomAttr& a) { return a.name == low; }),
attrs.end());
}
bool DomNode::has_class(std::string_view cls) const {
std::string classes = get_attr("class");
if (classes.empty()) return false;
size_t pos = 0;
while (pos < classes.size()) {
while (pos < classes.size() && std::isspace(static_cast<unsigned char>(classes[pos]))) ++pos;
size_t start = pos;
while (pos < classes.size() && !std::isspace(static_cast<unsigned char>(classes[pos]))) ++pos;
if (pos > start && std::string_view(classes.data() + start, pos - start) == cls) {
return true;
}
}
return false;
}
void DomNode::add_class(std::string_view cls) {
if (cls.empty() || has_class(cls)) return;
std::string cur = get_attr("class");
if (!cur.empty()) cur.push_back(' ');
cur.append(cls);
set_attr("class", cur);
}
void DomNode::remove_class(std::string_view cls) {
std::string cur = get_attr("class");
if (cur.empty()) return;
std::string rebuilt;
size_t pos = 0;
while (pos < cur.size()) {
while (pos < cur.size() && std::isspace(static_cast<unsigned char>(cur[pos]))) ++pos;
size_t start = pos;
while (pos < cur.size() && !std::isspace(static_cast<unsigned char>(cur[pos]))) ++pos;
if (pos > start) {
std::string_view tok(cur.data() + start, pos - start);
if (tok != cls) {
if (!rebuilt.empty()) rebuilt.push_back(' ');
rebuilt.append(tok);
}
}
}
set_attr("class", rebuilt);
}
bool DomNode::toggle_class(std::string_view cls) {
if (has_class(cls)) {
remove_class(cls);
return false;
}
add_class(cls);
return true;
}
std::string DomNode::text_content() const {
if (type == NodeType::Text) return text;
std::string out;
for (const DomNode* ch : children) {
out += ch->text_content();
}
return out;
}
void DomNode::set_text_content(SlabArena& arena, std::string_view new_text) {
children.clear();
DomNode* t = arena.create<DomNode>();
t->type = NodeType::Text;
t->text = std::string(new_text);
t->parent = this;
children.push_back(t);
}
namespace {
void serialize_node_html(const DomNode* n, std::string& out) {
if (!n) return;
if (n->type == NodeType::Text) {
out += n->text;
return;
}
if (n->type == NodeType::Element) {
out.push_back('<');
out += n->tag;
for (const auto& a : n->attrs) {
out.push_back(' ');
out += a.name;
out += "=\"";
out += a.value;
out.push_back('"');
}
out.push_back('>');
if (!is_void_tag(n->tag)) {
for (const DomNode* ch : n->children) serialize_node_html(ch, out);
out += "</";
out += n->tag;
out.push_back('>');
}
}
}
} // namespace
std::string DomNode::inner_html() const {
std::string out;
for (const DomNode* ch : children) serialize_node_html(ch, out);
return out;
}
void DomNode::set_inner_html(SlabArena& arena, std::string_view html) {
children.clear();
if (html.empty()) return;
HtmlDocument frag = HtmlDocument::parse(arena, html);
DomNode* src_root = frag.root();
if (!src_root) return;
for (DomNode* ch : src_root->children) {
ch->parent = this;
children.push_back(ch);
}
}
HtmlDocument::HtmlDocument(SlabArena& arena) : arena_(arena) {
root_ = arena_.create<DomNode>();
root_->type = NodeType::Document;
root_->tag = "#document";
node_count_ = 1;
}
DomNode* HtmlDocument::body() const {
DomNode* b = query_selector("body");
return b ? b : root_;
}
std::string HtmlDocument::title() const {
DomNode* t = query_selector("title");
if (!t) return {};
std::string s = t->text_content();
while (!s.empty() && std::isspace(static_cast<unsigned char>(s.front()))) s.erase(s.begin());
while (!s.empty() && std::isspace(static_cast<unsigned char>(s.back()))) s.pop_back();
return s;
}
std::string HtmlDocument::collect_stylesheets() const {
std::vector<std::string> sheets;
collect_tags_text(root_, "style", sheets);
std::string joined;
for (const auto& s : sheets) {
joined += s;
joined.push_back('\n');
}
return joined;
}
std::vector<std::string> HtmlDocument::collect_scripts() const {
std::vector<std::string> scripts;
collect_tags_text(root_, "script", scripts);
return scripts;
}
DomNode* HtmlDocument::query_selector(std::string_view selector) const {
std::vector<DomNode*> matches;
collect_matches(root_, selector, matches, true);
return matches.empty() ? nullptr : matches[0];
}
std::vector<DomNode*> HtmlDocument::query_selector_all(std::string_view selector) const {
std::vector<DomNode*> matches;
collect_matches(root_, selector, matches, false);
return matches;
}
HtmlDocument HtmlDocument::parse(SlabArena& arena, std::string_view html) {
HtmlDocument doc(arena);
std::vector<DomNode*> stack;
stack.push_back(doc.root_);
size_t pos = 0;
const size_t n = html.size();
while (pos < n) {
if (html[pos] == '<') {
// Comment
if (html.substr(pos, 4) == "<!--") {
size_t end = html.find("-->", pos + 4);
pos = (end == std::string_view::npos) ? n : end + 3;
continue;
}
// DOCTYPE or processing instruction
if (pos + 1 < n && (html[pos + 1] == '!' || html[pos + 1] == '?')) {
size_t end = html.find('>', pos + 2);
pos = (end == std::string_view::npos) ? n : end + 1;
continue;
}
// Closing tag
if (pos + 1 < n && html[pos + 1] == '/') {
size_t name_start = pos + 2;
size_t name_end = name_start;
while (name_end < n && !std::isspace(static_cast<unsigned char>(html[name_end])) && html[name_end] != '>') {
++name_end;
}
std::string close_tag = to_lower(html.substr(name_start, name_end - name_start));
size_t gt = html.find('>', name_end);
pos = (gt == std::string_view::npos) ? n : gt + 1;
// Pop up to matching tag
for (size_t i = stack.size(); i-- > 1;) {
if (stack[i]->tag == close_tag) {
stack.resize(i);
break;
}
}
continue;
}
// Opening tag
size_t name_start = pos + 1;
size_t name_end = name_start;
while (name_end < n && !std::isspace(static_cast<unsigned char>(html[name_end])) &&
html[name_end] != '>' && html[name_end] != '/') {
++name_end;
}
if (name_end == name_start) {
++pos;
continue;
}
std::string tag = to_lower(html.substr(name_start, name_end - name_start));
DomNode* elem = arena.create<DomNode>();
elem->type = NodeType::Element;
elem->tag = tag;
elem->parent = stack.back();
stack.back()->children.push_back(elem);
++doc.node_count_;
// Parse attributes
pos = name_end;
bool self_closing = false;
while (pos < n) {
while (pos < n && std::isspace(static_cast<unsigned char>(html[pos]))) ++pos;
if (pos >= n) break;
if (html[pos] == '>') {
++pos;
break;
}
if (html[pos] == '/' && pos + 1 < n && html[pos + 1] == '>') {
self_closing = true;
pos += 2;
break;
}
size_t a_start = pos;
while (pos < n && !std::isspace(static_cast<unsigned char>(html[pos])) &&
html[pos] != '=' && html[pos] != '>' && html[pos] != '/') {
++pos;
}
if (pos == a_start) { ++pos; continue; }
std::string attr_name = to_lower(html.substr(a_start, pos - a_start));
while (pos < n && std::isspace(static_cast<unsigned char>(html[pos]))) ++pos;
std::string attr_val;
if (pos < n && html[pos] == '=') {
++pos;
while (pos < n && std::isspace(static_cast<unsigned char>(html[pos]))) ++pos;
if (pos < n && (html[pos] == '"' || html[pos] == '\'')) {
char quote = html[pos++];
size_t v_start = pos;
while (pos < n && html[pos] != quote) ++pos;
attr_val = decode_entities(html.substr(v_start, pos - v_start));
if (pos < n && html[pos] == quote) ++pos;
} else {
size_t v_start = pos;
while (pos < n && !std::isspace(static_cast<unsigned char>(html[pos])) && html[pos] != '>') ++pos;
attr_val = decode_entities(html.substr(v_start, pos - v_start));
}
}
elem->attrs.push_back(DomAttr{std::move(attr_name), std::move(attr_val)});
}
// Raw text elements (<style>, <script>)
if (!self_closing && (tag == "style" || tag == "script")) {
std::string close_seq = "</" + tag;
size_t end_raw = pos;
while (end_raw < n) {
size_t lt = html.find('<', end_raw);
if (lt == std::string_view::npos) { end_raw = n; break; }
if (lt + close_seq.size() <= n &&
to_lower(html.substr(lt, close_seq.size())) == close_seq) {
end_raw = lt;
break;
}
end_raw = lt + 1;
}
if (end_raw > pos) {
DomNode* txt = arena.create<DomNode>();
txt->type = NodeType::Text;
txt->text = std::string(html.substr(pos, end_raw - pos));
txt->parent = elem;
elem->children.push_back(txt);
++doc.node_count_;
}
size_t close_gt = html.find('>', end_raw);
pos = (close_gt == std::string_view::npos) ? n : close_gt + 1;
continue;
}
if (!self_closing && !is_void_tag(tag)) {
stack.push_back(elem);
}
} else {
size_t next_lt = html.find('<', pos);
std::string_view raw = (next_lt == std::string_view::npos) ? html.substr(pos) : html.substr(pos, next_lt - pos);
pos = (next_lt == std::string_view::npos) ? n : next_lt;
std::string decoded = decode_entities(raw);
bool all_ws = std::all_of(decoded.begin(), decoded.end(),
[](unsigned char c) { return std::isspace(c); });
if (!all_ws) {
DomNode* txt = arena.create<DomNode>();
txt->type = NodeType::Text;
txt->text = std::move(decoded);
txt->parent = stack.back();
stack.back()->children.push_back(txt);
++doc.node_count_;
}
}
}
return doc;
}
} // namespace astra
|