srctree

Robin Linden parent 68724fc8 8cc8e4db
css: Expand the border-{width,color} shorthand properties

inlinesplit
css/parser.cpp added: 31, removed: 6, total 25
@@ -40,7 +40,13 @@ constexpr std::array kBorderStyleKeywords{
// https://developer.mozilla.org/en-US/docs/Web/CSS/border-width
constexpr std::array kBorderWidthKeywords{"thin", "medium", "thick"};
 
constexpr auto kShorthandEdgeProperties = std::array{"padding", "margin", "border-style"};
constexpr auto kShorthandEdgeProperties = std::array{
"padding",
"margin",
"border-color",
"border-style",
"border-width",
};
 
constexpr auto kAbsoluteSizeKeywords =
std::array{"xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"};
@@ -744,11 +750,18 @@ void Parser::expand_edge_values(Declarations &declarations, std::string_view pro
}
// NOLINTEND(bugprone-unchecked-optional-access)
std::string_view post_fix;
// The border properties aren't as simple as the padding or margin ones.
if (property == "border-style") {
// border-style is a bit special as we want border-top-style instead of border-style-top-style.
property = "border";
post_fix = "-style";
} else if (property == "border-width") {
property = "border";
post_fix = "-width";
} else if (property == "border-color") {
property = "border";
post_fix = "-color";
}
 
declarations.insert_or_assign(
property_id_from_string(fmt::format("{}-top{}", property, post_fix)), std::string{top});
declarations.insert_or_assign(
 
css/parser_test.cpp added: 31, removed: 6, total 25
@@ -414,7 +414,7 @@ int main() {
auto rules = css::parse(fmt::format("p {{ {}: {}; }}"sv, property, value)).rules;
require(rules.size() == 1);
 
if (property == "border-style") {
if (property == "border-style" || property == "border-color" || property == "border-width") {
property = "border";
}
 
@@ -439,6 +439,12 @@ int main() {
std::string border_style{"dashed"};
etest::test("parser: shorthand border-style, one value",
box_shorthand_one_value("border-style", border_style, "-style"));
 
etest::test(
"parser: shorthand border-color, one value", box_shorthand_one_value("border-color", "red", "-color"));
 
etest::test(
"parser: shorthand border-width, one value", box_shorthand_one_value("border-width", "10px", "-width"));
}
 
auto box_shorthand_two_values = [](std::string property,
@@ -519,7 +525,7 @@ int main() {
.rules;
require(rules.size() == 1);
 
if (property == "border-style") {
if (property == "border-style" || property == "border-color" || property == "border-width") {
property = "border";
}
 
@@ -544,6 +550,12 @@ int main() {
auto border_styles = std::array{"groove"s, "dashed"s, "solid"s, "dotted"s};
etest::test("parser: shorthand border-style, four values",
box_shorthand_four_values("border-style", border_styles, "-style"));
 
etest::test("parser: shorthand border-color, four values",
box_shorthand_four_values("border-color", std::array{"red"s, "green"s, "blue"s, "cyan"s}, "-color"));
 
etest::test("parser: shorthand border-width, four values",
box_shorthand_four_values("border-width", size_values, "-width"));
}
 
auto box_shorthand_overridden = [](std::string property,