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 +4 -4
- data/Rakefile +0 -2
- data/lib/better_html/node_iterator/html_erb.rb +10 -21
- data/lib/better_html/node_iterator/javascript_erb.rb +8 -14
- data/lib/better_html/version.rb +1 -1
- data/test/better_html/node_iterator/html_erb_test.rb +99 -0
- data/test/better_html/test_helper/safe_erb_tester_test.rb +2 -2
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c51581d5c834e5560f9ba999ec11a4902f57cb7
|
4
|
+
data.tar.gz: cdb3a16f6c3cbfe74e7fc70d7685e63d2c00099b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ccdcf32b31d7aec4852181f6b0ccdaaad3b14fddcf0b4a08bf5b50ac298ac1bf9ff3ea26880835961c0cf571de5aeaab05613bee842322c31f51da703c1abf2c
|
7
|
+
data.tar.gz: b9e2780e3b16b06bdbd124c2b1d0026656ae68a9d9665121d62fc9ed614091e649037e263eccb7f3593123ee07bb1bd052ceb8c8ba75fe1822661a11fefc7fcc
|
data/Rakefile
CHANGED
@@ -1,25 +1,27 @@
|
|
1
|
-
require '
|
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 < ::
|
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(
|
20
|
+
def add_text(text)
|
19
21
|
@parser.parse(text) { |*args| add_tokens(*args) }
|
20
22
|
end
|
21
23
|
|
22
|
-
def
|
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
|
36
|
-
text = "
|
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 '
|
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 < ::
|
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(
|
16
|
+
def add_text(text)
|
17
17
|
add_token(:text, text)
|
18
18
|
append(text)
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
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
|
28
|
-
text = "
|
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
|
|
data/lib/better_html/version.rb
CHANGED
@@ -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 "<%
|
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 "<%
|
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.
|
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-
|
11
|
+
date: 2017-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: erubi
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
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:
|
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
|