Perform input validation on feed_cols

Make sure that the value of feed_cols is 2, 3, 4, 5, 6, or 8, so that
the appropriate Pure CSS classes are guaranteed to exist
This commit is contained in:
psvenk 2021-04-21 11:27:24 -04:00
parent f6256fa514
commit 498ddffcf0
No known key found for this signature in database
GPG key ID: 6DE9ADBC26172557
2 changed files with 32 additions and 2 deletions

View file

@ -51,7 +51,7 @@ struct Preferences
@[YAML::Field(converter: Preferences::BoolToString)] @[YAML::Field(converter: Preferences::BoolToString)]
property dark_mode : String = CONFIG.default_user_preferences.dark_mode property dark_mode : String = CONFIG.default_user_preferences.dark_mode
@[JSON::Field(converter: Preferences::ClampInt)] @[JSON::Field(converter: Preferences::ClampFeedCols)]
property feed_cols : Int32 = CONFIG.default_user_preferences.feed_cols property feed_cols : Int32 = CONFIG.default_user_preferences.feed_cols
property latest_only : Bool = CONFIG.default_user_preferences.latest_only property latest_only : Bool = CONFIG.default_user_preferences.latest_only
property listen : Bool = CONFIG.default_user_preferences.listen property listen : Bool = CONFIG.default_user_preferences.listen
@ -147,6 +147,36 @@ struct Preferences
end end
end end
module ClampFeedCols
def self.to_json(value : Int32, json : JSON::Builder)
case value.clamp(2, 8)
when 7
# Pure CSS does not have pure-g-*-1-7
json.number 8
else
json.number value.clamp(2, 8)
end
end
def self.from_json(value : JSON::PullParser) : Int32
value.read_int.clamp(2, 8).to_i32
end
def self.to_yaml(value : Int32, yaml : YAML::Nodes::Builder)
case value.clamp(2, 8)
when 7
# Pure CSS does not have pure-g-*-1-7
yaml.scalar 8
else
yaml.scalar value.clamp(2, 8)
end
end
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Int32
node.value.clamp(2, 9)
end
end
module FamilyConverter module FamilyConverter
def self.to_yaml(value : Socket::Family, yaml : YAML::Nodes::Builder) def self.to_yaml(value : Socket::Family, yaml : YAML::Nodes::Builder)
case value case value

View file

@ -174,7 +174,7 @@
<div class="pure-control-group"> <div class="pure-control-group">
<label for="feed_cols"><%= translate(locale, "Number of videos shown per row: ") %></label> <label for="feed_cols"><%= translate(locale, "Number of videos shown per row: ") %></label>
<input name="feed_cols" id="feed_cols" type="number" value="<%= preferences.feed_cols %>"> <input name="feed_cols" id="feed_cols" type="number" value="<%= preferences.feed_cols %>" min="2" max="8">
</div> </div>
<% if env.get? "user" %> <% if env.get? "user" %>