better_html 0.0.5 → 0.0.7

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
  SHA1:
3
- metadata.gz: 8589c65746614e3e1b7f76afaae127cb4f0bb186
4
- data.tar.gz: b4fb1616fc7d4cbc0c5dcb96bd0106a895c98b4c
3
+ metadata.gz: 9c51581d5c834e5560f9ba999ec11a4902f57cb7
4
+ data.tar.gz: cdb3a16f6c3cbfe74e7fc70d7685e63d2c00099b
5
5
  SHA512:
6
- metadata.gz: '08611d36aaa2ebbc847d5250635b66d5a3fb2b3ac93ef3e89bb0410f4f6554d033f2023d6d65f47892d6192f07eee7dc87a1eeaec26acc2b8e26ffb502649d58'
7
- data.tar.gz: 72c4cb2c216c10e9dae458eac55788beb292f4ffda6c1de42bd5e2bedceb71fadf585050be349123e6e57fdec9c3cb90ceb09132018845b40ca3776b5cfd4b00
6
+ metadata.gz: ccdcf32b31d7aec4852181f6b0ccdaaad3b14fddcf0b4a08bf5b50ac298ac1bf9ff3ea26880835961c0cf571de5aeaab05613bee842322c31f51da703c1abf2c
7
+ data.tar.gz: b9e2780e3b16b06bdbd124c2b1d0026656ae68a9d9665121d62fc9ed614091e649037e263eccb7f3593123ee07bb1bd052ceb8c8ba75fe1822661a11fefc7fcc
data/Rakefile CHANGED
@@ -15,8 +15,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
15
15
  rdoc.rdoc_files.include('lib/**/*.rb')
16
16
  end
17
17
 
18
- Bundler::GemHelper.install_tasks
19
-
20
18
  require 'rake/testtask'
21
19
 
22
20
  Rake::TestTask.new(:test) do |t|
@@ -1,25 +1,27 @@
1
- require 'erubis/engine/eruby'
1
+ require 'erubi'
2
2
  require 'html_tokenizer'
3
3
  require_relative 'token'
4
4
  require_relative 'location'
5
5
 
6
6
  module BetterHtml
7
7
  class NodeIterator
8
- class HtmlErb < ::Erubis::Eruby
8
+ class HtmlErb < ::Erubi::Engine
9
9
  attr_reader :tokens
10
10
  attr_reader :parser
11
11
 
12
+ REGEXP_WITHOUT_TRIM = /<%(={1,2}|-|\#|%)?(.*?)(?:[-=])?()?%>([ \t]*\r?\n)?/m
13
+
12
14
  def initialize(document)
13
15
  @parser = HtmlTokenizer::Parser.new
14
16
  @tokens = []
15
- super
17
+ super(document, regexp: REGEXP_WITHOUT_TRIM, trim: false)
16
18
  end
17
19
 
18
- def add_text(src, text)
20
+ def add_text(text)
19
21
  @parser.parse(text) { |*args| add_tokens(*args) }
20
22
  end
21
23
 
22
- def add_stmt(src, code)
24
+ def add_code(code)
23
25
  text = "<%#{code}%>"
24
26
  start = @parser.document_length
25
27
  stop = start + text.size
@@ -32,25 +34,12 @@ module BetterHtml
32
34
  @parser.append_placeholder(text)
33
35
  end
34
36
 
35
- def add_expr_literal(src, code)
36
- text = "<%=#{code}%>"
37
- start = @parser.document_length
38
- stop = start + text.size
39
- @tokens << Token.new(
40
- type: :expr_literal,
41
- code: code,
42
- text: text,
43
- location: Location.new(start, stop, @parser.line_number, @parser.column_number)
44
- )
45
- @parser.append_placeholder(text)
46
- end
47
-
48
- def add_expr_escaped(src, code)
49
- text = "<%==#{code}%>"
37
+ def add_expression(indicator, code)
38
+ text = "<%#{indicator}#{code}%>"
50
39
  start = @parser.document_length
51
40
  stop = start + text.size
52
41
  @tokens << Token.new(
53
- type: :expr_escaped,
42
+ type: indicator == '=' ? :expr_literal : :expr_escaped,
54
43
  code: code,
55
44
  text: text,
56
45
  location: Location.new(start, stop, @parser.line_number, @parser.column_number)
@@ -1,38 +1,32 @@
1
- require 'erubis/engine/eruby'
1
+ require 'erubi'
2
2
  require_relative 'token'
3
3
  require_relative 'location'
4
4
 
5
5
  module BetterHtml
6
6
  class NodeIterator
7
- class JavascriptErb < ::Erubis::Eruby
7
+ class JavascriptErb < ::Erubi::Engine
8
8
  attr_reader :tokens
9
9
 
10
10
  def initialize(document)
11
11
  @document = ""
12
12
  @tokens = []
13
- super
13
+ super(document, regexp: HtmlErb::REGEXP_WITHOUT_TRIM, trim: false)
14
14
  end
15
15
 
16
- def add_text(src, text)
16
+ def add_text(text)
17
17
  add_token(:text, text)
18
18
  append(text)
19
19
  end
20
20
 
21
- def add_stmt(src, code)
21
+ def add_code(code)
22
22
  text = "<%#{code}%>"
23
23
  add_token(:stmt, text, code)
24
24
  append(text)
25
25
  end
26
26
 
27
- def add_expr_literal(src, code)
28
- text = "<%=#{code}%>"
29
- add_token(:expr_literal, text, code)
30
- append(text)
31
- end
32
-
33
- def add_expr_escaped(src, code)
34
- text = "<%==#{code}%>"
35
- add_token(:expr_escaped, text, code)
27
+ def add_expression(indicator, code)
28
+ text = "<%#{indicator}#{code}%>"
29
+ add_token(indicator == '=' ? :expr_literal : :expr_escaped, text, code)
36
30
  append(text)
37
31
  end
38
32
 
@@ -1,3 +1,3 @@
1
1
  module BetterHtml
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -0,0 +1,99 @@
1
+ require 'test_helper'
2
+
3
+ module BetterHtml
4
+ class NodeIterator
5
+ class HtmlErbTest < ActiveSupport::TestCase
6
+ test "text" do
7
+ scanner = BetterHtml::NodeIterator::HtmlErb.new("just some text")
8
+ assert_equal 1, scanner.tokens.size
9
+ token = scanner.tokens[0]
10
+ assert_attributes ({ type: :text, text: 'just some text', code: nil }), token
11
+ assert_attributes ({ start: 0, stop: 14, line: 1, column: 0 }), token.location
12
+ end
13
+
14
+ test "statement" do
15
+ scanner = BetterHtml::NodeIterator::HtmlErb.new("<% statement %>")
16
+ assert_equal 1, scanner.tokens.size
17
+ token = scanner.tokens[0]
18
+ assert_attributes ({ type: :stmt, text: '<% statement %>', code: ' statement ' }), token
19
+ assert_attributes ({ start: 0, stop: 15, line: 1, column: 0 }), token.location
20
+ end
21
+
22
+ test "expression literal" do
23
+ scanner = BetterHtml::NodeIterator::HtmlErb.new("<%= literal %>")
24
+ assert_equal 1, scanner.tokens.size
25
+ token = scanner.tokens[0]
26
+ assert_attributes ({ type: :expr_literal, text: '<%= literal %>', code: ' literal ' }), token
27
+ assert_attributes ({ start: 0, stop: 14, line: 1, column: 0 }), token.location
28
+ end
29
+
30
+ test "expression escaped" do
31
+ scanner = BetterHtml::NodeIterator::HtmlErb.new("<%== escaped %>")
32
+ assert_equal 1, scanner.tokens.size
33
+ token = scanner.tokens[0]
34
+ assert_attributes ({ type: :expr_escaped, text: '<%== escaped %>', code: ' escaped ' }), token
35
+ assert_attributes ({ start: 0, stop: 15, line: 1, column: 0 }), token.location
36
+ end
37
+
38
+ test "line number for multi-line statements" do
39
+ scanner = BetterHtml::NodeIterator::HtmlErb.new("before <% multi\nline %> after")
40
+ assert_equal 3, scanner.tokens.size
41
+
42
+ assert_attributes ({ type: :text, text: 'before ' }), scanner.tokens[0]
43
+ assert_attributes ({ line: 1 }), scanner.tokens[0].location
44
+
45
+ assert_attributes ({ type: :stmt, text: "<% multi\nline %>" }), scanner.tokens[1]
46
+ assert_attributes ({ line: 1 }), scanner.tokens[1].location
47
+
48
+ assert_attributes ({ type: :text, text: " after" }), scanner.tokens[2]
49
+ assert_attributes ({ line: 2 }), scanner.tokens[2].location
50
+ end
51
+
52
+ test "multi-line statements with trim" do
53
+ scanner = BetterHtml::NodeIterator::HtmlErb.new("before\n<% multi\nline -%>\nafter")
54
+ assert_equal 4, scanner.tokens.size
55
+
56
+ assert_attributes ({ type: :text, text: "before\n" }), scanner.tokens[0]
57
+ assert_attributes ({ line: 1 }), scanner.tokens[0].location
58
+
59
+ assert_attributes ({ type: :stmt, text: "<% multi\nline %>" }), scanner.tokens[1]
60
+ assert_attributes ({ line: 2 }), scanner.tokens[1].location
61
+
62
+ assert_attributes ({ type: :text, text: "\n" }), scanner.tokens[2]
63
+ assert_attributes ({ line: 3 }), scanner.tokens[2].location
64
+
65
+ assert_attributes ({ type: :text, text: "after" }), scanner.tokens[3]
66
+ assert_attributes ({ line: 4 }), scanner.tokens[3].location
67
+ end
68
+
69
+ test "multi-line expression with trim" do
70
+ scanner = BetterHtml::NodeIterator::HtmlErb.new("before\n<%= multi\nline -%>\nafter")
71
+ assert_equal 4, scanner.tokens.size
72
+
73
+ assert_attributes ({ type: :text, text: "before\n" }), scanner.tokens[0]
74
+ assert_attributes ({ line: 1 }), scanner.tokens[0].location
75
+
76
+ assert_attributes ({ type: :expr_literal, text: "<%= multi\nline %>" }), scanner.tokens[1]
77
+ assert_attributes ({ line: 2 }), scanner.tokens[1].location
78
+
79
+ assert_attributes ({ type: :text, text: "\n" }), scanner.tokens[2]
80
+ assert_attributes ({ line: 3 }), scanner.tokens[2].location
81
+
82
+ assert_attributes ({ type: :text, text: "after" }), scanner.tokens[3]
83
+ assert_attributes ({ line: 4 }), scanner.tokens[3].location
84
+ end
85
+
86
+ private
87
+
88
+ def assert_attributes(attributes, token)
89
+ attributes.each do |key, value|
90
+ if value.nil?
91
+ assert_nil token.send(key)
92
+ else
93
+ assert_equal value, token.send(key)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -254,7 +254,7 @@ module BetterHtml
254
254
  EOF
255
255
 
256
256
  assert_equal 1, errors.size
257
- assert_equal "<% if foo? \n%>", errors.first.token.text
257
+ assert_equal "<% if foo? %>", errors.first.token.text
258
258
  assert_equal "erb statement not allowed here; did you mean '<%=' ?", errors.first.message
259
259
  end
260
260
 
@@ -266,7 +266,7 @@ module BetterHtml
266
266
  JS
267
267
 
268
268
  assert_equal 1, errors.size
269
- assert_equal "<% if foo \n%>", errors.first.token.text
269
+ assert_equal "<% if foo %>", errors.first.token.text
270
270
  assert_equal "erb statement not allowed here; did you mean '<%=' ?", errors.first.message
271
271
  end
272
272
 
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francois Chagnon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-30 00:00:00.000000000 Z
11
+ date: 2017-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: erubis
14
+ name: erubi
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.7.0
19
+ version: '1.4'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.7.0
26
+ version: '1.4'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +108,7 @@ files:
108
108
  - lib/tasks/better_html_tasks.rake
109
109
  - test/better_html/better_erb/implementation_test.rb
110
110
  - test/better_html/helpers_test.rb
111
+ - test/better_html/node_iterator/html_erb_test.rb
111
112
  - test/better_html/node_iterator/html_lodash_test.rb
112
113
  - test/better_html/node_iterator_test.rb
113
114
  - test/better_html/test_helper/ruby_expr_test.rb
@@ -212,6 +213,7 @@ test_files:
212
213
  - test/better_html/helpers_test.rb
213
214
  - test/better_html/node_iterator_test.rb
214
215
  - test/better_html/node_iterator/html_lodash_test.rb
216
+ - test/better_html/node_iterator/html_erb_test.rb
215
217
  - test/better_html/better_erb/implementation_test.rb
216
218
  - test/better_html/test_helper/safe_erb_tester_test.rb
217
219
  - test/better_html/test_helper/safe_lodash_tester_test.rb