summaryrefslogtreecommitdiff
path: root/Meta
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2021-09-23 12:45:08 +0100
committerAndreas Kling <kling@serenityos.org>2021-09-23 17:47:40 +0200
commitb927972da799c78c0994e95610e84eed20eb15d7 (patch)
treefea84c2b0916f50ac8c039f8b33c8a3b54db5dbd /Meta
parente262596ee18a4e29daf6930b357ca5fc3bbd3075 (diff)
downloadserenity-b927972da799c78c0994e95610e84eed20eb15d7.zip
LibWeb: Add range-checking to property_accepts_value()
For `number` and `integer` types, you can add a range afterwards to add a range check, using similar syntax to that used in the CSS specs. For example: ```json "font-weight": { ... "valid-types": [ "number [1,1000]" ], ... } ``` This limits any numbers to the range `1 <= n <= 1000`.
Diffstat (limited to 'Meta')
-rw-r--r--Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_cpp.cpp23
1 files changed, 21 insertions, 2 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_cpp.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_cpp.cpp
index 6921131674..6553dfc49e 100644
--- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_cpp.cpp
+++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_cpp.cpp
@@ -279,7 +279,9 @@ bool property_accepts_value(PropertyID property_id, StyleValue& style_value)
if (!valid_types.is_empty()) {
for (auto& type : valid_types.values()) {
VERIFY(type.is_string());
- auto type_name = type.as_string();
+ auto type_parts = type.as_string().split_view(' ');
+ auto type_name = type_parts.first();
+ auto type_args = type_parts.size() > 1 ? type_parts[1] : ""sv;
if (type_name == "color") {
property_generator.append(R"~~~(
if (style_value.is_color())
@@ -298,8 +300,25 @@ bool property_accepts_value(PropertyID property_id, StyleValue& style_value)
)~~~");
} else if (type_name == "number" || type_name == "integer") {
// FIXME: Handle integers separately
+ StringView min_value;
+ StringView max_value;
+ if (!type_args.is_empty()) {
+ VERIFY(type_args.starts_with('[') && type_args.ends_with(']'));
+ auto comma_index = type_args.find(',').value();
+ min_value = type_args.substring_view(1, comma_index - 1);
+ max_value = type_args.substring_view(comma_index + 1, type_args.length() - comma_index - 2);
+ }
property_generator.append(R"~~~(
- if (style_value.is_numeric())
+ if (style_value.is_numeric())~~~");
+ if (!min_value.is_empty()) {
+ property_generator.set("minvalue", min_value);
+ property_generator.append(" && (style_value.as_number() >= (float)@minvalue@)");
+ }
+ if (!max_value.is_empty()) {
+ property_generator.set("maxvalue", max_value);
+ property_generator.append(" && (style_value.as_number() <= (float)@maxvalue@)");
+ }
+ property_generator.append(R"~~~()
return true;
)~~~");
} else if (type_name == "string") {