barber 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +1 -1
- data/lib/barber/precompiler.rb +22 -2
- data/lib/barber/version.rb +1 -1
- data/test/precompiler_test.rb +80 -5
- metadata +23 -22
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
data/lib/barber/precompiler.rb
CHANGED
@@ -39,6 +39,7 @@ module Barber
|
|
39
39
|
# break the compiler when \n are present in view helpers. Coffeescript block strings
|
40
40
|
# usually cause this problem.
|
41
41
|
# Case 3: "Normal" input. Reading from a file or something like that.
|
42
|
+
# Case 4: Sanitize using a RegExp (edge cases with jRuby + Rhino)
|
42
43
|
#
|
43
44
|
# Each one of these cases is covered by a test case. If you find another breaking
|
44
45
|
# use case please address it here with a regression test.
|
@@ -46,16 +47,35 @@ module Barber
|
|
46
47
|
begin
|
47
48
|
if template =~ /\A".+"\Z/m
|
48
49
|
# Case 1
|
49
|
-
|
50
|
+
sanitize_with_json(template)
|
50
51
|
else
|
51
52
|
# Case 2: evaluate a literal JS string in Ruby in the JS context
|
52
53
|
# to get an equivalent Ruby string back. This will convert liternal \n's
|
53
54
|
# to new lines. This is safer than trying to modify the string using regex.
|
54
|
-
|
55
|
+
sanitize_with_execjs(template)
|
55
56
|
end
|
56
57
|
rescue JSON::ParserError, ExecJS::RuntimeError
|
57
58
|
# Case 3
|
58
59
|
template
|
60
|
+
rescue ExecJS::ProgramError
|
61
|
+
# Case 4
|
62
|
+
sanitize_with_regexp(template)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def sanitize_with_json(template)
|
67
|
+
JSON.load(%Q|{"template":#{template}}|)['template']
|
68
|
+
end
|
69
|
+
|
70
|
+
def sanitize_with_execjs(template)
|
71
|
+
context.eval("'#{template}'")
|
72
|
+
end
|
73
|
+
|
74
|
+
def sanitize_with_regexp(template)
|
75
|
+
if template.respond_to? :gsub
|
76
|
+
template.gsub(/\\n/,"\n")
|
77
|
+
else
|
78
|
+
template
|
59
79
|
end
|
60
80
|
end
|
61
81
|
|
data/lib/barber/version.rb
CHANGED
data/test/precompiler_test.rb
CHANGED
@@ -20,20 +20,63 @@ class PrecompilerTest < MiniTest::Unit::TestCase
|
|
20
20
|
assert result
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
24
|
-
|
23
|
+
def test_handles_nil_template
|
24
|
+
result = compile nil
|
25
|
+
assert result
|
26
|
+
end
|
25
27
|
|
26
|
-
|
28
|
+
def test_handles_multiline_coffeescript_strings
|
29
|
+
result = compile template_with_multiline_coffeescript_strings
|
27
30
|
assert result
|
28
31
|
end
|
29
32
|
|
30
33
|
def test_handles_prescaped_JSON_strings
|
31
|
-
|
34
|
+
result = compile template_with_prescaped_JSON_strings
|
35
|
+
assert result
|
36
|
+
end
|
32
37
|
|
33
|
-
|
38
|
+
def test_handles_single_quoted_strings
|
39
|
+
result = compile template_with_single_quoted_strings
|
40
|
+
assert result
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_sanitize_with_json_multiline_coffeescript_strings
|
44
|
+
assert_raises JSON::ParserError do
|
45
|
+
sanitize_with_json template_with_multiline_coffeescript_strings
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_sanitize_multiline_coffeescript_strings
|
50
|
+
result = sanitize template_with_multiline_coffeescript_strings
|
51
|
+
assert_equal sanitized_template_with_multiline_coffeescript_strings, result
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_sanitize_prescaped_JSON_strings
|
55
|
+
result = sanitize template_with_prescaped_JSON_strings
|
56
|
+
assert result
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_sanitize_with_execjs_multiline_coffeescript_strings
|
60
|
+
result = sanitize_with_execjs template_with_multiline_coffeescript_strings
|
61
|
+
assert_equal sanitized_template_with_multiline_coffeescript_strings, result
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_sanitize_with_regexp_multiline_coffeescript_strings
|
65
|
+
result = sanitize_with_regexp template_with_multiline_coffeescript_strings
|
66
|
+
assert_equal sanitized_template_with_multiline_coffeescript_strings, result
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_sanitize_with_json_prescaped_JSON_strings
|
70
|
+
result = sanitize_with_json template_with_prescaped_JSON_strings
|
34
71
|
assert result
|
35
72
|
end
|
36
73
|
|
74
|
+
def test_sanitize_with_execjs_prescaped_JSON_strings
|
75
|
+
assert_raises ExecJS::RuntimeError, ExecJS::ProgramError do
|
76
|
+
sanitize_with_execjs template_with_prescaped_JSON_strings
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
37
80
|
def test_raises_an_error_on_bad_templates
|
38
81
|
assert_raises Barber::PrecompilerError do
|
39
82
|
compile "Hello {{"
|
@@ -58,4 +101,36 @@ class PrecompilerTest < MiniTest::Unit::TestCase
|
|
58
101
|
def compile(template)
|
59
102
|
Barber::Precompiler.compile template
|
60
103
|
end
|
104
|
+
|
105
|
+
def sanitize_with_json(template)
|
106
|
+
Barber::Precompiler.new.send :sanitize_with_json, template
|
107
|
+
end
|
108
|
+
|
109
|
+
def sanitize_with_execjs(template)
|
110
|
+
Barber::Precompiler.new.send :sanitize_with_execjs, template
|
111
|
+
end
|
112
|
+
|
113
|
+
def sanitize_with_regexp(template)
|
114
|
+
Barber::Precompiler.new.send :sanitize_with_regexp, template
|
115
|
+
end
|
116
|
+
|
117
|
+
def sanitize(template)
|
118
|
+
Barber::Precompiler.new.send :sanitize, template
|
119
|
+
end
|
120
|
+
|
121
|
+
def template_with_multiline_coffeescript_strings
|
122
|
+
'<h2>{{unbound view.title}}</h2>\n<ul>\n {{#each view.content}}\n {{view view.resultItemView\n contentBinding="this"\n selectedItemBinding="view.selectedItem"}}\n {{/each}}\n</ul>'
|
123
|
+
end
|
124
|
+
|
125
|
+
def sanitized_template_with_multiline_coffeescript_strings
|
126
|
+
"<h2>{{unbound view.title}}</h2>\n<ul>\n {{#each view.content}}\n {{view view.resultItemView\n contentBinding=\"this\"\n selectedItemBinding=\"view.selectedItem\"}}\n {{/each}}\n</ul>"
|
127
|
+
end
|
128
|
+
|
129
|
+
def template_with_prescaped_JSON_strings
|
130
|
+
'"<div class=\"comments\">\n <div class=\"span5 offset1\">\n {{#if view.comments.length}}\n <ul class=\"comments-list unstyled\">\n {{#each view.comments}}\n {{view Radium.CommentView commentBinding=\"this\"}}\n {{/each}}\n </ul>\n {{/if}}\n {{#if view.isError}}\n <p class=\"error\">Hmm, something wasn\'t done correctly. Try again?</p>\n {{/if}}\n <div style=\"clear: both\"></div>\n {{view view.commentTextArea}}\n </div>\n</div>\n"'
|
131
|
+
end
|
132
|
+
|
133
|
+
def template_with_single_quoted_strings
|
134
|
+
"<h2>{{unbound view.title}}</h2>\n<ul>\n {{#each view.content}}\n {{view view.resultItemView\n contentBinding='this'\n selectedItemBinding='view.selectedItem'}}\n {{/each}}\n</ul>"
|
135
|
+
end
|
61
136
|
end
|
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: barber
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.2
|
5
4
|
prerelease:
|
5
|
+
version: 0.1.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- tchak
|
@@ -10,67 +10,67 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-12-18 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
+
type: :runtime
|
17
|
+
version_requirements: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
16
23
|
name: execjs
|
24
|
+
prerelease: false
|
17
25
|
requirement: !ruby/object:Gem::Requirement
|
18
26
|
none: false
|
19
27
|
requirements:
|
20
28
|
- - ! '>='
|
21
29
|
- !ruby/object:Gem::Version
|
22
30
|
version: '0'
|
23
|
-
|
24
|
-
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
type: :development
|
25
33
|
version_requirements: !ruby/object:Gem::Requirement
|
26
34
|
none: false
|
27
35
|
requirements:
|
28
36
|
- - ! '>='
|
29
37
|
- !ruby/object:Gem::Version
|
30
38
|
version: '0'
|
31
|
-
- !ruby/object:Gem::Dependency
|
32
39
|
name: rake
|
40
|
+
prerelease: false
|
33
41
|
requirement: !ruby/object:Gem::Requirement
|
34
42
|
none: false
|
35
43
|
requirements:
|
36
44
|
- - ! '>='
|
37
45
|
- !ruby/object:Gem::Version
|
38
46
|
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
39
48
|
type: :development
|
40
|
-
prerelease: false
|
41
49
|
version_requirements: !ruby/object:Gem::Requirement
|
42
50
|
none: false
|
43
51
|
requirements:
|
44
52
|
- - ! '>='
|
45
53
|
- !ruby/object:Gem::Version
|
46
54
|
version: '0'
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
55
|
name: simplecov
|
56
|
+
prerelease: false
|
49
57
|
requirement: !ruby/object:Gem::Requirement
|
50
58
|
none: false
|
51
59
|
requirements:
|
52
60
|
- - ! '>='
|
53
61
|
- !ruby/object:Gem::Version
|
54
62
|
version: '0'
|
63
|
+
- !ruby/object:Gem::Dependency
|
55
64
|
type: :development
|
56
|
-
prerelease: false
|
57
65
|
version_requirements: !ruby/object:Gem::Requirement
|
58
66
|
none: false
|
59
67
|
requirements:
|
60
68
|
- - ! '>='
|
61
69
|
- !ruby/object:Gem::Version
|
62
70
|
version: '0'
|
63
|
-
- !ruby/object:Gem::Dependency
|
64
71
|
name: mocha
|
65
|
-
requirement: !ruby/object:Gem::Requirement
|
66
|
-
none: false
|
67
|
-
requirements:
|
68
|
-
- - ! '>='
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
version: '0'
|
71
|
-
type: :development
|
72
72
|
prerelease: false
|
73
|
-
|
73
|
+
requirement: !ruby/object:Gem::Requirement
|
74
74
|
none: false
|
75
75
|
requirements:
|
76
76
|
- - ! '>='
|
@@ -85,6 +85,7 @@ extensions: []
|
|
85
85
|
extra_rdoc_files: []
|
86
86
|
files:
|
87
87
|
- .gitignore
|
88
|
+
- .travis.yml
|
88
89
|
- Gemfile
|
89
90
|
- LICENSE
|
90
91
|
- README.md
|
@@ -116,22 +117,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
116
117
|
requirements:
|
117
118
|
- - ! '>='
|
118
119
|
- !ruby/object:Gem::Version
|
119
|
-
version: '0'
|
120
120
|
segments:
|
121
121
|
- 0
|
122
|
-
hash: -
|
122
|
+
hash: -4591227415054494269
|
123
|
+
version: '0'
|
123
124
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
125
|
none: false
|
125
126
|
requirements:
|
126
127
|
- - ! '>='
|
127
128
|
- !ruby/object:Gem::Version
|
128
|
-
version: '0'
|
129
129
|
segments:
|
130
130
|
- 0
|
131
|
-
hash: -
|
131
|
+
hash: -4591227415054494269
|
132
|
+
version: '0'
|
132
133
|
requirements: []
|
133
134
|
rubyforge_project:
|
134
|
-
rubygems_version: 1.8.
|
135
|
+
rubygems_version: 1.8.23
|
135
136
|
signing_key:
|
136
137
|
specification_version: 3
|
137
138
|
summary: Handlebars precompilation toolkit
|