smolbars 0.1.2 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c86063c597fa360633865862f4b842436d6d5489b84cc9d1f9510e20dbf28dd
4
- data.tar.gz: ff87fb9498227b9f241784568aaf179e0e4830dc0493021ea3cb07ef1339695a
3
+ metadata.gz: cb5c650020d40b4cbb2eba845fd599113c44de878d58bb7015b1573248a3dba0
4
+ data.tar.gz: 00f4d9f37f81a1c081d1db373eb62819f2b852ea5b7f50b83884381f084ddd44
5
5
  SHA512:
6
- metadata.gz: df4fbdf91b1fe83b5b09882265d4d6674d9f32c9099aa5f5fea0ae176d001ca772ccb23784ea0d86949d0b91e24a7fc60f58bd1c1a21820b7b59701bdf3628ca
7
- data.tar.gz: 8460b1163060ce6759b643213e84b4a09f3f262ce3d53cfc0adc336243038ae052f53b9fcd931f004c4b8ad579065fd1c9def115d25db2708e5a602206978416
6
+ metadata.gz: ba0bb6952537564b7c0d7324f3aef4444ba7c142d478d5d283c617fb0de2aa5aaa26797c6e23a9e76b62efdfce4b77751bf595c4eafa3639aa5776ef55d5f7aa
7
+ data.tar.gz: bfd01f873c70f5a71272f4ad55f2a2b20f500a0396028d07d615df7cfe1f614e30ba1ef33e63bd199543b544f88a5078b74e7b4ada199e8d6c66b0e04eff8499
@@ -4,6 +4,18 @@ require 'securerandom'
4
4
 
5
5
  module Smolbars
6
6
  class Context
7
+ JS_ESCAPE_MAP = {
8
+ '\\' => '\\\\',
9
+ "</" => '<\/',
10
+ "\r\n" => '\n',
11
+ "\n" => '\n',
12
+ "\r" => '\n',
13
+ '"' => '\\"',
14
+ "'" => "\\'",
15
+ "`" => "\\`",
16
+ "$" => "\\$"
17
+ }.freeze
18
+
7
19
  def initialize(**kwargs)
8
20
  @js = MiniRacer::Context.new(kwargs)
9
21
  @js.load(Handlebars::Source.bundled_path)
@@ -13,11 +25,8 @@ module Smolbars
13
25
  # hope the template passed in does not form invalid Ruby. So don't use templates with backtick characters without
14
26
  # manually escaping them
15
27
  def compile(template)
16
- if template.include?("`")
17
- raise RuntimeError.new("template cannot contain a backtick character '`'")
18
- end
19
28
  handle = fn_handle
20
- invocation = %Q{var #{handle} = Handlebars.compile(`#{template}`);}
29
+ invocation = %{var #{handle} = Handlebars.compile("#{escape_javascript(template)}");}
21
30
  @js.eval(invocation)
22
31
  ::Smolbars::Template.new(self, handle)
23
32
  end
@@ -27,7 +36,7 @@ module Smolbars
27
36
  end
28
37
 
29
38
  def load_pattern(pattern)
30
- Dir[pattern].each{ |path| load(path) }
39
+ Dir[pattern].each { |path| load(path) }
31
40
  end
32
41
 
33
42
  def load(path)
@@ -39,5 +48,14 @@ module Smolbars
39
48
  def fn_handle
40
49
  "js_fn_#{SecureRandom.hex}"
41
50
  end
51
+
52
+ def escape_javascript(javascript)
53
+ javascript = javascript.to_s
54
+ if javascript.empty?
55
+ ''
56
+ else
57
+ javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"']|[`]|[$])/u, JS_ESCAPE_MAP)
58
+ end
59
+ end
42
60
  end
43
61
  end
@@ -8,8 +8,7 @@ module Smolbars
8
8
  if args.length == 0
9
9
  invocation = "%s(%s)" % [@fn, kwargs.to_json]
10
10
  else
11
- raise "unsupported"
12
- invocation = "%s(%s)" % [@fn, args.to_json]
11
+ invocation = "%s(%s)" % [@fn, args.first.to_json]
13
12
  end
14
13
  @context.eval(invocation)
15
14
  end
@@ -1,3 +1,3 @@
1
1
  module Smolbars
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smolbars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Lowell
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-02-15 00:00:00.000000000 Z
12
+ date: 2022-08-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mini_racer
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
100
  requirements: []
101
- rubygems_version: 3.2.23
101
+ rubygems_version: 3.1.6
102
102
  signing_key:
103
103
  specification_version: 4
104
104
  summary: Ruby bindings for the smolbars.js templating library