smolbars 0.1.4 → 0.1.5

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: cb5c650020d40b4cbb2eba845fd599113c44de878d58bb7015b1573248a3dba0
4
+ data.tar.gz: 00f4d9f37f81a1c081d1db373eb62819f2b852ea5b7f50b83884381f084ddd44
5
5
  SHA512:
6
- metadata.gz: fa466da75da4abe7376b214290c873e07702cacc398636742c2e5db8aec1bfbeab68b3af0a24ea13e40d5a122671b8601ee3550ac9da9cfbae221d78717c8015
7
- data.tar.gz: 42d12dbd05de3d84757e5553b43dc651c15e13a5f4144e74d5694bab388201e61ec1546b85e57d074fd8cf6bf4a193e69939d40d789c16725ba650a1d545ad94
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)
@@ -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.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.4
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-04-13 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.22
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