srctree

Robin Linden parent af1a1f13 d660d72e
all: Simplify trying to get values out of std::variants

inlinesplit
browser/gui/app.cpp added: 40, removed: 31, total 9
@@ -42,10 +42,15 @@ auto constexpr kMouseWheelScrollFactor = 10;
 
std::optional<std::string_view> try_get_text_content(dom::Document const &doc, std::string_view xpath) {
auto nodes = dom::nodes_by_xpath(doc.html(), xpath);
if (nodes.empty() || nodes[0]->children.empty() || !std::holds_alternative<dom::Text>(nodes[0]->children[0])) {
if (nodes.empty() || nodes[0]->children.empty()) {
return std::nullopt;
}
return std::get<dom::Text>(nodes[0]->children[0]).text;
 
if (auto const *text = std::get_if<dom::Text>(&nodes[0]->children[0])) {
return text->text;
}
 
return std::nullopt;
}
 
void ensure_has_scheme(std::string &url) {
@@ -66,8 +71,8 @@ std::string element_text(std::vector<dom::Node const *> const &dom_nodes) {
return "a: "s + std::string{*uri};
}
 
if (std::holds_alternative<dom::Text>(*dom_nodes[0])) {
return std::get<dom::Text>(*dom_nodes[0]).text;
if (auto const *text = std::get_if<dom::Text>(dom_nodes[0])) {
return text->text;
}
 
return std::get<dom::Element>(*dom_nodes[0]).name;
 
engine/engine.cpp added: 40, removed: 31, total 9
@@ -25,10 +25,15 @@ namespace {
 
std::optional<std::string_view> try_get_text_content(dom::Document const &doc, std::string_view xpath) {
auto nodes = dom::nodes_by_xpath(doc.html(), xpath);
if (nodes.empty() || nodes[0]->children.empty() || !std::holds_alternative<dom::Text>(nodes[0]->children[0])) {
if (nodes.empty() || nodes[0]->children.empty()) {
return std::nullopt;
}
return std::get<dom::Text>(nodes[0]->children[0]).text;
 
if (auto const *text = std::get_if<dom::Text>(&nodes[0]->children[0])) {
return text->text;
}
 
return std::nullopt;
}
 
std::optional<std::string> zlib_decode(std::string_view data) {
 
html2/tokenizer.cpp added: 40, removed: 31, total 9
@@ -263,8 +263,8 @@ void Tokenizer::run() {
}
 
auto append_to_tag_name = [&](auto text) {
if (std::holds_alternative<StartTagToken>(current_token_)) {
std::get<StartTagToken>(current_token_).tag_name += text;
if (auto *start_tag = std::get_if<StartTagToken>(&current_token_)) {
start_tag->tag_name += text;
} else {
std::get<EndTagToken>(current_token_).tag_name += text;
}
@@ -907,8 +907,8 @@ void Tokenizer::run() {
}
 
auto append_to_current_attribute_name = [&](auto text) {
if (std::holds_alternative<StartTagToken>(current_token_)) {
std::get<StartTagToken>(current_token_).attributes.back().name += text;
if (auto *start_tag = std::get_if<StartTagToken>(&current_token_)) {
start_tag->attributes.back().name += text;
} else {
std::get<EndTagToken>(current_token_).attributes.back().name += text;
}
@@ -1129,8 +1129,8 @@ void Tokenizer::run() {
 
switch (*c) {
case '>':
if (std::holds_alternative<StartTagToken>(current_token_)) {
std::get<StartTagToken>(current_token_).self_closing = true;
if (auto *start_tag = std::get_if<StartTagToken>(&current_token_)) {
start_tag->self_closing = true;
} else {
std::get<EndTagToken>(current_token_).self_closing = true;
}
@@ -2285,8 +2285,8 @@ void Tokenizer::emit(ParseError error) {
}
 
void Tokenizer::emit(Token &&token) {
if (std::holds_alternative<StartTagToken>(token)) {
last_start_tag_name_ = std::get<StartTagToken>(token).tag_name;
if (auto const *start_tag = std::get_if<StartTagToken>(&token)) {
last_start_tag_name_ = start_tag->tag_name;
}
on_emit_(*this, std::move(token));
}
@@ -2313,16 +2313,16 @@ bool Tokenizer::is_eof() const {
}
 
void Tokenizer::start_attribute_in_current_tag_token(Attribute attr) {
if (std::holds_alternative<StartTagToken>(current_token_)) {
std::get<StartTagToken>(current_token_).attributes.push_back(std::move(attr));
if (auto *start_tag = std::get_if<StartTagToken>(&current_token_)) {
start_tag->attributes.push_back(std::move(attr));
} else {
std::get<EndTagToken>(current_token_).attributes.push_back(std::move(attr));
}
}
 
Attribute &Tokenizer::current_attribute() {
if (std::holds_alternative<StartTagToken>(current_token_)) {
return std::get<StartTagToken>(current_token_).attributes.back();
if (auto *start_tag = std::get_if<StartTagToken>(&current_token_)) {
return start_tag->attributes.back();
} else {
return std::get<EndTagToken>(current_token_).attributes.back();
}
@@ -2354,8 +2354,8 @@ void Tokenizer::emit_temporary_buffer_as_character_tokens() {
}
 
bool Tokenizer::is_appropriate_end_tag_token(Token const &token) const {
if (std::holds_alternative<EndTagToken>(token)) {
return std::get<EndTagToken>(token).tag_name == last_start_tag_name_;
if (auto const *end_tag = std::get_if<EndTagToken>(&token)) {
return end_tag->tag_name == last_start_tag_name_;
}
return false;
}
 
layout/layout.cpp added: 40, removed: 31, total 9
@@ -126,10 +126,9 @@ void calculate_left_and_right_margin(LayoutBox &box,
void calculate_width_and_margin(LayoutBox &box, geom::Rect const &parent, int const font_size) {
assert(box.node != nullptr);
 
if (std::holds_alternative<dom::Text>(box.node->node)) {
if (auto const *text_node = std::get_if<dom::Text>(&box.node->node)) {
// TODO(robinlinden): Measure the text for real.
auto text_node = std::get<dom::Text>(box.node->node);
box.dimensions.content.width = std::min(parent.width, static_cast<int>(text_node.text.size()) * font_size / 2);
box.dimensions.content.width = std::min(parent.width, static_cast<int>(text_node->text.size()) * font_size / 2);
return;
}
 
 
style/style.cpp added: 40, removed: 31, total 9
@@ -102,9 +102,9 @@ void style_tree_impl(StyledNode &current, dom::Node const &root, std::vector<css
}
}
 
current.properties = std::holds_alternative<dom::Element>(root)
? matching_rules(std::get<dom::Element>(root), stylesheet)
: std::vector<std::pair<css::PropertyId, std::string>>{};
if (auto const *element = std::get_if<dom::Element>(&root)) {
current.properties = matching_rules(*element, stylesheet);
}
}
} // namespace