srctree

Robin Linden parent 2dbca8a0 850ac91c
css: Handle background-color being provided by the background shorthand

inlinesplit
css/parser.h added: 48, removed: 2, total 46
@@ -209,6 +209,8 @@ private:
std::string_view value) const {
if (is_shorthand_edge_property(name)) {
expand_edge_values(declarations, std::string{name}, value);
} else if (name == "background") {
expand_background(declarations, value);
} else if (name == "font") {
expand_font(declarations, value);
} else {
@@ -216,6 +218,25 @@ private:
}
}
 
// https://developer.mozilla.org/en-US/docs/Web/CSS/background
// TODO(robinlinden): This only handles a color being named, and assumes any single item listed is a color.
static void expand_background(
std::map<std::string, std::string, std::less<>> &declarations, std::string_view value) {
declarations["background-image"] = "none";
declarations["background-position"] = "0% 0%";
declarations["background-size"] = "auto auto";
declarations["background-repeat"] = "repeat";
declarations["background-origin"] = "padding-box";
declarations["background-clip"] = "border-box";
declarations["background-attachment"] = "scroll";
declarations["background-color"] = "transparent";
 
Tokenizer tokenizer{value, ' '};
if (tokenizer.size() == 1) {
declarations["background-color"] = tokenizer.get().value();
}
}
 
void expand_edge_values(std::map<std::string, std::string, std::less<>> &declarations,
std::string property,
std::string_view value) const {
 
css/parser_test.cpp added: 48, removed: 2, total 46
@@ -27,6 +27,22 @@ namespace {
return os;
}
 
auto const initial_background_values = std::map<std::string, std::string, std::less<>>{{"background-image", "none"},
{"background-position", "0% 0%"},
{"background-size", "auto auto"},
{"background-repeat", "repeat"},
{"background-origin", "padding-box"},
{"background-clip", "border-box"},
{"background-attachment", "scroll"},
{"background-color", "transparent"}};
 
bool check_initial_background_values(std::map<std::string, std::string, std::less<>> const &declarations) {
return std::all_of(cbegin(declarations), cend(declarations), [](auto const &decl) {
auto it = initial_background_values.find(decl.first);
return it != cend(initial_background_values) && it->second == decl.second;
});
}
 
auto const initial_font_values = std::map<std::string, std::string, std::less<>>{{"font-stretch", "normal"},
{"font-variant", "normal"},
{"font-weight", "normal"},
@@ -410,6 +426,15 @@ int main() {
box_override_with_shorthand("border-style", border_styles, "-style"));
}
 
etest::test("parser: shorthand background color", [] {
auto rules = css::parse("p { background: red }"sv);
require(rules.size() == 1);
 
auto &p = rules[0];
expect_eq(get_and_erase(p.declarations, "background-color"s), "red"sv);
expect(check_initial_background_values(p.declarations));
});
 
etest::test("parser: shorthand font with only size and generic font family", [] {
auto rules = css::parse("p { font: 1.5em sans-serif; }"sv);
require(rules.size() == 1);