srctree

Robin Linden parent 566eda11 fac0f15e
css: Expand the outline shorthand property

inlinesplit
css/parser.cpp added: 87, removed: 5, total 82
@@ -446,6 +446,8 @@ void Parser::add_declaration(Declarations &declarations, std::string_view name,
expand_flex_flow(declarations, value);
} else if (is_in_array<kBorderShorthandProperties>(name)) {
expand_border(name, declarations, value);
} else if (name == "outline") {
expand_outline(declarations, value);
} else {
declarations.insert_or_assign(property_id_from_string(name), std::string{value});
}
@@ -537,6 +539,13 @@ void Parser::expand_border_or_outline_impl(
declarations.insert_or_assign(ids.width, width.value_or("medium"));
}
 
// https://developer.mozilla.org/en-US/docs/Web/CSS/outline
void Parser::expand_outline(Declarations &declarations, std::string_view outline) {
static constexpr BorderOrOutlinePropertyIds kIds{
PropertyId::OutlineColor, PropertyId::OutlineStyle, PropertyId::OutlineWidth};
expand_border_or_outline_impl(kIds, declarations, outline);
}
 
// 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.
void Parser::expand_background(Declarations &declarations, std::string_view value) {
 
css/parser.h added: 87, removed: 5, total 82
@@ -65,6 +65,8 @@ private:
};
static void expand_border_or_outline_impl(BorderOrOutlinePropertyIds, Declarations &, std::string_view);
 
static void expand_outline(Declarations &, std::string_view);
 
// 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(Declarations &, std::string_view value);
 
css/parser_test.cpp added: 87, removed: 5, total 82
@@ -105,10 +105,75 @@ void text_decoration_tests() {
});
}
 
void outline_tests() {
etest::test("parser: outline shorthand, all values", [] {
auto rules = css::parse("p { outline: 5px black solid; }").rules;
auto const &p = rules.at(0);
expect_eq(p.declarations,
std::map<css::PropertyId, std::string>{
{css::PropertyId::OutlineColor, "black"s},
{css::PropertyId::OutlineStyle, "solid"s},
{css::PropertyId::OutlineWidth, "5px"s},
});
});
 
etest::test("parser: outline shorthand, color+style", [] {
auto rules = css::parse("p { outline: #123 dotted; }").rules;
auto const &p = rules.at(0);
expect_eq(p.declarations,
std::map<css::PropertyId, std::string>{
{css::PropertyId::OutlineColor, "#123"s},
{css::PropertyId::OutlineStyle, "dotted"s},
{css::PropertyId::OutlineWidth, "medium"s},
});
});
 
etest::test("parser: outline shorthand, width+style", [] {
auto rules = css::parse("p { outline: ridge 30em; }").rules;
auto const &p = rules.at(0);
expect_eq(p.declarations,
std::map<css::PropertyId, std::string>{
{css::PropertyId::OutlineColor, "currentcolor"s},
{css::PropertyId::OutlineStyle, "ridge"s},
{css::PropertyId::OutlineWidth, "30em"s},
});
});
 
etest::test("parser: outline shorthand, width", [] {
auto rules = css::parse("p { outline: thin; }").rules;
auto const &p = rules.at(0);
expect_eq(p.declarations,
std::map<css::PropertyId, std::string>{
{css::PropertyId::OutlineColor, "currentcolor"s},
{css::PropertyId::OutlineStyle, "none"s},
{css::PropertyId::OutlineWidth, "thin"s},
});
});
 
etest::test("parser: outline shorthand, width, first character a dot", [] {
auto rules = css::parse("p { outline: .3em; }").rules;
auto const &p = rules.at(0);
expect_eq(p.declarations,
std::map<css::PropertyId, std::string>{
{css::PropertyId::OutlineColor, "currentcolor"s},
{css::PropertyId::OutlineStyle, "none"s},
{css::PropertyId::OutlineWidth, ".3em"s},
});
});
 
etest::test("parser: outline shorthand, too many values", [] {
auto rules = css::parse("p { outline: outset #123 none solid; }").rules;
require(rules.size() == 1);
auto const &p = rules[0];
expect_eq(p.declarations, std::map<css::PropertyId, std::string>{});
});
}
 
} // namespace
 
int main() {
text_decoration_tests();
outline_tests();
 
etest::test("parser: simple rule", [] {
auto rules = css::parse("body { width: 50px; }"sv).rules;
 
css/property_id.cpp added: 87, removed: 5, total 82
@@ -89,6 +89,9 @@ std::map<std::string_view, PropertyId> const known_properties{
{"min-height"sv, PropertyId::MinHeight},
{"min-width"sv, PropertyId::MinWidth},
{"orphans"sv, PropertyId::Orphans},
{"outline-color", PropertyId::OutlineColor},
{"outline-style", PropertyId::OutlineStyle},
{"outline-width", PropertyId::OutlineWidth},
{"padding-bottom"sv, PropertyId::PaddingBottom},
{"padding-left"sv, PropertyId::PaddingLeft},
{"padding-right"sv, PropertyId::PaddingRight},
 
css/property_id.h added: 87, removed: 5, total 82
@@ -86,6 +86,9 @@ enum class PropertyId {
MinHeight,
MinWidth,
Orphans,
OutlineColor,
OutlineStyle,
OutlineWidth,
PaddingBottom,
PaddingLeft,
PaddingRight,