srctree

Robin Linden parent fcf8b656 7cfbd619
css: Create a less stringly-typed css property id abstraction

inlinesplit
css/BUILD added: 295, removed: 4, total 291
@@ -13,6 +13,7 @@ cc_library(
srcs = [
"default.cpp",
"parse.cpp",
"property_id.cpp",
"rule.cpp",
":default_css.h",
],
@@ -20,6 +21,7 @@ cc_library(
"default.h",
"parse.h",
"parser.h",
"property_id.h",
"rule.h",
],
visibility = ["//visibility:public"],
@@ -58,3 +60,14 @@ cc_test(
"//etest",
],
)
 
cc_test(
name = "property_id_test",
size = "small",
srcs = ["property_id_test.cpp"],
deps = [
":css",
"//etest",
"@fmt",
],
)
 
filename was Deleted added: 295, removed: 4, total 291
@@ -0,0 +1,127 @@
// SPDX-FileCopyrightText: 2022 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "css/property_id.h"
 
#include <algorithm>
#include <map>
#include <string_view>
 
using namespace std::literals;
 
namespace css {
namespace {
 
std::map<std::string_view, PropertyId> const kKnownProperties{
{"azimuth"sv, PropertyId::Azimuth},
{"background-attachment"sv, PropertyId::BackgroundAttachment},
{"background-clip"sv, PropertyId::BackgroundClip},
{"background-color"sv, PropertyId::BackgroundColor},
{"background-image"sv, PropertyId::BackgroundImage},
{"background-origin"sv, PropertyId::BackgroundOrigin},
{"background-position"sv, PropertyId::BackgroundPosition},
{"background-repeat"sv, PropertyId::BackgroundRepeat},
{"background-size"sv, PropertyId::BackgroundSize},
{"border-bottom-color"sv, PropertyId::BorderBottomColor},
{"border-bottom-style"sv, PropertyId::BorderBottomStyle},
{"border-bottom-width"sv, PropertyId::BorderBottomWidth},
{"border-collapse"sv, PropertyId::BorderCollapse},
{"border-left-color"sv, PropertyId::BorderLeftColor},
{"border-left-style"sv, PropertyId::BorderLeftStyle},
{"border-left-width"sv, PropertyId::BorderLeftWidth},
{"border-right-color"sv, PropertyId::BorderRightColor},
{"border-right-style"sv, PropertyId::BorderRightStyle},
{"border-right-width"sv, PropertyId::BorderRightWidth},
{"border-spacing"sv, PropertyId::BorderSpacing},
{"border-top-color"sv, PropertyId::BorderTopColor},
{"border-top-style"sv, PropertyId::BorderTopStyle},
{"border-top-width"sv, PropertyId::BorderTopWidth},
{"caption-side"sv, PropertyId::CaptionSide},
{"color"sv, PropertyId::Color},
{"cursor"sv, PropertyId::Cursor},
{"direction"sv, PropertyId::Direction},
{"display"sv, PropertyId::Display},
{"elevation"sv, PropertyId::Elevation},
{"empty-cells"sv, PropertyId::EmptyCells},
{"font"sv, PropertyId::Font},
{"font-family"sv, PropertyId::FontFamily},
{"font-feature-settings"sv, PropertyId::FontFeatureSettings},
{"font-kerning"sv, PropertyId::FontKerning},
{"font-language-override"sv, PropertyId::FontLanguageOverride},
{"font-optical-sizing"sv, PropertyId::FontOpticalSizing},
{"font-palette"sv, PropertyId::FontPalette},
{"font-size"sv, PropertyId::FontSize},
{"font-size-adjust"sv, PropertyId::FontSizeAdjust},
{"font-stretch"sv, PropertyId::FontStretch},
{"font-style"sv, PropertyId::FontStyle},
{"font-variant"sv, PropertyId::FontVariant},
{"font-variant-alternatives"sv, PropertyId::FontVariantAlternatives},
{"font-variant-caps"sv, PropertyId::FontVariantCaps},
{"font-variant-east-asian"sv, PropertyId::FontVariantEastAsian},
{"font-variant-ligatures"sv, PropertyId::FontVariantLigatures},
{"font-variant-numeric"sv, PropertyId::FontVariantNumeric},
{"font-variant-position"sv, PropertyId::FontVariantPosition},
{"font-variation-settings"sv, PropertyId::FontVariationSettings},
{"font-weight"sv, PropertyId::FontWeight},
{"height"sv, PropertyId::Height},
{"letter-spacing"sv, PropertyId::LetterSpacing},
{"line-height"sv, PropertyId::LineHeight},
{"list-style"sv, PropertyId::ListStyle},
{"list-style-image"sv, PropertyId::ListStyleImage},
{"list-style-position"sv, PropertyId::ListStylePosition},
{"list-style-type"sv, PropertyId::ListStyleType},
{"margin-bottom"sv, PropertyId::MarginBottom},
{"margin-left"sv, PropertyId::MarginLeft},
{"margin-right"sv, PropertyId::MarginRight},
{"margin-top"sv, PropertyId::MarginTop},
{"max-height"sv, PropertyId::MaxHeight},
{"max-width"sv, PropertyId::MaxWidth},
{"min-height"sv, PropertyId::MinHeight},
{"min-width"sv, PropertyId::MinWidth},
{"orphans"sv, PropertyId::Orphans},
{"padding-bottom"sv, PropertyId::PaddingBottom},
{"padding-left"sv, PropertyId::PaddingLeft},
{"padding-right"sv, PropertyId::PaddingRight},
{"padding-top"sv, PropertyId::PaddingTop},
{"pitch"sv, PropertyId::Pitch},
{"pitch-range"sv, PropertyId::PitchRange},
{"quotes"sv, PropertyId::Quotes},
{"richness"sv, PropertyId::Richness},
{"speak"sv, PropertyId::Speak},
{"speak-header"sv, PropertyId::SpeakHeader},
{"speak-numeral"sv, PropertyId::SpeakNumeral},
{"speak-punctuation"sv, PropertyId::SpeakPunctuation},
{"speech-rate"sv, PropertyId::SpeechRate},
{"stress"sv, PropertyId::Stress},
{"text-align"sv, PropertyId::TextAlign},
{"text-indent"sv, PropertyId::TextIndent},
{"text-transform"sv, PropertyId::TextTransform},
{"visibility"sv, PropertyId::Visibility},
{"voice-family"sv, PropertyId::VoiceFamily},
{"volume"sv, PropertyId::Volume},
{"widows"sv, PropertyId::Widows},
{"width"sv, PropertyId::Width},
{"word-spacing"sv, PropertyId::WordSpacing},
};
 
} // namespace
 
PropertyId property_id_from_string(std::string_view id) {
if (!kKnownProperties.contains(id)) {
return PropertyId::Unknown;
}
 
return kKnownProperties.at(id);
}
 
std::string_view to_string(PropertyId id) {
auto it = std::ranges::find_if(kKnownProperties, [id](auto const &entry) { return entry.second == id; });
if (it != end(kKnownProperties)) {
return it->first;
}
 
return "unknown"sv;
}
 
} // namespace css
 
filename was Deleted added: 295, removed: 4, total 291
@@ -0,0 +1,112 @@
// SPDX-FileCopyrightText: 2022 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#ifndef CSS_PROPERTY_ID_H_
#define CSS_PROPERTY_ID_H_
 
#include <string_view>
 
namespace css {
 
enum class PropertyId {
Unknown,
 
Azimuth,
BackgroundAttachment,
BackgroundClip,
BackgroundColor,
BackgroundImage,
BackgroundOrigin,
BackgroundPosition,
BackgroundRepeat,
BackgroundSize,
BorderBottomColor,
BorderBottomStyle,
BorderBottomWidth,
BorderCollapse,
BorderLeftColor,
BorderLeftStyle,
BorderLeftWidth,
BorderRightColor,
BorderRightStyle,
BorderRightWidth,
BorderSpacing,
BorderTopColor,
BorderTopStyle,
BorderTopWidth,
CaptionSide,
Color,
Cursor,
Direction,
Display,
Elevation,
EmptyCells,
Font,
FontFamily,
FontFeatureSettings,
FontKerning,
FontLanguageOverride,
FontOpticalSizing,
FontPalette,
FontSize,
FontSizeAdjust,
FontStretch,
FontStyle,
FontVariant,
FontVariantAlternatives,
FontVariantCaps,
FontVariantEastAsian,
FontVariantLigatures,
FontVariantNumeric,
FontVariantPosition,
FontVariationSettings,
FontWeight,
Height,
LetterSpacing,
LineHeight,
ListStyle,
ListStyleImage,
ListStylePosition,
ListStyleType,
MarginBottom,
MarginLeft,
MarginRight,
MarginTop,
MaxHeight,
MaxWidth,
MinHeight,
MinWidth,
Orphans,
PaddingBottom,
PaddingLeft,
PaddingRight,
PaddingTop,
Pitch,
PitchRange,
Quotes,
Richness,
Speak,
SpeakHeader,
SpeakNumeral,
SpeakPunctuation,
SpeechRate,
Stress,
TextAlign,
TextIndent,
TextTransform,
Visibility,
VoiceFamily,
Volume,
Widows,
Width,
WordSpacing, // When adding an id after this, remember to update the property id -> string test.
};
 
PropertyId property_id_from_string(std::string_view);
 
std::string_view to_string(PropertyId);
 
} // namespace css
 
#endif
 
filename was Deleted added: 295, removed: 4, total 291
@@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2022 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "css/property_id.h"
 
#include "etest/etest.h"
 
#include <fmt/format.h>
 
#include <string_view>
 
using etest::expect;
using etest::expect_eq;
using namespace std::literals;
 
int main() {
etest::test("property_id_from_string", [] {
expect_eq(css::property_id_from_string("width"), css::PropertyId::Width);
expect_eq(css::property_id_from_string("aaaaa"), css::PropertyId::Unknown);
});
 
etest::test("to_string", [] {
expect_eq(css::to_string(css::PropertyId::Width), "width");
expect_eq(css::to_string(css::PropertyId::Unknown), "unknown");
});
 
etest::test("all ids have strings", [] {
auto id = static_cast<int>(css::PropertyId::Unknown) + 1;
// Requires a manual update every time we add something last in the enum.
while (id <= static_cast<int>(css::PropertyId::WordSpacing)) {
expect(css::to_string(static_cast<css::PropertyId>(id)) != "unknown"sv,
fmt::format("Property {} is missing a string mapping", id));
id += 1;
}
});
 
return etest::run_all_tests();
}