srctree

Robin Linden parent 995dc16b 018228a2
style: Fix crash on invalid inline style

inlinesplit
style/style.cpp added: 12, removed: 4, total 8
@@ -13,8 +13,9 @@
#include "dom/dom.h"
#include "util/string.h"
 
#include <spdlog/spdlog.h>
 
#include <algorithm>
#include <cassert>
#include <iterator>
#include <memory>
#include <string>
@@ -172,9 +173,10 @@ MatchingProperties matching_properties(
// parsing only declarations. Replace with the //css2 parser once possible.
auto element_style = css::parse("dummy{"s + style_attr->second + "}"s).rules;
// The above should always parse to 1 rule when using the old parser.
assert(element_style.size() == 1);
if (element_style.size() == 1) {
std::ranges::copy(element_style[0].declarations, std::back_inserter(matched_properties));
} else {
spdlog::warn("Failed to parse inline style '{}' for element '{}'", style_attr->second, element->name);
}
}
}
 
style/style_test.cpp added: 12, removed: 4, total 8
@@ -18,6 +18,7 @@
#include <array>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
 
@@ -55,6 +56,11 @@ void inline_css_tests() {
expect_eq(styled->properties, std::vector{std::pair{css::PropertyId::FontSize, "2px"s}});
});
 
etest::test("inline css: doesn't explode", [] {
dom::Node dom = dom::Element{"div", {{"style", {"aaa"}}}};
std::ignore = style::style_tree(dom, {}, {});
});
 
etest::test("inline css: overrides the stylesheet", [] {
dom::Node dom = dom::Element{"div", {{"style", {"font-size:2px"}}}};
auto styled = style::style_tree(dom, {{css::Rule{{"div"}, {{css::PropertyId::FontSize, "2000px"}}}}}, {});