smolbars 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40d10c7406cd516218824c76f8a4fac9ba3c6caaa7002b30519943d64adc6857
4
- data.tar.gz: 0d27753a31290273c8174e3d5e4270f50a819bd00d03c6d0d75785f4efa9c424
3
+ metadata.gz: ffed6204efd252089f232757ab1bc4ff88293cf3ee12d03e4ace867e443b8f77
4
+ data.tar.gz: b9f213c71c01d1bb23c90276816b2a5f5e3360d0f93b81ade1757d19bfdebf6e
5
5
  SHA512:
6
- metadata.gz: fa466da75da4abe7376b214290c873e07702cacc398636742c2e5db8aec1bfbeab68b3af0a24ea13e40d5a122671b8601ee3550ac9da9cfbae221d78717c8015
7
- data.tar.gz: 42d12dbd05de3d84757e5553b43dc651c15e13a5f4144e74d5694bab388201e61ec1546b85e57d074fd8cf6bf4a193e69939d40d789c16725ba650a1d545ad94
6
+ metadata.gz: df6e4cb8f39d64beadfd49f7113dfa32fd53436eaee9e631b68823ce8c7fdcdf2e2e01dcfb05f6ae1d580e9a7d640867340211f33360a4b1780f3a447399503a
7
+ data.tar.gz: e9a5ba81a5c96092f107d3219695a0145fc1e582f1aacc33e6ff40a1dda1d11cb216f8361baf824dbcba6c91fcf8d7aa5fa774321858a1a5fcaa373caed895d8
data/README.mdown CHANGED
@@ -79,9 +79,19 @@ underlying JS instance
79
79
 
80
80
  rspec spec/
81
81
 
82
+ ### Tests in Docker
83
+
84
+ Building an image:
85
+
86
+ docker compose run --rm app bundle install
87
+
88
+ Running tests:
89
+
90
+ docker compose run --rm app bundle exec rspec spec/
91
+
82
92
 
83
93
  [1]: https://github.com/rubyjs/mini_racer "mini_racer"
84
94
  [2]: https://github.com/wycats/handlebars.js "Handlebars JavaScript templating library"
85
95
  [3]: https://github.com/cowboyd/handlebars.rb "Handlebars Ruby library"
86
96
  [4]: https://github.com/cowboyd/therubyracer "The Ruby Racer"
87
- [5]: https://github.com/combinaut/minibars "Minibars"
97
+ [5]: https://github.com/combinaut/minibars "Minibars"
@@ -4,8 +4,20 @@ 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
- @js = MiniRacer::Context.new(kwargs)
20
+ @js = MiniRacer::Context.new(**kwargs)
9
21
  @js.load(Handlebars::Source.bundled_path)
10
22
  end
11
23
 
@@ -14,7 +26,7 @@ module Smolbars
14
26
  # manually escaping them
15
27
  def compile(template)
16
28
  handle = fn_handle
17
- invocation = %Q{var #{handle} = Handlebars.compile(`#{template.gsub('`', "\`")}`);}
29
+ invocation = %{var #{handle} = Handlebars.compile("#{escape_javascript(template)}");}
18
30
  @js.eval(invocation)
19
31
  ::Smolbars::Template.new(self, handle)
20
32
  end
@@ -24,7 +36,7 @@ module Smolbars
24
36
  end
25
37
 
26
38
  def load_pattern(pattern)
27
- Dir[pattern].each{ |path| load(path) }
39
+ Dir[pattern].each { |path| load(path) }
28
40
  end
29
41
 
30
42
  def load(path)
@@ -36,5 +48,14 @@ module Smolbars
36
48
  def fn_handle
37
49
  "js_fn_#{SecureRandom.hex}"
38
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
39
60
  end
40
61
  end
@@ -1,3 +1,3 @@
1
1
  module Smolbars
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.6"
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.4
4
+ version: 0.1.6
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-04-13 00:00:00.000000000 Z
12
+ date: 2023-03-21 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.22
101
+ rubygems_version: 3.4.7
102
102
  signing_key:
103
103
  specification_version: 4
104
104
  summary: Ruby bindings for the smolbars.js templating library