Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt accessing only valid item properties #6992

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/jekyll/filters.rb
Expand Up @@ -347,7 +347,7 @@ def item_property(item, property)
@item_property_cache[property][item] ||= begin
if item.respond_to?(:to_liquid)
property.to_s.split(".").reduce(item.to_liquid) do |subvalue, attribute|
parse_sort_input(subvalue[attribute])
parse_sort_input(access_property(subvalue, attribute))
end
elsif item.respond_to?(:data)
parse_sort_input(item.data[property.to_s])
Expand All @@ -357,10 +357,19 @@ def item_property(item, property)
end
end

def access_property(object, key)
return object unless object.respond_to?(:[])

accessor = parse_sort_input(key)
return object if object.is_a?(Array) && !accessor.is_a?(Integer)

object[accessor]
end

# return numeric values as numbers for proper sorting
def parse_sort_input(property)
number_like = %r!\A\s*-?(?:\d+\.?\d*|\.\d+)\s*\Z!
return property.to_f if property =~ number_like
return property.to_f if property.to_s =~ number_like

property
end
Expand Down
10 changes: 10 additions & 0 deletions test/test_filters.rb
Expand Up @@ -906,6 +906,16 @@ def to_liquid
assert_equal 0, @filter.where(hash, "category", "ear").length
end

should "not throw exception when an item is null" do
array = [{}, { "color" => "text" }, { "foo" => "bar" }, nil]
assert_equal [{ "color" => "text" }], @filter.where(array, "color", "text")
end

should "not throw exception when an item itself is an array" do
array = [{}, { "color" => "text" }, [], { "foo" => "bar" }]
assert_equal [{ "color" => "text" }], @filter.where(array, "color", "text")
end

should "stringify during comparison for compatibility with liquid parsing" do
hash = {
"The Words" => { "rating" => 1.2, "featured" => false },
Expand Down