ejs 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/ejs.rb +25 -15
  2. metadata +4 -4
data/lib/ejs.rb CHANGED
@@ -3,6 +3,19 @@
3
3
  # http://documentcloud.github.com/underscore/
4
4
 
5
5
  module EJS
6
+ JS_UNESCAPES = {
7
+ '\\' => '\\',
8
+ "'" => "'",
9
+ 'r' => "\r",
10
+ 'n' => "\n",
11
+ 't' => "\t",
12
+ 'u2028' => "\u2028",
13
+ 'u2029' => "\u2029"
14
+ }
15
+ JS_ESCAPES = JS_UNESCAPES.invert
16
+ JS_UNESCAPE_PATTERN = /\\(#{Regexp.union(JS_UNESCAPES.keys)})/
17
+ JS_ESCAPE_PATTERN = Regexp.union(JS_ESCAPES.keys)
18
+
6
19
  class << self
7
20
  attr_accessor :evaluation_pattern
8
21
  attr_accessor :interpolation_pattern
@@ -21,12 +34,10 @@ module EJS
21
34
  def compile(source, options = {})
22
35
  source = source.dup
23
36
 
24
- escape_quotes!(source)
37
+ js_escape!(source)
25
38
  replace_escape_tags!(source, options)
26
39
  replace_interpolation_tags!(source, options)
27
40
  replace_evaluation_tags!(source, options)
28
- escape_whitespace!(source)
29
-
30
41
  "function(obj){var __p=[],print=function(){__p.push.apply(__p,arguments);};" +
31
42
  "with(obj||{}){__p.push('#{source}');}return __p.join('');}"
32
43
  end
@@ -46,35 +57,34 @@ module EJS
46
57
  end
47
58
 
48
59
  protected
49
- def escape_quotes!(source)
50
- source.gsub!(/\\/) { '\\\\' }
51
- source.gsub!(/'/) { "\\'" }
60
+ def js_escape!(source)
61
+ source.gsub!(JS_ESCAPE_PATTERN) { |match| '\\' + JS_ESCAPES[match] }
62
+ source
63
+ end
64
+
65
+ def js_unescape!(source)
66
+ source.gsub!(JS_UNESCAPE_PATTERN) { |match| JS_UNESCAPES[match[1..-1]] }
67
+ source
52
68
  end
53
69
 
54
70
  def replace_escape_tags!(source, options)
55
71
  source.gsub!(options[:escape_pattern] || escape_pattern) do
56
- "',(''+" + $1.gsub(/\\'/, "'") + ")#{escape_function},'"
72
+ "',(''+#{js_unescape!($1)})#{escape_function},'"
57
73
  end
58
74
  end
59
75
 
60
76
  def replace_evaluation_tags!(source, options)
61
77
  source.gsub!(options[:evaluation_pattern] || evaluation_pattern) do
62
- "');" + $1.gsub(/\\'/, "'").gsub(/[\r\n\t]/, ' ') + "; __p.push('"
78
+ "'); #{js_unescape!($1)}; __p.push('"
63
79
  end
64
80
  end
65
81
 
66
82
  def replace_interpolation_tags!(source, options)
67
83
  source.gsub!(options[:interpolation_pattern] || interpolation_pattern) do
68
- "'," + $1.gsub(/\\'/, "'") + ",'"
84
+ "', #{js_unescape!($1)},'"
69
85
  end
70
86
  end
71
87
 
72
- def escape_whitespace!(source)
73
- source.gsub!(/\r/, '\\r')
74
- source.gsub!(/\n/, '\\n')
75
- source.gsub!(/\t/, '\\t')
76
- end
77
-
78
88
  def escape_function
79
89
  ".replace(/&/g, '&amp;')" +
80
90
  ".replace(/</g, '&lt;')" +
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ejs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-01 00:00:00.000000000 Z
12
+ date: 2012-06-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: execjs
16
- requirement: &70242739871960 !ruby/object:Gem::Requirement
16
+ requirement: &70341039116780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0.4'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70242739871960
24
+ version_requirements: *70341039116780
25
25
  description: Compile and evaluate EJS (Embedded JavaScript) templates from Ruby.
26
26
  email:
27
27
  - sstephenson@gmail.com