liquid-autoescape 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/LICENSE +21 -0
- data/README.md +165 -0
- data/lib/liquid/autoescape.rb +33 -0
- data/lib/liquid/autoescape/configuration.rb +41 -0
- data/lib/liquid/autoescape/core_exemptions.rb +40 -0
- data/lib/liquid/autoescape/errors.rb +11 -0
- data/lib/liquid/autoescape/exemption.rb +47 -0
- data/lib/liquid/autoescape/exemption_list.rb +106 -0
- data/lib/liquid/autoescape/filters.rb +26 -0
- data/lib/liquid/autoescape/liquid_ext/variable.rb +38 -0
- data/lib/liquid/autoescape/tags/autoescape.rb +45 -0
- data/lib/liquid/autoescape/template_variable.rb +74 -0
- data/lib/liquid/autoescape/version.rb +5 -0
- data/spec/functional/autoescape_tag_spec.rb +200 -0
- data/spec/unit/autoescape_spec.rb +49 -0
- data/spec/unit/configuration_spec.rb +72 -0
- data/spec/unit/core_exemptions_spec.rb +72 -0
- data/spec/unit/exemption_list_spec.rb +165 -0
- data/spec/unit/exemption_spec.rb +29 -0
- data/spec/unit/template_variable_spec.rb +80 -0
- metadata +148 -0
@@ -0,0 +1,72 @@
|
|
1
|
+
require "liquid/autoescape"
|
2
|
+
require "liquid/autoescape/core_exemptions"
|
3
|
+
require "liquid/autoescape/template_variable"
|
4
|
+
|
5
|
+
module Liquid
|
6
|
+
module Autoescape
|
7
|
+
describe CoreExemptions do
|
8
|
+
|
9
|
+
let(:exemptions) { Module.new { extend CoreExemptions } }
|
10
|
+
|
11
|
+
let(:variable) { TemplateVariable.new(:name => name, :filters => filters) }
|
12
|
+
let(:name) { "variable" }
|
13
|
+
let(:filters) { [] }
|
14
|
+
|
15
|
+
describe ".uses_escaping_filter?" do
|
16
|
+
|
17
|
+
subject { exemptions.uses_escaping_filter?(variable) }
|
18
|
+
|
19
|
+
context "when no filters are used" do
|
20
|
+
let(:filters) { [] }
|
21
|
+
it { should be(false) }
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when the escape filter is used" do
|
25
|
+
let(:filters) { [:escape] }
|
26
|
+
it { should be(true) }
|
27
|
+
end
|
28
|
+
|
29
|
+
context "when the skip_escape filter is used" do
|
30
|
+
let(:filters) { [:skip_escape] }
|
31
|
+
it { should be(true) }
|
32
|
+
end
|
33
|
+
|
34
|
+
context "when a non-escaping filter is used" do
|
35
|
+
let(:filters) { [:downcase] }
|
36
|
+
it { should be(false) }
|
37
|
+
end
|
38
|
+
|
39
|
+
context "when an escaping filter is used with a non-escaping filter" do
|
40
|
+
let(:filters) { [:downcase, :escape] }
|
41
|
+
it { should be(true) }
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe ".uses_trusted_filter?" do
|
47
|
+
|
48
|
+
subject { exemptions.uses_trusted_filter?(variable) }
|
49
|
+
|
50
|
+
before(:each) do
|
51
|
+
Autoescape.configure { |config| config.trusted_filters << :downcase }
|
52
|
+
end
|
53
|
+
|
54
|
+
after(:each) do
|
55
|
+
Autoescape.reconfigure
|
56
|
+
end
|
57
|
+
|
58
|
+
context "when a trusted filter is used" do
|
59
|
+
let(:filters) { [:downcase] }
|
60
|
+
it { should be(true) }
|
61
|
+
end
|
62
|
+
|
63
|
+
context "when a non-trusted filter is used" do
|
64
|
+
let(:filters) { [:capitalize] }
|
65
|
+
it { should be(false) }
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
require "liquid/autoescape/exemption_list"
|
2
|
+
require "liquid/autoescape/template_variable"
|
3
|
+
|
4
|
+
module Liquid
|
5
|
+
module Autoescape
|
6
|
+
describe ExemptionList do
|
7
|
+
|
8
|
+
let(:exemptions) { ExemptionList.new }
|
9
|
+
|
10
|
+
describe ".from_module" do
|
11
|
+
|
12
|
+
it "creates a new exemption list with the module's methods as exemptions" do
|
13
|
+
custom = Module.new do
|
14
|
+
def exemption_one(variable)
|
15
|
+
variable.name == "one"
|
16
|
+
end
|
17
|
+
|
18
|
+
def exemption_two(variable)
|
19
|
+
variable.name == "two"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
from_module = ExemptionList.from_module(custom)
|
24
|
+
expect(from_module).to be_an_instance_of(ExemptionList)
|
25
|
+
expect(from_module.size).to eq(2)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#add" do
|
31
|
+
|
32
|
+
it "adds a filter function as an exemption" do
|
33
|
+
expect(exemptions.size).to eq(0)
|
34
|
+
exemptions.add { |variable| variable.name == "variable" }
|
35
|
+
expect(exemptions.size).to eq(1)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "is chainable" do
|
39
|
+
first = lambda { |variable| variable.name == "one" }
|
40
|
+
second = lambda { |variable| variable.name == "two" }
|
41
|
+
|
42
|
+
expect(exemptions.size).to eq(0)
|
43
|
+
exemptions.add(&first).add(&second)
|
44
|
+
expect(exemptions.size).to eq(2)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#import" do
|
50
|
+
|
51
|
+
it "adds all instance methods from a module as exemptions" do
|
52
|
+
custom = Module.new do
|
53
|
+
def exemption(variable)
|
54
|
+
variable.name == "variable"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
expect(exemptions.size).to eq(0)
|
59
|
+
exemptions.import(custom)
|
60
|
+
expect(exemptions.size).to eq(1)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "adds methods with identical names from different modules" do
|
64
|
+
module_one = Module.new do
|
65
|
+
def exemption(variable)
|
66
|
+
variable.name == "one"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
module_two = Module.new do
|
71
|
+
def exemption(variable)
|
72
|
+
variable.name == "two"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
expect(exemptions.size).to eq(0)
|
77
|
+
exemptions.import(module_one)
|
78
|
+
exemptions.import(module_two)
|
79
|
+
expect(exemptions.size).to eq(2)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "is chainable" do
|
83
|
+
custom = Module.new do
|
84
|
+
def exemption(variable)
|
85
|
+
variable.name == "one"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
expect(exemptions.size).to eq(0)
|
90
|
+
exemptions.import(custom).import(custom)
|
91
|
+
expect(exemptions.size).to eq(2)
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "#apply?" do
|
97
|
+
|
98
|
+
let(:var_one) { TemplateVariable.new(:name => "one") }
|
99
|
+
let(:var_two) { TemplateVariable.new(:name => "two") }
|
100
|
+
|
101
|
+
it "returns true when a filter function applies to a variable" do
|
102
|
+
expect(exemptions.apply?(var_one)).to be(false)
|
103
|
+
expect(exemptions.apply?(var_two)).to be(false)
|
104
|
+
|
105
|
+
exemptions.add { |variable| variable.name == "one" }
|
106
|
+
|
107
|
+
expect(exemptions.apply?(var_one)).to be(true)
|
108
|
+
expect(exemptions.apply?(var_two)).to be(false)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "returns true when a module function applies to a variable" do
|
112
|
+
custom = Module.new do
|
113
|
+
def exemption(variable)
|
114
|
+
variable.name == "one"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
expect(exemptions.apply?(var_one)).to be(false)
|
119
|
+
expect(exemptions.apply?(var_two)).to be(false)
|
120
|
+
|
121
|
+
exemptions.import(custom)
|
122
|
+
|
123
|
+
expect(exemptions.apply?(var_one)).to be(true)
|
124
|
+
expect(exemptions.apply?(var_two)).to be(false)
|
125
|
+
end
|
126
|
+
|
127
|
+
it "has an #applies? alias" do
|
128
|
+
exemption_list = ExemptionList.new
|
129
|
+
exemption_list.add { |variable| variable.name == "one" }
|
130
|
+
|
131
|
+
expect(exemption_list.applies?(var_one)).to be(true)
|
132
|
+
expect(exemption_list.applies?(var_two)).to be(false)
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "#populated?" do
|
138
|
+
|
139
|
+
it "is false when there are no exemptions" do
|
140
|
+
expect(exemptions.populated?).to be(false)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "is true when there are exemptions" do
|
144
|
+
exemptions.add { true }
|
145
|
+
expect(exemptions.populated?).to be(true)
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "#size" do
|
151
|
+
|
152
|
+
it "is zero when there are no exemptions" do
|
153
|
+
expect(exemptions.size).to eq(0)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "is true when there are exemptions" do
|
157
|
+
exemptions.add { true }
|
158
|
+
expect(exemptions.size).to eq(1)
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "liquid/autoescape/errors"
|
2
|
+
require "liquid/autoescape/exemption"
|
3
|
+
require "liquid/autoescape/template_variable"
|
4
|
+
|
5
|
+
module Liquid
|
6
|
+
module Autoescape
|
7
|
+
describe Exemption do
|
8
|
+
|
9
|
+
it "requires a callable filter block" do
|
10
|
+
expect { Exemption.new }.to raise_error(ExemptionError)
|
11
|
+
expect { Exemption.new { true } }.to_not raise_error
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#applies?" do
|
15
|
+
|
16
|
+
it "evaluates the filter block in the context of variable data" do
|
17
|
+
exemption = Exemption.new { |variable| variable.name == "one" }
|
18
|
+
var_one = TemplateVariable.new(:name => "one")
|
19
|
+
var_two = TemplateVariable.new(:name => "two")
|
20
|
+
|
21
|
+
expect(exemption.applies?(var_one)).to be(true)
|
22
|
+
expect(exemption.applies?(var_two)).to be(false)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require "liquid"
|
2
|
+
require "liquid/autoescape/template_variable"
|
3
|
+
|
4
|
+
module Liquid
|
5
|
+
module Autoescape
|
6
|
+
describe TemplateVariable do
|
7
|
+
|
8
|
+
it "requires a variable name" do
|
9
|
+
expect { TemplateVariable.new }.to raise_error(KeyError)
|
10
|
+
expect { TemplateVariable.new(:name => "variable") }.to_not raise_error
|
11
|
+
end
|
12
|
+
|
13
|
+
it "exposes its variable name" do
|
14
|
+
data = TemplateVariable.new(:name => "variable")
|
15
|
+
expect(data.name).to eq("variable")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "can accept variable names describing a lookup" do
|
19
|
+
data = TemplateVariable.new(:name => "hash.key")
|
20
|
+
expect(data.name).to eq("hash.key")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "can accept a list of filters applied to the variable" do
|
24
|
+
data = TemplateVariable.new(:name => "variable", :filters => [:downcase])
|
25
|
+
expect(data.filters).to match_array([:downcase])
|
26
|
+
end
|
27
|
+
|
28
|
+
describe ".from_liquid_variable" do
|
29
|
+
|
30
|
+
let(:liquid_variable) { Liquid::Variable.new(variable_name) }
|
31
|
+
let(:variable_name) { nil }
|
32
|
+
|
33
|
+
let(:wrapper) { TemplateVariable.from_liquid_variable(liquid_variable) }
|
34
|
+
|
35
|
+
context "with an unfiltered Liquid variable" do
|
36
|
+
let(:variable_name) { "from_liquid" }
|
37
|
+
|
38
|
+
it "resolves the variable name" do
|
39
|
+
expect(wrapper.name).to eq("from_liquid")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "has an empty list of filters" do
|
43
|
+
expect(wrapper.filters).to be_empty
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "with a filtered Liquid variable" do
|
48
|
+
let(:variable_name) { "from_liquid | downcase | capitalize" }
|
49
|
+
|
50
|
+
it "resolves the variable name" do
|
51
|
+
expect(wrapper.name).to eq("from_liquid")
|
52
|
+
end
|
53
|
+
|
54
|
+
it "exposes a list of filters" do
|
55
|
+
expect(wrapper.filters).to eq([:downcase, :capitalize])
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
context "with a lookup-style Liquid variable" do
|
61
|
+
let(:variable_name) { "hash.key" }
|
62
|
+
|
63
|
+
it "exposes the full variable name" do
|
64
|
+
expect(wrapper.name).to eq("hash.key")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "with a deep lookup-style Liquid variable" do
|
69
|
+
let(:variable_name) { "trunk.branch.leaf" }
|
70
|
+
|
71
|
+
it "exposes the full variable name" do
|
72
|
+
expect(wrapper.name).to eq("trunk.branch.leaf")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: liquid-autoescape
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Within3
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: liquid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
- - <
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '4.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.0'
|
30
|
+
- - <
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '4.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: appraisal
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ~>
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2.0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rspec
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ~>
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '3.0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rubocop
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ~>
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0.30'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ~>
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0.30'
|
89
|
+
description: Apply HTML escaping to all variables in a Liquid block
|
90
|
+
email:
|
91
|
+
- it-operations@within3.com
|
92
|
+
executables: []
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- LICENSE
|
97
|
+
- README.md
|
98
|
+
- lib/liquid/autoescape.rb
|
99
|
+
- lib/liquid/autoescape/configuration.rb
|
100
|
+
- lib/liquid/autoescape/core_exemptions.rb
|
101
|
+
- lib/liquid/autoescape/errors.rb
|
102
|
+
- lib/liquid/autoescape/exemption.rb
|
103
|
+
- lib/liquid/autoescape/exemption_list.rb
|
104
|
+
- lib/liquid/autoescape/filters.rb
|
105
|
+
- lib/liquid/autoescape/liquid_ext/variable.rb
|
106
|
+
- lib/liquid/autoescape/tags/autoescape.rb
|
107
|
+
- lib/liquid/autoescape/template_variable.rb
|
108
|
+
- lib/liquid/autoescape/version.rb
|
109
|
+
- spec/functional/autoescape_tag_spec.rb
|
110
|
+
- spec/unit/autoescape_spec.rb
|
111
|
+
- spec/unit/configuration_spec.rb
|
112
|
+
- spec/unit/core_exemptions_spec.rb
|
113
|
+
- spec/unit/exemption_list_spec.rb
|
114
|
+
- spec/unit/exemption_spec.rb
|
115
|
+
- spec/unit/template_variable_spec.rb
|
116
|
+
homepage: https://github.com/Within3/liquid-autoescape
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ! '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.4.5
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Autoescape support for Liquid
|
140
|
+
test_files:
|
141
|
+
- spec/functional/autoescape_tag_spec.rb
|
142
|
+
- spec/unit/autoescape_spec.rb
|
143
|
+
- spec/unit/configuration_spec.rb
|
144
|
+
- spec/unit/core_exemptions_spec.rb
|
145
|
+
- spec/unit/exemption_list_spec.rb
|
146
|
+
- spec/unit/exemption_spec.rb
|
147
|
+
- spec/unit/template_variable_spec.rb
|
148
|
+
has_rdoc:
|