ejx 0.1 → 0.2

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: 7d977622dbdaa5af0afbc8c9c024439f104337c3b4133dbf2c7866656256f373
4
- data.tar.gz: e02233b080ee6d8dce9e94135993771464e33497a9102419d75ad8cf058f8984
3
+ metadata.gz: 9f52677d4f3ebc419528b87b62deb8c54c4c87b65193a58efd675ebf4cdec670
4
+ data.tar.gz: 1d3529c96bb7756822e0b1f0708f5f39ab6fabccb20376196ee1729cddf75e8c
5
5
  SHA512:
6
- metadata.gz: e5dd9308fe7904ad51c24475de414887f92cdc62c76111f2c98ff03342d4d2874e17d44cbea939ae9dc3285670ebce7b027afb278630931b96898a40657babd1
7
- data.tar.gz: f53c0b6ca412d595ae449f8db65f6038a152ba411f12debc9f57226d9679134c2a471f5e69a14572f8728c97bd0b797e9243322cf47e3af12532ce8719e4b6f4
6
+ metadata.gz: 7ca36c97ddf80cef591f4f2c0c0787c6c8ea09f52484ca30932aae9c8a1c4195cbdab3a5a8f12579a671e47fa0fcbdaeeefd0ba849218cfbb1c698ad70ec34df
7
+ data.tar.gz: dd5c1d1c21812e910e24fd156d0dca8458aacf6f04958a073c172fe4bf7cf6f8e3e11d6c0b52904dae3597cb13996267228ba5896f703198646f3c41a53330f8
@@ -1,5 +1,7 @@
1
1
  class Condenser::EjxTransformer < Condenser::NodeProcessor
2
2
 
3
+ attr_accessor :options
4
+
3
5
  def initialize(options = {})
4
6
  @options = options
5
7
  end
@@ -20,4 +22,4 @@ class Condenser::EjxTransformer < Condenser::NodeProcessor
20
22
  input[:source] = EJX::Template.new(input[:source], @options).to_module
21
23
  end
22
24
 
23
- end
25
+ end
@@ -14,18 +14,31 @@ function repalceejx(el, withItems) {
14
14
  }
15
15
  }
16
16
 
17
- export function append(items, to, escape, promises, promise_results) {
17
+ export function append(items, to, escape, promises, promiseResults) {
18
18
  if ( Array.isArray(items) ) {
19
19
  items.forEach((i) => append(i, to, escape, promises));
20
20
  } else {
21
21
  let method = Array.isArray(to) ? 'push' : 'append';
22
-
23
22
  if (items instanceof Promise) {
24
23
  let holder = document.createElement( "div");
25
24
  to[method](holder);
26
25
  promises.push( items.then((resolvedItems) => {
27
- repalceejx(holder, promise_results || resolvedItems)
26
+ if(holder.parentElement) {
27
+ repalceejx(holder, resolvedItems || promiseResults.flat())
28
+ } else if (Array.isArray(resolvedItems)) {
29
+ to.splice(to.indexOf(holder), 1, ...resolvedItems)
30
+ } else {
31
+ to.splice(to.indexOf(holder), 1, resolvedItems)
32
+ }
28
33
  }));
34
+ } else if (typeof items === 'string') {
35
+ if (escape) {
36
+ to[method](items);
37
+ } else {
38
+ var container = document.createElement(Array.isArray(to) ? 'div' : to.tagName);
39
+ container.innerHTML = items;
40
+ to[method](...container.childNodes);
41
+ }
29
42
  } else {
30
43
  to[method](items);
31
44
  }
@@ -51,6 +51,7 @@ class EJX::Template
51
51
  end
52
52
  end
53
53
  when :js
54
+ pre_js = pre_match
54
55
  scan_until(Regexp.new("(#{@js_close_tags.map{|s| Regexp.escape(s) }.join('|')})"))
55
56
  pm = pre_match
56
57
  open_modifier = @open_tag_modifiers.find { |k,v| pm.start_with?(v) }&.first
@@ -63,15 +64,18 @@ class EJX::Template
63
64
  import += ';' if !import.end_with?(';')
64
65
  @tree.first.imports << import
65
66
  @stack.pop
66
- elsif @tree.last.is_a?(EJX::Template::Subtemplate) && pm.match(/\A\s*\}\s*\)/m) && !pm.match(/\A\s*\}.*\{\s*\Z/m)
67
+ elsif @tree.last.is_a?(EJX::Template::Subtemplate) && pm.match(/\A\s*\}.*\)/m) && !pm.match(/\A\s*\}.*\{\s*\Z/m)
67
68
  subtemplate = @tree.pop
68
69
  subtemplate.children << pm.strip
69
70
  @tree.last.children << subtemplate
70
71
  @stack.pop
71
- elsif pm.match(/function\s*\([^\)]*\)\s*\{\s*\Z/m)
72
+ elsif open_modifier && (pm.match(/function\s*\([^\)]*\)\s*\{\s*\Z/m) || pm.match(/=>\s*\{\s*\Z/m))
72
73
  @tree << EJX::Template::Subtemplate.new(pm.strip, [open_modifier, close_modifier].compact)
73
74
  @stack.pop
74
75
  else
76
+ if open_modifier != :comment && !pre_js.empty? && @tree.last.children.last.is_a?(EJX::Template::JS)
77
+ @tree.last.children << EJX::Template::String.new(' ')
78
+ end
75
79
  value = EJX::Template::JS.new(pm.strip, [open_modifier, close_modifier].compact)
76
80
 
77
81
  @stack.pop
@@ -89,6 +93,10 @@ class EJX::Template
89
93
  end
90
94
  end
91
95
  when :html_tag
96
+ if @tree.last.children.last.is_a?(EJX::Template::JS)
97
+ @tree.last.children << EJX::Template::String.new(' ')
98
+ end
99
+
92
100
  scan_until(Regexp.new("(#{@js_start_tags.map{|s| Regexp.escape(s) }.join('|')}|\\/|[^\\s>]+)"))
93
101
  if @js_start_tags.include?(matched)
94
102
  @tree << EJX::Template::HTMLTag.new
@@ -182,18 +190,42 @@ class EJX::Template
182
190
  @tree.last.attrs << { key => matched }
183
191
  end
184
192
  when :html_tag_attr_value_double_quoted
185
- scan_until(/[^\"]*/)
193
+ quoted_value = []
194
+ scan_until(/("|\[\[=)/)
195
+ while matched == '[[='
196
+ quoted_value << pre_match if !pre_match.strip.empty?
197
+ scan_until(/\]\]/)
198
+ quoted_value << EJX::Template::JS.new(pre_match.strip)
199
+ scan_until(/("|\[\[=)/)
200
+ end
201
+ quoted_value << pre_match if !pre_match.strip.empty?
202
+ rewind(1)
203
+
204
+ quoted_value = EJX::Template::HTMLTag::AttributeValue.new(quoted_value)
205
+
186
206
  key = @tree.last.attrs.pop
187
- @tree.last.namespace = matched if key == 'xmlns'
188
- @tree.last.attrs << { key => matched }
207
+ @tree.last.namespace = quoted_value if key == 'xmlns'
208
+ @tree.last.attrs << { key => quoted_value }
189
209
  scan_until(/\"/)
190
210
  @stack.pop
191
211
  when :html_tag_attr_value_single_quoted
192
- scan_until(/[^']*/)
212
+ quoted_value = []
213
+ scan_until(/('|\[\[=)/)
214
+ while matched == '[[='
215
+ quoted_value << pre_match if !pre_match.strip.empty?
216
+ scan_until(/\]\]/)
217
+ quoted_value << EJX::Template::JS.new(pre_match.strip)
218
+ scan_until(/('|\[\[=)/)
219
+ end
220
+ quoted_value << pre_match if !pre_match.strip.empty?
221
+ rewind(1)
222
+
223
+ quoted_value = EJX::Template::HTMLTag::AttributeValue.new(quoted_value)
224
+
193
225
  key = @tree.last.attrs.pop
194
- @tree.last.namespace = matched if key == 'xmlns'
195
- @tree.last.attrs << { key => matched }
196
- scan_until(/'/)
226
+ @tree.last.namespace = quoted_value if key == 'xmlns'
227
+ @tree.last.attrs << { key => quoted_value }
228
+ scan_until(/\'/)
197
229
  @stack.pop
198
230
  end
199
231
  end
@@ -1,4 +1,6 @@
1
1
  class EJX::Template::HTMLTag
2
+
3
+ autoload :AttributeValue, File.expand_path('../html_tag/attribute_value', __FILE__)
2
4
 
3
5
  attr_accessor :tag_name, :attrs, :children, :namespace
4
6
 
@@ -21,7 +23,7 @@ class EJX::Template::HTMLTag
21
23
  output_var = var_generator.next
22
24
  js = "#{' '*indentation}var #{output_var} = document.createElement"
23
25
  js << if namespace
24
- "NS(#{JSON.generate(namespace)}, #{JSON.generate(tag_name)});\n"
26
+ "NS(#{namespace.to_js}, #{JSON.generate(tag_name)});\n"
25
27
  else
26
28
  "(#{JSON.generate(tag_name)});\n"
27
29
  end
@@ -29,7 +31,7 @@ class EJX::Template::HTMLTag
29
31
  @attrs.each do |attr|
30
32
  if attr.is_a?(Hash)
31
33
  attr.each do |k, v|
32
- js << "#{' '*indentation}#{output_var}.setAttribute(#{JSON.generate(k)}, #{JSON.generate(v)});\n"
34
+ js << "#{' '*indentation}#{output_var}.setAttribute(#{JSON.generate(k)}, #{v.to_js});\n"
33
35
  end
34
36
  else
35
37
  js << "#{' '*indentation}#{output_var}.setAttribute(#{JSON.generate(attr)}, \"\");\n"
@@ -0,0 +1,15 @@
1
+ class EJX::Template::HTMLTag::AttributeValue
2
+
3
+ def initialize(values)
4
+ @values = values
5
+ end
6
+
7
+ def to_js
8
+ if @values.empty?
9
+ JSON.generate('')
10
+ else
11
+ @values.map{ |v| v.is_a?(::String) ? JSON.generate(v) : v.value }.join(' + ')
12
+ end
13
+ end
14
+
15
+ end
@@ -1,6 +1,8 @@
1
1
  class EJX::Template::JS
2
2
 
3
- def initialize(value, modifiers)
3
+ attr_reader :value
4
+
5
+ def initialize(value, modifiers = [])
4
6
  @modifiers = modifiers
5
7
  @value = value
6
8
  end
@@ -19,8 +19,8 @@ module EJX::Template::ParseHelpers
19
19
  @source[@old_index...(@index-@matched.size)]
20
20
  end
21
21
 
22
- def rewind(by)
23
- @index -= 1
22
+ def rewind(by=1)
23
+ @index -= by
24
24
  end
25
25
 
26
26
  def seek(pos)
@@ -8,13 +8,14 @@ class EJX::Template::Subtemplate
8
8
  end
9
9
 
10
10
  def to_js(indentation: 4, var_generator: nil, append: "__output")
11
+ global_output_var = var_generator.next
11
12
  output_var = var_generator.next
12
- output = "#{' '*indentation}var #{output_var} = [];\n"
13
+
14
+ output = "#{' '*indentation}var #{global_output_var} = [];\n"
13
15
  output << "#{' '*indentation}__ejx_append("
14
- output << @children.first
15
- output << "\n"
16
-
17
- # var_generator ||= EJX::Template::VarGenerator.new
16
+ output << @children.first << "\n"
17
+ output << "#{' '*(indentation+4)}var #{output_var} = [];\n"
18
+
18
19
  @children[1..-2].each do |child|
19
20
  output << case child
20
21
  when EJX::Template::String
@@ -23,9 +24,11 @@ class EJX::Template::Subtemplate
23
24
  child.to_js(indentation: indentation + 4, var_generator: var_generator, append: output_var)
24
25
  end
25
26
  end
26
- output << ' '*indentation
27
- output << @children.last
28
- output << ", #{append}, true, __promises, #{output_var});\n"
27
+
28
+ output << ' '*(indentation+4) << "#{global_output_var}.push(#{output_var});\n";
29
+ output << ' '*(indentation+4) << "return #{output_var};\n";
30
+ output << ' '*indentation << @children.last
31
+ output << ", #{append}, true, __promises, #{global_output_var});\n"
29
32
 
30
33
  output
31
34
  end
@@ -1,3 +1,3 @@
1
1
  module EJX
2
- VERSION = '0.1'
2
+ VERSION = '0.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ejx
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Bracy
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-30 00:00:00.000000000 Z
11
+ date: 2020-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -68,7 +68,7 @@ dependencies:
68
68
  version: '0'
69
69
  description: |
70
70
  Compile EJX (Embedded JavaScript) templates to Javascript
71
- functions with Ruby.
71
+ with Ruby.
72
72
  email:
73
73
  - jonbracy@gmail.com
74
74
  executables: []
@@ -83,6 +83,7 @@ files:
83
83
  - lib/ejx/template.rb
84
84
  - lib/ejx/template/base.rb
85
85
  - lib/ejx/template/html_tag.rb
86
+ - lib/ejx/template/html_tag/attribute_value.rb
86
87
  - lib/ejx/template/js.rb
87
88
  - lib/ejx/template/parse_helpers.rb
88
89
  - lib/ejx/template/string.rb
@@ -93,7 +94,7 @@ homepage: https://github.com/malomalo/ejx
93
94
  licenses:
94
95
  - MIT
95
96
  metadata: {}
96
- post_install_message:
97
+ post_install_message:
97
98
  rdoc_options: []
98
99
  require_paths:
99
100
  - lib
@@ -108,8 +109,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
109
  - !ruby/object:Gem::Version
109
110
  version: '0'
110
111
  requirements: []
111
- rubygems_version: 3.1.2
112
- signing_key:
112
+ rubygems_version: 3.1.4
113
+ signing_key:
113
114
  specification_version: 4
114
115
  summary: EJX Template Compiler
115
116
  test_files: []