mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 08:15:41 +03:00
Compare commits
21 Commits
ragel
...
cache_var_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
717174a57a | ||
|
|
b8fbd2b4fa | ||
|
|
ba5a9f2e47 | ||
|
|
1e309ba74b | ||
|
|
485340713a | ||
|
|
2af4ea1295 | ||
|
|
c5dfcd29b0 | ||
|
|
f9c289372d | ||
|
|
f7d1e1d0c1 | ||
|
|
28fd2222c8 | ||
|
|
9913895b81 | ||
|
|
d706db3bd7 | ||
|
|
38b4543bf1 | ||
|
|
1300210f05 | ||
|
|
a48e162237 | ||
|
|
7bcb565668 | ||
|
|
c3e6cde67f | ||
|
|
0b36540b78 | ||
|
|
50bd34fd78 | ||
|
|
ee41b3f4a3 | ||
|
|
05d9976e16 |
@@ -1,5 +1,13 @@
|
||||
# Liquid Version History
|
||||
|
||||
## 2.5.0 / 2013-03-06
|
||||
|
||||
* Prevent Object methods from being called on drops
|
||||
* Avoid symbol injection from liquid
|
||||
* Added break and continue statements
|
||||
* Fix filter parser for args without space separators
|
||||
* Add support for filter keyword arguments
|
||||
|
||||
## 2.4.0 / 2012-08-03
|
||||
|
||||
* Performance improvements
|
||||
|
||||
5
Rakefile
5
Rakefile
@@ -7,11 +7,6 @@ require 'rubygems/package_task'
|
||||
|
||||
task :default => 'test'
|
||||
|
||||
task :ragel do
|
||||
sh "find . -name '*.rl' | xargs ragel -R -F1"
|
||||
end
|
||||
|
||||
|
||||
Rake::TestTask.new(:test) do |t|
|
||||
t.libs << '.' << 'lib' << 'test'
|
||||
t.test_files = FileList['test/liquid/**/*_test.rb']
|
||||
|
||||
@@ -20,14 +20,15 @@
|
||||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
module Liquid
|
||||
WordRegex = RUBY_VERSION < "1.9" ? '\w' : '[[:word:]]'
|
||||
FilterSeparator = /\|/
|
||||
ArgumentSeparator = ','
|
||||
FilterArgumentSeparator = ':'
|
||||
VariableAttributeSeparator = '.'
|
||||
TagStart = /\{\%/
|
||||
TagEnd = /\%\}/
|
||||
VariableSignature = /\(?[\w\-\.\[\]]\)?/
|
||||
VariableSegment = /[\w\-]/
|
||||
VariableSignature = /\(?[#{WordRegex}\-\.\[\]]\)?/o
|
||||
VariableSegment = /[#{WordRegex}\-]/o
|
||||
VariableStart = /\{\{/
|
||||
VariableEnd = /\}\}/
|
||||
VariableIncompleteEnd = /\}\}?/
|
||||
@@ -38,7 +39,7 @@ module Liquid
|
||||
OtherFilterArgument = /#{ArgumentSeparator}(?:#{StrictQuotedFragment})/o
|
||||
SpacelessFilter = /^(?:'[^']+'|"[^"]+"|[^'"])*#{FilterSeparator}(?:#{StrictQuotedFragment})(?:#{FirstFilterArgument}(?:#{OtherFilterArgument})*)?/o
|
||||
Expression = /(?:#{QuotedFragment}(?:#{SpacelessFilter})*)/o
|
||||
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/o
|
||||
TagAttributes = /(#{WordRegex}+)\s*\:\s*(#{QuotedFragment})/o
|
||||
AnyStartingTag = /\{\{|\{\%/
|
||||
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/o
|
||||
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/o
|
||||
@@ -62,7 +63,6 @@ require 'liquid/standardfilters'
|
||||
require 'liquid/condition'
|
||||
require 'liquid/module_ex'
|
||||
require 'liquid/utils'
|
||||
require 'liquid/parser'
|
||||
|
||||
# Load all the tags of the standard library
|
||||
#
|
||||
|
||||
@@ -3,7 +3,7 @@ module Liquid
|
||||
class Block < Tag
|
||||
IsTag = /^#{TagStart}/o
|
||||
IsVariable = /^#{VariableStart}/o
|
||||
FullToken = /^#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}$/o
|
||||
FullToken = /^#{TagStart}\s*(#{WordRegex}+)\s*(.*)?#{TagEnd}$/o
|
||||
ContentOfVariable = /^#{VariableStart}(.*)#{VariableEnd}$/o
|
||||
|
||||
def parse(tokens)
|
||||
|
||||
@@ -25,6 +25,7 @@ module Liquid
|
||||
squash_instance_assigns_with_environments
|
||||
|
||||
@interrupts = []
|
||||
@variable_cache = {}
|
||||
end
|
||||
|
||||
def strainer
|
||||
@@ -40,6 +41,7 @@ module Liquid
|
||||
|
||||
filters.each do |f|
|
||||
raise ArgumentError, "Expected module but got: #{f.class}" unless f.is_a?(Module)
|
||||
Strainer.add_known_filter(f)
|
||||
strainer.extend(f)
|
||||
end
|
||||
end
|
||||
@@ -72,26 +74,25 @@ module Liquid
|
||||
end
|
||||
|
||||
def invoke(method, *args)
|
||||
if strainer.respond_to?(method)
|
||||
strainer.__send__(method, *args)
|
||||
else
|
||||
args.first
|
||||
end
|
||||
strainer.invoke(method, *args)
|
||||
end
|
||||
|
||||
# Push new local scope on the stack. use <tt>Context#stack</tt> instead
|
||||
def push(new_scope={})
|
||||
@variable_cache = {}
|
||||
@scopes.unshift(new_scope)
|
||||
raise StackLevelError, "Nesting too deep" if @scopes.length > 100
|
||||
end
|
||||
|
||||
# Merge a hash of variables in the current local scope
|
||||
def merge(new_scopes)
|
||||
@variable_cache = {}
|
||||
@scopes[0].merge!(new_scopes)
|
||||
end
|
||||
|
||||
# Pop from the stack. use <tt>Context#stack</tt> instead
|
||||
def pop
|
||||
@variable_cache = {}
|
||||
raise ContextError if @scopes.size == 1
|
||||
@scopes.shift
|
||||
end
|
||||
@@ -105,86 +106,90 @@ module Liquid
|
||||
#
|
||||
# context['var] #=> nil
|
||||
def stack(new_scope={})
|
||||
@variable_cache = {}
|
||||
push(new_scope)
|
||||
yield
|
||||
ensure
|
||||
pop
|
||||
@variable_cache = {}
|
||||
end
|
||||
|
||||
def clear_instance_assigns
|
||||
@variable_cache = {}
|
||||
@scopes[0] = {}
|
||||
end
|
||||
|
||||
# Only allow String, Numeric, Hash, Array, Proc, Boolean or <tt>Liquid::Drop</tt>
|
||||
def []=(key, value)
|
||||
@scopes[0][key] = value
|
||||
|
||||
# Look up variable, either resolve directly after considering the name. We can directly handle
|
||||
# Strings, digits, floats and booleans (true,false).
|
||||
# If no match is made we lookup the variable in the current scope and
|
||||
# later move up to the parent blocks to see if we can resolve the variable somewhere up the tree.
|
||||
# Some special keywords return symbols. Those symbols are to be called on the rhs object in expressions
|
||||
#
|
||||
# Example:
|
||||
# products == empty #=> products.empty?
|
||||
def resolve(key)
|
||||
case key
|
||||
when nil, ""
|
||||
return nil
|
||||
when "blank"
|
||||
return :blank?
|
||||
when "empty"
|
||||
return :empty?
|
||||
end
|
||||
|
||||
result = Parser.parse(key)
|
||||
stack = []
|
||||
|
||||
result.each do |(sym, value)|
|
||||
|
||||
case sym
|
||||
when :id
|
||||
stack.push value
|
||||
when :lookup
|
||||
left = stack.pop
|
||||
value = find_variable(left)
|
||||
|
||||
stack.push(harden(value))
|
||||
when :range
|
||||
right = stack.pop.to_i
|
||||
left = stack.pop.to_i
|
||||
|
||||
stack.push (left..right)
|
||||
when :buildin
|
||||
left = stack.pop
|
||||
value = invoke_buildin(left, value)
|
||||
|
||||
stack.push(harden(value))
|
||||
when :call
|
||||
left = stack.pop
|
||||
right = stack.pop
|
||||
value = lookup_and_evaluate(right, left)
|
||||
|
||||
stack.push(harden(value))
|
||||
else
|
||||
raise "unknown #{sym}"
|
||||
end
|
||||
end
|
||||
|
||||
return stack.first
|
||||
end
|
||||
|
||||
def [](key)
|
||||
resolve(key)
|
||||
|
||||
# Only allow String, Numeric, Hash, Array, Proc, Boolean or <tt>Liquid::Drop</tt>
|
||||
def []=(key, value)
|
||||
@variable_cache[key] = value
|
||||
@scopes[0][key] = value
|
||||
end
|
||||
|
||||
def has_key?(key)
|
||||
resolve(key) != nil
|
||||
end
|
||||
|
||||
alias_method :[], :resolve
|
||||
|
||||
private
|
||||
|
||||
# Look up variable, either resolve directly after considering the name. We can directly handle
|
||||
# Strings, digits, floats and booleans (true,false).
|
||||
# If no match is made we lookup the variable in the current scope and
|
||||
# later move up to the parent blocks to see if we can resolve the variable somewhere up the tree.
|
||||
# Some special keywords return symbols. Those symbols are to be called on the rhs object in expressions
|
||||
#
|
||||
# Example:
|
||||
# products == empty #=> products.empty?
|
||||
def resolve(key)
|
||||
case key
|
||||
when nil, ""
|
||||
return nil
|
||||
when "blank"
|
||||
return :blank?
|
||||
when "empty"
|
||||
return :empty?
|
||||
end
|
||||
|
||||
result = Parser.parse(key)
|
||||
stack = []
|
||||
|
||||
result.each do |(sym, value)|
|
||||
|
||||
case sym
|
||||
when :id
|
||||
stack.push value
|
||||
when :lookup
|
||||
left = stack.pop
|
||||
value = find_variable(left)
|
||||
|
||||
stack.push(harden(value))
|
||||
when :range
|
||||
right = stack.pop.to_i
|
||||
left = stack.pop.to_i
|
||||
|
||||
stack.push (left..right)
|
||||
when :buildin
|
||||
left = stack.pop
|
||||
value = invoke_buildin(left, value)
|
||||
|
||||
stack.push(harden(value))
|
||||
when :call
|
||||
left = stack.pop
|
||||
right = stack.pop
|
||||
value = lookup_and_evaluate(right, left)
|
||||
|
||||
stack.push(harden(value))
|
||||
else
|
||||
raise "unknown #{sym}"
|
||||
end
|
||||
end
|
||||
|
||||
return stack.first
|
||||
end
|
||||
|
||||
def invoke_buildin(obj, key)
|
||||
# as weird as this is, liquid unit tests demand that we prioritize hash lookups
|
||||
# to buildins. So if we got a hash and it has a :first element we need to call that
|
||||
@@ -203,6 +208,10 @@ module Liquid
|
||||
|
||||
# Fetches an object starting at the local scope and then moving up the hierachy
|
||||
def find_variable(key)
|
||||
if val = @variable_cache[key]
|
||||
return val
|
||||
end
|
||||
|
||||
scope = @scopes.find { |s| s.has_key?(key) }
|
||||
|
||||
if scope.nil?
|
||||
@@ -216,6 +225,7 @@ module Liquid
|
||||
|
||||
scope ||= @environments.last || @scopes.last
|
||||
variable ||= lookup_and_evaluate(scope, key)
|
||||
@variable_cache[key] = variable
|
||||
|
||||
return variable
|
||||
end
|
||||
@@ -229,8 +239,7 @@ module Liquid
|
||||
|
||||
value = obj[key]
|
||||
|
||||
case value
|
||||
when Proc
|
||||
if value.is_a?(Proc)
|
||||
# call the proc
|
||||
value = (value.arity == 0) ? value.call : value.call(self)
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
require 'set'
|
||||
|
||||
module Liquid
|
||||
|
||||
# A drop in liquid is a class which allows you to export DOM like things to liquid.
|
||||
@@ -31,8 +33,8 @@ module Liquid
|
||||
|
||||
# called by liquid to invoke a drop
|
||||
def invoke_drop(method_or_key)
|
||||
if method_or_key && method_or_key != EMPTY_STRING && self.class.public_method_defined?(method_or_key.to_s.to_sym)
|
||||
send(method_or_key.to_s.to_sym)
|
||||
if method_or_key && method_or_key != EMPTY_STRING && self.class.invokable?(method_or_key)
|
||||
send(method_or_key)
|
||||
else
|
||||
before_method(method_or_key)
|
||||
end
|
||||
@@ -47,5 +49,13 @@ module Liquid
|
||||
end
|
||||
|
||||
alias :[] :invoke_drop
|
||||
|
||||
private
|
||||
|
||||
# Check for method existence without invoking respond_to?, which creates symbols
|
||||
def self.invokable?(method_name)
|
||||
@invokable_methods ||= Set.new((public_instance_methods - Liquid::Drop.public_instance_methods).map(&:to_s))
|
||||
@invokable_methods.include?(method_name.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module Liquid
|
||||
class TableRow < Block
|
||||
Syntax = /(\w+)\s+in\s+(#{QuotedFragment}+)/o
|
||||
Syntax = /(#{WordRegex}+)\s+in\s+(#{QuotedFragment}+)/o
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
if markup =~ Syntax
|
||||
|
||||
1313
lib/liquid/parser.rb
1313
lib/liquid/parser.rb
File diff suppressed because it is too large
Load Diff
@@ -1,126 +0,0 @@
|
||||
# Parser for context#[] method. Generated through ragel from parser.rl
|
||||
# Only modify parser.rl. Run rake ragel afterwards to generate this file.
|
||||
#
|
||||
#VERBOSE=true
|
||||
|
||||
%%{
|
||||
machine fsm;
|
||||
|
||||
action mark {
|
||||
mark = p
|
||||
}
|
||||
|
||||
action lookup {
|
||||
emit(:lookup, :instruction, nil, tokens)
|
||||
}
|
||||
|
||||
action call {
|
||||
emit(:call, :instruction, nil, tokens)
|
||||
}
|
||||
action range {
|
||||
emit(:range, :instruction, nil, tokens)
|
||||
}
|
||||
|
||||
constants = ( "true" | "false" | "nil" | "null" );
|
||||
|
||||
# strings
|
||||
string = "\"" any* "\"" | "'" any* "'";
|
||||
|
||||
# nothingness
|
||||
nil = "nil" | "null" ;
|
||||
|
||||
# numbers
|
||||
integer = ('+'|'-')? digit+;
|
||||
float = ('+'|'-')? digit+ '.' digit+;
|
||||
|
||||
# simple values
|
||||
primitive = (
|
||||
|
||||
integer >mark %{ emit(:id, :integer, Integer(data[mark..p-1]), tokens) } |
|
||||
|
||||
float >mark %{ emit(:id, :float, Float(data[mark..p-1]), tokens) } |
|
||||
|
||||
nil %{ emit(:id, :nil, nil, tokens) } |
|
||||
"true" %{ emit(:id, :bool, true, tokens) } |
|
||||
"false" %{ emit(:id, :bool, false, tokens)} |
|
||||
|
||||
string >mark %{ emit(:id, :string, data[mark+1..p-2], tokens) }
|
||||
|
||||
);
|
||||
|
||||
entity = (
|
||||
((alpha [A-Za-z0-9_\-]*) - (constants)) >mark %{
|
||||
emit(:id, :label, data[mark..p-1], tokens)
|
||||
emit(:lookup, :variable, nil, tokens)
|
||||
}
|
||||
);
|
||||
|
||||
# Because of recursion we cannot immediatly resolve the content of this in
|
||||
# the current grammar. We simply re-invoke the parser here to descend into
|
||||
# the substring
|
||||
recur = (
|
||||
(any+ - ']') >mark %{
|
||||
self.parse(data[mark..p-1], tokens)
|
||||
}
|
||||
);
|
||||
|
||||
expr = (
|
||||
entity |
|
||||
primitive |
|
||||
"(" (primitive | entity) ".." (primitive | entity) <: ")" %range |
|
||||
"[" recur "]" %lookup
|
||||
);
|
||||
|
||||
hash_accessors = (
|
||||
"[" recur "]" %call |
|
||||
|
||||
".first" %{
|
||||
emit(:buildin, :symbol, "first", tokens)
|
||||
} |
|
||||
|
||||
".last" %{
|
||||
emit(:buildin, :symbol, "last", tokens)
|
||||
} |
|
||||
|
||||
".size" %{
|
||||
emit(:buildin, :symbol, "size", tokens)
|
||||
} |
|
||||
|
||||
"." ((alpha [A-Za-z0-9_\-]*) - ("first"|"last"|"size")) >mark %{
|
||||
emit(:id, :label, data[mark..p-1], tokens)
|
||||
emit(:call, :variable, nil, tokens)
|
||||
}
|
||||
);
|
||||
|
||||
main := (
|
||||
|
||||
expr <: (hash_accessors)*
|
||||
|
||||
);
|
||||
|
||||
}%%
|
||||
# % fix syntax highlighting
|
||||
|
||||
|
||||
module Liquid
|
||||
module Parser
|
||||
%% write data;
|
||||
|
||||
def self.emit(sym, type, data, tokens)
|
||||
puts "emitting: #{type} #{sym} -> #{data.inspect}" if $VERBOSE
|
||||
tokens.push [sym, data]
|
||||
end
|
||||
|
||||
def self.parse(data, tokens = [])
|
||||
puts "--> self.parse with #{data.inspect}, #{tokens.inspect}" if $VERBOSE
|
||||
|
||||
eof = data.length
|
||||
|
||||
%% write init;
|
||||
%% write exec;
|
||||
|
||||
puts "<-- #{tokens.inspect}" if $VERBOSE
|
||||
return tokens
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,24 +2,15 @@ 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.
|
||||
# New filters are mixed into the strainer class which is then instantiated for each liquid template render run.
|
||||
#
|
||||
# One of the strainer's responsibilities is to keep malicious method calls out
|
||||
class Strainer < parent_object #:nodoc:
|
||||
INTERNAL_METHOD = /^__/
|
||||
@@required_methods = Set.new([:__id__, :__send__, :respond_to?, :kind_of?, :extend, :methods, :singleton_methods, :class, :object_id])
|
||||
|
||||
# Ruby 1.9.2 introduces Object#respond_to_missing?, which is invoked by Object#respond_to?
|
||||
@@required_methods << :respond_to_missing? if Object.respond_to? :respond_to_missing?
|
||||
|
||||
# The Strainer only allows method calls defined in filters given to it via Strainer.global_filter,
|
||||
# Context#add_filters or Template.register_filter
|
||||
class Strainer #:nodoc:
|
||||
@@filters = {}
|
||||
@@known_filters = Set.new
|
||||
@@known_methods = Set.new
|
||||
|
||||
def initialize(context)
|
||||
@context = context
|
||||
@@ -27,28 +18,36 @@ module Liquid
|
||||
|
||||
def self.global_filter(filter)
|
||||
raise ArgumentError, "Passed filter is not a module" unless filter.is_a?(Module)
|
||||
add_known_filter(filter)
|
||||
@@filters[filter.name] = filter
|
||||
end
|
||||
|
||||
def self.add_known_filter(filter)
|
||||
unless @@known_filters.include?(filter)
|
||||
@@method_blacklist ||= Set.new(Strainer.instance_methods.map(&:to_s))
|
||||
new_methods = filter.instance_methods.map(&:to_s)
|
||||
new_methods.reject!{ |m| @@method_blacklist.include?(m) }
|
||||
@@known_methods.merge(new_methods)
|
||||
@@known_filters.add(filter)
|
||||
end
|
||||
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
|
||||
def invoke(method, *args)
|
||||
if invokable?(method)
|
||||
send(method, *args)
|
||||
else
|
||||
args.first
|
||||
end
|
||||
end
|
||||
|
||||
# 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
|
||||
def invokable?(method)
|
||||
@@known_methods.include?(method.to_s) && respond_to?(method)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,7 +12,7 @@ module Liquid
|
||||
# in a sidebar or footer.
|
||||
#
|
||||
class Capture < Block
|
||||
Syntax = /(\w+)/
|
||||
Syntax = /(#{WordRegex}+)/o
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
if markup =~ Syntax
|
||||
|
||||
@@ -44,7 +44,7 @@ module Liquid
|
||||
# forloop.last:: Returns true if the item is the last item.
|
||||
#
|
||||
class For < Block
|
||||
Syntax = /(\w+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/o
|
||||
Syntax = /(#{WordRegex}+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/ou
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
if markup =~ Syntax
|
||||
|
||||
@@ -2,10 +2,10 @@ module Liquid
|
||||
class Include < Tag
|
||||
Syntax = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/o
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
def initialize(tag_name, markup, tokens)
|
||||
if markup =~ Syntax
|
||||
|
||||
@template_name = $1
|
||||
@template_name = $1
|
||||
@variable_name = $3
|
||||
@attributes = {}
|
||||
|
||||
@@ -24,8 +24,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def render(context)
|
||||
source = _read_template_from_file_system(context)
|
||||
partial = Liquid::Template.parse(source)
|
||||
partial = load_cached_partial(context)
|
||||
variable = context[@variable_name || @template_name[1..-2]]
|
||||
|
||||
context.stack do
|
||||
@@ -46,7 +45,21 @@ module Liquid
|
||||
end
|
||||
|
||||
private
|
||||
def _read_template_from_file_system(context)
|
||||
def load_cached_partial(context)
|
||||
cached_partials = context.registers[:cached_partials] || {}
|
||||
template_name = context[@template_name]
|
||||
|
||||
if cached = cached_partials[template_name]
|
||||
return cached
|
||||
end
|
||||
source = read_template_from_file_system(context)
|
||||
partial = Liquid::Template.parse(source)
|
||||
cached_partials[template_name] = partial
|
||||
context.registers[:cached_partials] = cached_partials
|
||||
partial
|
||||
end
|
||||
|
||||
def read_template_from_file_system(context)
|
||||
file_system = context.registers[:file_system] || Liquid::Template.file_system
|
||||
|
||||
# make read_template_file call backwards-compatible.
|
||||
|
||||
@@ -11,7 +11,7 @@ module Liquid
|
||||
# {{ user | link }}
|
||||
#
|
||||
class Variable
|
||||
FilterParser = /(?:#{FilterSeparator}|(?:\s*(?!(?:#{FilterSeparator}))(?:#{QuotedFragment}|\S+)\s*)+)/o
|
||||
FilterParser = /(?:#{FilterSeparator}|(?:\s*(?:#{QuotedFragment}|#{ArgumentSeparator})\s*)+)/o
|
||||
attr_accessor :filters, :name
|
||||
|
||||
def initialize(markup)
|
||||
@@ -23,10 +23,10 @@ module Liquid
|
||||
if match[2].match(/#{FilterSeparator}\s*(.*)/o)
|
||||
filters = Regexp.last_match(1).scan(FilterParser)
|
||||
filters.each do |f|
|
||||
if matches = f.match(/\s*(\w+)/)
|
||||
if matches = f.match(/\s*(#{WordRegex}+)(?:\s*#{FilterArgumentSeparator}(.*))?/)
|
||||
filtername = matches[1]
|
||||
filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/o).flatten
|
||||
@filters << [filtername.to_sym, filterargs]
|
||||
filterargs = matches[2].to_s.scan(/(?:\A|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o).flatten
|
||||
@filters << [filtername, filterargs]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -36,9 +36,16 @@ module Liquid
|
||||
def render(context)
|
||||
return '' if @name.nil?
|
||||
@filters.inject(context[@name]) do |output, filter|
|
||||
filterargs = filter[1].to_a.collect do |a|
|
||||
context[a]
|
||||
filterargs = []
|
||||
keyword_args = {}
|
||||
filter[1].to_a.each do |a|
|
||||
if matches = a.match(/\A#{TagAttributes}\z/o)
|
||||
keyword_args[matches[1]] = context[matches[2]]
|
||||
else
|
||||
filterargs << context[a]
|
||||
end
|
||||
end
|
||||
filterargs << keyword_args unless keyword_args.empty?
|
||||
begin
|
||||
output = context.invoke(filter[0], output, *filterargs)
|
||||
rescue FilterNotFound
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = "liquid"
|
||||
s.version = "2.4.1"
|
||||
s.version = "2.5.0"
|
||||
s.platform = Gem::Platform::RUBY
|
||||
s.summary = "A secure, non-evaling end user template engine with aesthetic markup."
|
||||
s.authors = ["Tobias Luetke"]
|
||||
|
||||
@@ -6,12 +6,14 @@ profiler = ThemeRunner.new
|
||||
|
||||
puts 'Running profiler...'
|
||||
|
||||
results = profiler.run_profile
|
||||
results = profiler.run
|
||||
|
||||
puts 'Success'
|
||||
puts
|
||||
|
||||
filename = (ENV['TMP'] || '/tmp') + "/callgrind.liquid.txt"
|
||||
File.open(filename, "w+") do |fp|
|
||||
RubyProf::CallTreePrinter.new(results).print(fp, :print_file => true)
|
||||
[RubyProf::FlatPrinter, RubyProf::GraphPrinter, RubyProf::GraphHtmlPrinter, RubyProf::CallTreePrinter].each do |klass|
|
||||
filename = (ENV['TMP'] || '/tmp') + (klass.name.include?('Html') ? "/liquid.#{klass.name.downcase}.html" : "/callgrind.liquid.#{klass.name.downcase}.txt")
|
||||
filename.gsub!(/:+/, '_')
|
||||
File.open(filename, "w+") { |fp| klass.new(results).print(fp, :print_file => true) }
|
||||
$stderr.puts "wrote #{klass.name} output to #{filename}"
|
||||
end
|
||||
$stderr.puts "wrote RubyProf::CallTreePrinter output to #{filename}"
|
||||
|
||||
@@ -14,6 +14,17 @@ require File.dirname(__FILE__) + '/shopify/liquid'
|
||||
require File.dirname(__FILE__) + '/shopify/database.rb'
|
||||
|
||||
class ThemeRunner
|
||||
class FileSystem
|
||||
|
||||
def initialize(path)
|
||||
@path = path
|
||||
end
|
||||
|
||||
# Called by Liquid to retrieve a template file
|
||||
def read_template_file(template_path, context)
|
||||
File.read(@path + '/' + template_path + '.liquid')
|
||||
end
|
||||
end
|
||||
|
||||
# Load all templates into memory, do this now so that
|
||||
# we don't profile IO.
|
||||
@@ -47,7 +58,7 @@ class ThemeRunner
|
||||
|
||||
# Compute page_tempalte outside of profiler run, uninteresting to profiler
|
||||
page_template = File.basename(template_name, File.extname(template_name))
|
||||
compile_and_render(liquid, layout, assigns, page_template)
|
||||
compile_and_render(liquid, layout, assigns, page_template, template_name)
|
||||
|
||||
end
|
||||
end
|
||||
@@ -74,7 +85,7 @@ class ThemeRunner
|
||||
html = nil
|
||||
|
||||
RubyProf.resume
|
||||
html = compile_and_render(liquid, layout, assigns, page_template)
|
||||
html = compile_and_render(liquid, layout, assigns, page_template, template_name)
|
||||
RubyProf.pause
|
||||
|
||||
|
||||
@@ -88,10 +99,11 @@ class ThemeRunner
|
||||
RubyProf.stop
|
||||
end
|
||||
|
||||
def compile_and_render(template, layout, assigns, page_template)
|
||||
def compile_and_render(template, layout, assigns, page_template, template_file)
|
||||
tmpl = Liquid::Template.new
|
||||
tmpl.assigns['page_title'] = 'Page title'
|
||||
tmpl.assigns['template'] = page_template
|
||||
tmpl.registers[:file_system] = ThemeRunner::FileSystem.new(File.dirname(template_file))
|
||||
|
||||
content_for_layout = tmpl.parse(template).render(assigns)
|
||||
|
||||
|
||||
@@ -12,7 +12,13 @@ class AssignTest < Test::Unit::TestCase
|
||||
'{% assign foo = values %}.{{ foo[1] }}.',
|
||||
'values' => %w{foo bar baz})
|
||||
end
|
||||
|
||||
|
||||
def test_assigned_utf8_variable
|
||||
assert_template_result('.bar.',
|
||||
"{% assign foo\u6000 = values %}.{{ foo\u6000[1] }}.",
|
||||
'values' => %w{foo bar baz})
|
||||
end
|
||||
|
||||
def test_assign_with_filter
|
||||
assert_template_result('.bar.',
|
||||
'{% assign foo = values | split: "," %}.{{ foo[1] }}.',
|
||||
|
||||
@@ -51,6 +51,14 @@ class BlockTest < Test::Unit::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_custom_utf8_tag
|
||||
Liquid::Template.register_tag("testtag\u6000", Block)
|
||||
|
||||
assert_nothing_thrown do
|
||||
template = Liquid::Template.parse( "{% testtag\u6000 something\u6000 %} {% endtesttag\u6000 %}")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def block_types(nodelist)
|
||||
nodelist.collect { |node| node.class }
|
||||
|
||||
@@ -7,6 +7,10 @@ class CaptureTest < Test::Unit::TestCase
|
||||
assert_template_result("test string", "{% capture 'var' %}test string{% endcapture %}{{var}}", {})
|
||||
end
|
||||
|
||||
def test_captures_block_content_in_utf8_variable
|
||||
assert_template_result("test string", "{% capture var\u6000 %}test string{% endcapture %}{{var\u6000}}", {})
|
||||
end
|
||||
|
||||
def test_capture_to_variable_from_outer_scope_if_existing
|
||||
template_source = <<-END_TEMPLATE
|
||||
{% assign var = '' %}
|
||||
|
||||
@@ -98,6 +98,11 @@ class ContextTest < Test::Unit::TestCase
|
||||
assert_equal nil, @context['nil']
|
||||
end
|
||||
|
||||
def test_utf8_variables
|
||||
@context["chinese\u6000variable"] = 'chinese'
|
||||
assert_equal 'chinese', @context["chinese\u6000variable"]
|
||||
end
|
||||
|
||||
def test_variables_not_existing
|
||||
assert_equal nil, @context['does_not_exist']
|
||||
end
|
||||
@@ -189,10 +194,10 @@ class ContextTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
context = Context.new
|
||||
methods_before = context.strainer.methods.map { |method| method.to_s }
|
||||
assert_equal "Wookie", context.invoke("hi", "Wookie")
|
||||
|
||||
context.add_filters(filter)
|
||||
methods_after = context.strainer.methods.map { |method| method.to_s }
|
||||
assert_equal (methods_before + ["hi"]).sort, methods_after.sort
|
||||
assert_equal "Wookie hi!", context.invoke("hi", "Wookie")
|
||||
end
|
||||
|
||||
def test_add_item_in_outer_scope
|
||||
@@ -254,16 +259,12 @@ class ContextTest < Test::Unit::TestCase
|
||||
@context['test'] = {'test' => [1,2,3,4,5]}
|
||||
|
||||
assert_equal 1, @context['test.test[0]']
|
||||
end
|
||||
|
||||
def test_recoursive_array_notation_for_hash
|
||||
@context['test'] = [{'test' => 'worked'}]
|
||||
|
||||
assert_equal 'worked', @context['test[0].test']
|
||||
end
|
||||
|
||||
|
||||
|
||||
def test_hash_to_array_transition
|
||||
@context['colors'] = {
|
||||
'Blue' => ['003366','336699', '6699CC', '99CCFF'],
|
||||
@@ -319,7 +320,7 @@ class ContextTest < Test::Unit::TestCase
|
||||
@context['nested'] = {'var' => 'tags'}
|
||||
@context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
|
||||
|
||||
#assert_equal 'deepsnow', @context['products[var].first']
|
||||
assert_equal 'deepsnow', @context['products[var].first']
|
||||
assert_equal 'freestyle', @context['products[nested.var].last']
|
||||
end
|
||||
|
||||
|
||||
@@ -115,6 +115,13 @@ class DropsTest < Test::Unit::TestCase
|
||||
assert_equal ' ', output
|
||||
end
|
||||
|
||||
def test_object_methods_not_allowed
|
||||
[:dup, :clone, :singleton_class, :eval, :class_eval, :inspect].each do |method|
|
||||
output = Liquid::Template.parse(" {{ product.#{method} }} ").render('product' => ProductDrop.new)
|
||||
assert_equal ' ', output
|
||||
end
|
||||
end
|
||||
|
||||
def test_scope
|
||||
assert_equal '1', Liquid::Template.parse( '{{ context.scopes }}' ).render('context' => ContextDrop.new)
|
||||
assert_equal '2', Liquid::Template.parse( '{%for i in dummy%}{{ context.scopes }}{%endfor%}' ).render('context' => ContextDrop.new, 'dummy' => [1])
|
||||
|
||||
@@ -16,6 +16,12 @@ module CanadianMoneyFilter
|
||||
end
|
||||
end
|
||||
|
||||
module SubstituteFilter
|
||||
def substitute(input, params={})
|
||||
input.gsub(/%\{(\w+)\}/) { |match| params[$1] }
|
||||
end
|
||||
end
|
||||
|
||||
class FiltersTest < Test::Unit::TestCase
|
||||
include Liquid
|
||||
|
||||
@@ -92,6 +98,13 @@ class FiltersTest < Test::Unit::TestCase
|
||||
|
||||
assert_equal 1000, Variable.new("var | xyzzy").render(@context)
|
||||
end
|
||||
|
||||
def test_filter_with_keyword_arguments
|
||||
@context['surname'] = 'john'
|
||||
@context.add_filters(SubstituteFilter)
|
||||
output = Variable.new(%! 'hello %{first_name}, %{last_name}' | substitute: first_name: surname, last_name: 'doe' !).render(@context)
|
||||
assert_equal 'hello john, doe', output
|
||||
end
|
||||
end
|
||||
|
||||
class FiltersInTemplate < Test::Unit::TestCase
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
|
||||
class ParserTest < Test::Unit::TestCase
|
||||
include Liquid
|
||||
|
||||
|
||||
def test_strings
|
||||
assert_equal [[:id, "string"]], Parser.parse('"string"')
|
||||
assert_equal [[:id, "string"]], Parser.parse('\'string\'')
|
||||
end
|
||||
|
||||
def test_integer
|
||||
assert_equal [[:id, 1]], Parser.parse('1')
|
||||
assert_equal [[:id, 100001]], Parser.parse('100001')
|
||||
end
|
||||
|
||||
def test_float
|
||||
assert_equal [[:id, 1.1]], Parser.parse('1.1')
|
||||
assert_equal [[:id, 1.55435]], Parser.parse('1.55435')
|
||||
end
|
||||
|
||||
def test_null
|
||||
assert_equal [[:id, nil]], Parser.parse('null')
|
||||
assert_equal [[:id, nil]], Parser.parse('nil')
|
||||
end
|
||||
|
||||
def test_bool
|
||||
assert_equal [[:id, true]], Parser.parse('true')
|
||||
assert_equal [[:id, false]], Parser.parse('false')
|
||||
end
|
||||
|
||||
def test_ranges
|
||||
assert_equal [[:id, 1], [:id, 5], [:range, nil]], Parser.parse('(1..5)')
|
||||
assert_equal [[:id, 100], [:id, 500], [:range, nil]], Parser.parse('(100..500)')
|
||||
end
|
||||
|
||||
def test_ranges_with_lookups
|
||||
assert_equal [[:id, 1], [:id, "test"], [:lookup, nil], [:range, nil]], Parser.parse('(1..test)')
|
||||
end
|
||||
|
||||
def test_lookups
|
||||
assert_equal [[:id, "variable"], [:lookup, nil]], Parser.parse('variable')
|
||||
assert_equal [[:id, "underscored_variable"], [:lookup, nil]], Parser.parse('underscored_variable')
|
||||
end
|
||||
|
||||
def test_global_hash
|
||||
assert_equal [[:id, true], [:lookup, nil]], Parser.parse('[true]')
|
||||
|
||||
assert_equal [[:id, "string"], [:lookup, nil]], Parser.parse('["string"]')
|
||||
assert_equal [[:id, 5.55], [:lookup, nil]], Parser.parse('[5.55]')
|
||||
assert_equal [[:id, 0], [:lookup, nil]], Parser.parse('[0]')
|
||||
assert_equal [[:id, "variable"], [:lookup, nil], [:lookup, nil]], Parser.parse('[variable]')
|
||||
end
|
||||
|
||||
def test_descent
|
||||
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "variable2"], [:call, nil]], Parser.parse('variable1.variable2')
|
||||
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "variable2"], [:call, nil], [:id, "variable3"], [:call, nil]], Parser.parse('variable1.variable2.variable3')
|
||||
end
|
||||
|
||||
def test_descent_hash
|
||||
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "variable2"], [:call, nil]], Parser.parse('variable1["variable2"]')
|
||||
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "variable2"], [:lookup, nil], [:call, nil]], Parser.parse('variable1[variable2]')
|
||||
end
|
||||
|
||||
def test_buildin
|
||||
assert_equal [[:id, "first"], [:lookup, nil]], Parser.parse('first')
|
||||
|
||||
assert_equal [[:id, "var"], [:lookup, nil], [:buildin, "first"]], Parser.parse('var.first')
|
||||
assert_equal [[:id, "var"], [:lookup, nil], [:buildin, "last"]], Parser.parse('var.last')
|
||||
assert_equal [[:id, "var"], [:lookup, nil], [:buildin, "size"]], Parser.parse('var.size')
|
||||
|
||||
end
|
||||
|
||||
def test_descent_hash_descent
|
||||
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "test1"], [:lookup, nil], [:id, "test2"], [:call, nil], [:call, nil]],
|
||||
Parser.parse('variable1[test1.test2]'), "resolove: variable1[test1.test2]"
|
||||
|
||||
# assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "test1"], [:lookup, nil], [:id, "test2"], [:call, nil], [:call, nil]],
|
||||
# Parser.parse('variable1[test1["test2"]]'), 'resolove: variable1[test1["test2"]]'
|
||||
|
||||
# assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "test1"], [:lookup, nil], [:id, "test2"], [:lookup, nil], [:call, nil], [:call, nil]],
|
||||
# Parser.parse('variable1[test1[test2]]'), "resolove: variable1[test1[test2]]"
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
@@ -39,7 +39,7 @@ class ParsingQuirksTest < Test::Unit::TestCase
|
||||
|
||||
def test_meaningless_parens
|
||||
assigns = {'b' => 'bar', 'c' => 'baz'}
|
||||
markup = "a == 'foo' or b == 'bar' and c == 'baz' or false"
|
||||
markup = "a == 'foo' or (b == 'bar' and c == 'baz') or false"
|
||||
assert_template_result(' YES ',"{% if #{markup} %} YES {% endif %}", assigns)
|
||||
end
|
||||
|
||||
|
||||
@@ -38,4 +38,27 @@ class SecurityTest < Test::Unit::TestCase
|
||||
|
||||
assert_equal expected, Template.parse(text).render(@assigns, :filters => SecurityFilter)
|
||||
end
|
||||
|
||||
def test_does_not_add_filters_to_symbol_table
|
||||
current_symbols = Symbol.all_symbols
|
||||
|
||||
test = %( {{ "some_string" | a_bad_filter }} )
|
||||
|
||||
template = Template.parse(test)
|
||||
assert_equal [], (Symbol.all_symbols - current_symbols)
|
||||
|
||||
template.render
|
||||
assert_equal [], (Symbol.all_symbols - current_symbols)
|
||||
end
|
||||
|
||||
def test_does_not_add_drop_methods_to_symbol_table
|
||||
current_symbols = Symbol.all_symbols
|
||||
|
||||
drop = Drop.new
|
||||
drop.invoke_drop("custom_method_1")
|
||||
drop.invoke_drop("custom_method_2")
|
||||
drop.invoke_drop("custom_method_3")
|
||||
|
||||
assert_equal [], (Symbol.all_symbols - current_symbols)
|
||||
end
|
||||
end # SecurityTest
|
||||
|
||||
@@ -3,23 +3,50 @@ require 'test_helper'
|
||||
class StrainerTest < Test::Unit::TestCase
|
||||
include Liquid
|
||||
|
||||
module AccessScopeFilters
|
||||
def public_filter
|
||||
"public"
|
||||
end
|
||||
|
||||
def private_filter
|
||||
"private"
|
||||
end
|
||||
private :private_filter
|
||||
end
|
||||
|
||||
Strainer.global_filter(AccessScopeFilters)
|
||||
|
||||
def test_strainer
|
||||
strainer = Strainer.create(nil)
|
||||
assert_equal false, strainer.respond_to?('__test__')
|
||||
assert_equal false, strainer.respond_to?('test')
|
||||
assert_equal false, strainer.respond_to?('instance_eval')
|
||||
assert_equal false, strainer.respond_to?('__send__')
|
||||
assert_equal true, strainer.respond_to?('size') # from the standard lib
|
||||
assert_equal 5, strainer.invoke('size', 'input')
|
||||
assert_equal "public", strainer.invoke("public_filter")
|
||||
end
|
||||
|
||||
def test_should_respond_to_two_parameters
|
||||
def test_strainer_only_invokes_public_filter_methods
|
||||
strainer = Strainer.create(nil)
|
||||
assert_equal true, strainer.respond_to?('size', false)
|
||||
assert_equal false, strainer.invokable?('__test__')
|
||||
assert_equal false, strainer.invokable?('test')
|
||||
assert_equal false, strainer.invokable?('instance_eval')
|
||||
assert_equal false, strainer.invokable?('__send__')
|
||||
assert_equal true, strainer.invokable?('size') # from the standard lib
|
||||
end
|
||||
|
||||
# Asserts that Object#respond_to_missing? is not being undefined in Ruby versions where it has been implemented
|
||||
# Currently this method is only present in Ruby v1.9.2, or higher
|
||||
def test_object_respond_to_missing
|
||||
assert_equal Object.respond_to?(:respond_to_missing?), Strainer.create(nil).respond_to?(:respond_to_missing?)
|
||||
def test_strainer_returns_nil_if_no_filter_method_found
|
||||
strainer = Strainer.create(nil)
|
||||
assert_nil strainer.invoke("private_filter")
|
||||
assert_nil strainer.invoke("undef_the_filter")
|
||||
end
|
||||
|
||||
def test_strainer_returns_first_argument_if_no_method_and_arguments_given
|
||||
strainer = Strainer.create(nil)
|
||||
assert_equal "password", strainer.invoke("undef_the_method", "password")
|
||||
end
|
||||
|
||||
def test_strainer_only_allows_methods_defined_in_filters
|
||||
strainer = Strainer.create(nil)
|
||||
assert_equal "1 + 1", strainer.invoke("instance_eval", "1 + 1")
|
||||
assert_equal "puts", strainer.invoke("__send__", "puts", "Hi Mom")
|
||||
assert_equal "has_method?", strainer.invoke("invoke", "has_method?", "invoke")
|
||||
end
|
||||
|
||||
end # StrainerTest
|
||||
|
||||
@@ -25,6 +25,11 @@ HERE
|
||||
assert_template_result(expected,template,'array' => [1,2,3])
|
||||
end
|
||||
|
||||
def test_utf8_for
|
||||
assigns = {"array\u6000chinese" => [1,2,3]}
|
||||
assert_template_result('123', "{% for item\u6000chinese in array\u6000chinese %}{{ item\u6000chinese }}{% endfor %}", assigns)
|
||||
end
|
||||
|
||||
def test_for_reversed
|
||||
assigns = {'array' => [ 1, 2, 3] }
|
||||
assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',assigns)
|
||||
|
||||
@@ -26,6 +26,12 @@ class HtmlTagTest < Test::Unit::TestCase
|
||||
'numbers' => [])
|
||||
end
|
||||
|
||||
def test_utf8_html_table
|
||||
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td></tr>\n",
|
||||
"{% tablerow n\u6000 in numbers\u6000 %} {{n\u6000}} {% endtablerow %}",
|
||||
"numbers\u6000" => [1])
|
||||
end
|
||||
|
||||
def test_html_table_with_different_cols
|
||||
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td><td class=\"col4\"> 4 </td><td class=\"col5\"> 5 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 6 </td></tr>\n",
|
||||
'{% tablerow n in numbers cols:5%} {{n}} {% endtablerow %}',
|
||||
|
||||
@@ -39,6 +39,15 @@ class OtherFileSystem
|
||||
end
|
||||
end
|
||||
|
||||
class CountingFileSystem
|
||||
attr_reader :count
|
||||
def read_template_file(template_path, context)
|
||||
@count ||= 0
|
||||
@count += 1
|
||||
'from CountingFileSystem'
|
||||
end
|
||||
end
|
||||
|
||||
class IncludeTagTest < Test::Unit::TestCase
|
||||
include Liquid
|
||||
|
||||
@@ -136,4 +145,22 @@ class IncludeTagTest < Test::Unit::TestCase
|
||||
|
||||
assert_equal "Product: Draft 151cm ", Template.parse("{% include template for product %}").render("template" => 'product', 'product' => { 'title' => 'Draft 151cm'})
|
||||
end
|
||||
end # IncludeTagTest
|
||||
|
||||
def test_include_tag_caches_second_read_of_same_partial
|
||||
file_system = CountingFileSystem.new
|
||||
assert_equal 'from CountingFileSystemfrom CountingFileSystem',
|
||||
Template.parse("{% include 'pick_a_source' %}{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system})
|
||||
assert_equal 1, file_system.count
|
||||
end
|
||||
|
||||
def test_include_tag_doesnt_cache_partials_across_renders
|
||||
file_system = CountingFileSystem.new
|
||||
assert_equal 'from CountingFileSystem',
|
||||
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system})
|
||||
assert_equal 1, file_system.count
|
||||
|
||||
assert_equal 'from CountingFileSystem',
|
||||
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system})
|
||||
assert_equal 2, file_system.count
|
||||
end
|
||||
end # IncludeTagTest
|
||||
|
||||
@@ -11,67 +11,77 @@ class VariableTest < Test::Unit::TestCase
|
||||
def test_filters
|
||||
var = Variable.new('hello | textileze')
|
||||
assert_equal 'hello', var.name
|
||||
assert_equal [[:textileze,[]]], var.filters
|
||||
assert_equal [["textileze",[]]], var.filters
|
||||
|
||||
var = Variable.new('hello | textileze | paragraph')
|
||||
assert_equal 'hello', var.name
|
||||
assert_equal [[:textileze,[]], [:paragraph,[]]], var.filters
|
||||
assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
|
||||
|
||||
var = Variable.new(%! hello | strftime: '%Y'!)
|
||||
assert_equal 'hello', var.name
|
||||
assert_equal [[:strftime,["'%Y'"]]], var.filters
|
||||
assert_equal [["strftime",["'%Y'"]]], var.filters
|
||||
|
||||
var = Variable.new(%! 'typo' | link_to: 'Typo', true !)
|
||||
assert_equal %!'typo'!, var.name
|
||||
assert_equal [[:link_to,["'Typo'", "true"]]], var.filters
|
||||
assert_equal [["link_to",["'Typo'", "true"]]], var.filters
|
||||
|
||||
var = Variable.new(%! 'typo' | link_to: 'Typo', false !)
|
||||
assert_equal %!'typo'!, var.name
|
||||
assert_equal [[:link_to,["'Typo'", "false"]]], var.filters
|
||||
assert_equal [["link_to",["'Typo'", "false"]]], var.filters
|
||||
|
||||
var = Variable.new(%! 'foo' | repeat: 3 !)
|
||||
assert_equal %!'foo'!, var.name
|
||||
assert_equal [[:repeat,["3"]]], var.filters
|
||||
assert_equal [["repeat",["3"]]], var.filters
|
||||
|
||||
var = Variable.new(%! 'foo' | repeat: 3, 3 !)
|
||||
assert_equal %!'foo'!, var.name
|
||||
assert_equal [[:repeat,["3","3"]]], var.filters
|
||||
assert_equal [["repeat",["3","3"]]], var.filters
|
||||
|
||||
var = Variable.new(%! 'foo' | repeat: 3, 3, 3 !)
|
||||
assert_equal %!'foo'!, var.name
|
||||
assert_equal [[:repeat,["3","3","3"]]], var.filters
|
||||
assert_equal [["repeat",["3","3","3"]]], var.filters
|
||||
|
||||
var = Variable.new(%! hello | strftime: '%Y, okay?'!)
|
||||
assert_equal 'hello', var.name
|
||||
assert_equal [[:strftime,["'%Y, okay?'"]]], var.filters
|
||||
assert_equal [["strftime",["'%Y, okay?'"]]], var.filters
|
||||
|
||||
var = Variable.new(%! hello | things: "%Y, okay?", 'the other one'!)
|
||||
assert_equal 'hello', var.name
|
||||
assert_equal [[:things,["\"%Y, okay?\"","'the other one'"]]], var.filters
|
||||
assert_equal [["things",["\"%Y, okay?\"","'the other one'"]]], var.filters
|
||||
end
|
||||
|
||||
def test_utf8_filters
|
||||
var = Variable.new("foo | chinese\u6000filter: value\u6000")
|
||||
assert_equal 'foo', var.name
|
||||
assert_equal [["chinese\u6000filter",["value\u6000"]]], var.filters
|
||||
end
|
||||
|
||||
def test_filter_with_date_parameter
|
||||
|
||||
var = Variable.new(%! '2006-06-06' | date: "%m/%d/%Y"!)
|
||||
assert_equal "'2006-06-06'", var.name
|
||||
assert_equal [[:date,["\"%m/%d/%Y\""]]], var.filters
|
||||
assert_equal [["date",["\"%m/%d/%Y\""]]], var.filters
|
||||
|
||||
end
|
||||
|
||||
def test_filters_without_whitespace
|
||||
var = Variable.new('hello | textileze | paragraph')
|
||||
assert_equal 'hello', var.name
|
||||
assert_equal [[:textileze,[]], [:paragraph,[]]], var.filters
|
||||
assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
|
||||
|
||||
var = Variable.new('hello|textileze|paragraph')
|
||||
assert_equal 'hello', var.name
|
||||
assert_equal [[:textileze,[]], [:paragraph,[]]], var.filters
|
||||
assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
|
||||
|
||||
var = Variable.new("hello|replace:'foo','bar'|textileze")
|
||||
assert_equal 'hello', var.name
|
||||
assert_equal [["replace", ["'foo'", "'bar'"]], ["textileze", []]], var.filters
|
||||
end
|
||||
|
||||
def test_symbol
|
||||
var = Variable.new("http://disney.com/logo.gif | image: 'med' ")
|
||||
assert_equal 'http://disney.com/logo.gif', var.name
|
||||
assert_equal [[:image,["'med'"]]], var.filters
|
||||
assert_equal [["image",["'med'"]]], var.filters
|
||||
end
|
||||
|
||||
def test_string_single_quoted
|
||||
@@ -103,6 +113,12 @@ class VariableTest < Test::Unit::TestCase
|
||||
var = Variable.new(%| test.test |)
|
||||
assert_equal 'test.test', var.name
|
||||
end
|
||||
|
||||
def test_filter_with_keyword_arguments
|
||||
var = Variable.new(%! hello | things: greeting: "world", farewell: 'goodbye'!)
|
||||
assert_equal 'hello', var.name
|
||||
assert_equal [['things',["greeting: \"world\"","farewell: 'goodbye'"]]], var.filters
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user