mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
committed by
Tobias Lütke
parent
56f1aa9b4a
commit
daadb1a2d6
+17
-18
@@ -1,52 +1,51 @@
|
||||
require 'set'
|
||||
|
||||
module Liquid
|
||||
|
||||
|
||||
|
||||
parent_object = if defined? BlankObject
|
||||
BlankObject
|
||||
else
|
||||
Object
|
||||
end
|
||||
|
||||
# Strainer is the parent class for the filters system.
|
||||
# New filters are mixed into the strainer class which is then instanciated for each liquid template render run.
|
||||
# Strainer is the parent class for the filters system.
|
||||
# New filters are mixed into the strainer class which is then instanciated for each liquid template render run.
|
||||
#
|
||||
# One of the strainer's responsibilities is to keep malicious method calls out
|
||||
# One of the strainer's responsibilities is to keep malicious method calls out
|
||||
class Strainer < parent_object #:nodoc:
|
||||
INTERNAL_METHOD = /^__/
|
||||
@@required_methods = Set.new([:__send__, :__id__, :respond_to?, :extend, :methods, :class])
|
||||
|
||||
INTERNAL_METHOD = /^__/
|
||||
@@required_methods = Set.new([:__send__, :respond_to?, :extend, :methods, :class, :object_id])
|
||||
|
||||
@@filters = {}
|
||||
|
||||
|
||||
def initialize(context)
|
||||
@context = context
|
||||
end
|
||||
|
||||
|
||||
def self.global_filter(filter)
|
||||
raise ArgumentError, "Passed filter is not a module" unless filter.is_a?(Module)
|
||||
@@filters[filter.name] = filter
|
||||
end
|
||||
|
||||
|
||||
def self.create(context)
|
||||
strainer = Strainer.new(context)
|
||||
@@filters.each { |k,m| strainer.extend(m) }
|
||||
strainer
|
||||
end
|
||||
|
||||
|
||||
def respond_to?(method, include_private = false)
|
||||
method_name = method.to_s
|
||||
return false if method_name =~ INTERNAL_METHOD
|
||||
return false if @@required_methods.include?(method_name)
|
||||
super
|
||||
end
|
||||
|
||||
# remove all standard methods from the bucket so circumvent security
|
||||
# problems
|
||||
instance_methods.each do |m|
|
||||
unless @@required_methods.include?(m.to_sym)
|
||||
|
||||
# remove all standard methods from the bucket so circumvent security
|
||||
# problems
|
||||
instance_methods.each do |m|
|
||||
unless @@required_methods.include?(m.to_sym)
|
||||
undef_method m
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,7 +21,7 @@ module Liquid
|
||||
@name = match[1]
|
||||
if markup.match(/#{FilterSeparator}\s*(.*)/)
|
||||
filters = Regexp.last_match(1).split(/#{FilterSeparator}/)
|
||||
filters.each do |f|
|
||||
filters.each do |f|
|
||||
if matches = f.match(/\s*(\w+)/)
|
||||
filtername = matches[1]
|
||||
filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/).flatten
|
||||
@@ -34,8 +34,7 @@ module Liquid
|
||||
|
||||
def render(context)
|
||||
return '' if @name.nil?
|
||||
output = context[@name]
|
||||
@filters.inject(output) do |output, filter|
|
||||
@filters.inject(context[@name]) do |output, filter|
|
||||
filterargs = filter[1].to_a.collect do |a|
|
||||
context[a]
|
||||
end
|
||||
@@ -45,7 +44,6 @@ module Liquid
|
||||
raise FilterNotFound, "Error - filter '#{filter[0]}' in '#{@markup.strip}' could not be found."
|
||||
end
|
||||
end
|
||||
output
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user