better_html 1.0.14 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -0
- data/Rakefile +19 -14
- data/ext/better_html_ext/better_html.h +1 -0
- data/ext/better_html_ext/extconf.rb +16 -0
- data/ext/better_html_ext/html_tokenizer.c +12 -0
- data/ext/better_html_ext/html_tokenizer.h +7 -0
- data/ext/better_html_ext/parser.c +793 -0
- data/ext/better_html_ext/parser.h +93 -0
- data/ext/better_html_ext/tokenizer.c +717 -0
- data/ext/better_html_ext/tokenizer.h +80 -0
- data/lib/better_html/ast/iterator.rb +14 -9
- data/lib/better_html/ast/node.rb +4 -2
- data/lib/better_html/better_erb/erubi_implementation.rb +43 -39
- data/lib/better_html/better_erb/runtime_checks.rb +140 -133
- data/lib/better_html/better_erb/validated_output_buffer.rb +30 -22
- data/lib/better_html/better_erb.rb +58 -54
- data/lib/better_html/config.rb +7 -4
- data/lib/better_html/errors.rb +4 -2
- data/lib/better_html/helpers.rb +7 -3
- data/lib/better_html/html_attributes.rb +6 -2
- data/lib/better_html/parser.rb +21 -14
- data/lib/better_html/railtie.rb +8 -4
- data/lib/better_html/test_helper/ruby_node.rb +15 -9
- data/lib/better_html/test_helper/safe_erb/allowed_script_type.rb +8 -4
- data/lib/better_html/test_helper/safe_erb/base.rb +12 -9
- data/lib/better_html/test_helper/safe_erb/no_javascript_tag_helper.rb +7 -3
- data/lib/better_html/test_helper/safe_erb/no_statements.rb +7 -3
- data/lib/better_html/test_helper/safe_erb/script_interpolation.rb +9 -4
- data/lib/better_html/test_helper/safe_erb/tag_interpolation.rb +23 -20
- data/lib/better_html/test_helper/safe_erb_tester.rb +35 -33
- data/lib/better_html/test_helper/safe_lodash_tester.rb +36 -34
- data/lib/better_html/test_helper/safety_error.rb +2 -0
- data/lib/better_html/tokenizer/base_erb.rb +19 -15
- data/lib/better_html/tokenizer/html_erb.rb +3 -2
- data/lib/better_html/tokenizer/html_lodash.rb +22 -13
- data/lib/better_html/tokenizer/javascript_erb.rb +3 -1
- data/lib/better_html/tokenizer/location.rb +17 -6
- data/lib/better_html/tokenizer/token.rb +2 -0
- data/lib/better_html/tokenizer/token_array.rb +8 -8
- data/lib/better_html/tree/attribute.rb +10 -6
- data/lib/better_html/tree/attributes_list.rb +9 -5
- data/lib/better_html/tree/tag.rb +10 -6
- data/lib/better_html/version.rb +3 -1
- data/lib/better_html.rb +19 -17
- data/lib/tasks/better_html_tasks.rake +1 -0
- metadata +43 -148
- data/lib/better_html/better_erb/erubis_implementation.rb +0 -44
- data/test/better_html/better_erb/implementation_test.rb +0 -406
- data/test/better_html/errors_test.rb +0 -13
- data/test/better_html/helpers_test.rb +0 -49
- data/test/better_html/parser_test.rb +0 -314
- data/test/better_html/test_helper/ruby_node_test.rb +0 -288
- data/test/better_html/test_helper/safe_erb/allowed_script_type_test.rb +0 -45
- data/test/better_html/test_helper/safe_erb/no_javascript_tag_helper_test.rb +0 -37
- data/test/better_html/test_helper/safe_erb/no_statements_test.rb +0 -128
- data/test/better_html/test_helper/safe_erb/script_interpolation_test.rb +0 -149
- data/test/better_html/test_helper/safe_erb/tag_interpolation_test.rb +0 -303
- data/test/better_html/test_helper/safe_lodash_tester_test.rb +0 -90
- data/test/better_html/tokenizer/html_erb_test.rb +0 -180
- data/test/better_html/tokenizer/html_lodash_test.rb +0 -98
- data/test/better_html/tokenizer/location_test.rb +0 -75
- data/test/better_html/tokenizer/token_array_test.rb +0 -145
- data/test/better_html/tokenizer/token_test.rb +0 -15
- data/test/dummy/README.rdoc +0 -28
- data/test/dummy/Rakefile +0 -6
- data/test/dummy/app/assets/javascripts/application.js +0 -13
- data/test/dummy/app/assets/stylesheets/application.css +0 -15
- data/test/dummy/app/controllers/application_controller.rb +0 -5
- data/test/dummy/app/helpers/application_helper.rb +0 -2
- data/test/dummy/app/views/layouts/application.html.erb +0 -14
- data/test/dummy/bin/bundle +0 -3
- data/test/dummy/bin/rails +0 -4
- data/test/dummy/bin/rake +0 -4
- data/test/dummy/bin/setup +0 -29
- data/test/dummy/config/application.rb +0 -26
- data/test/dummy/config/boot.rb +0 -5
- data/test/dummy/config/database.yml +0 -25
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/environments/development.rb +0 -41
- data/test/dummy/config/environments/production.rb +0 -79
- data/test/dummy/config/environments/test.rb +0 -42
- data/test/dummy/config/initializers/assets.rb +0 -11
- data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/test/dummy/config/initializers/cookies_serializer.rb +0 -3
- data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/test/dummy/config/initializers/inflections.rb +0 -16
- data/test/dummy/config/initializers/mime_types.rb +0 -4
- data/test/dummy/config/initializers/session_store.rb +0 -3
- data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/test/dummy/config/locales/en.yml +0 -23
- data/test/dummy/config/routes.rb +0 -56
- data/test/dummy/config/secrets.yml +0 -22
- data/test/dummy/config.ru +0 -4
- data/test/dummy/public/404.html +0 -67
- data/test/dummy/public/422.html +0 -67
- data/test/dummy/public/500.html +0 -66
- data/test/dummy/public/favicon.ico +0 -0
- data/test/test_helper.rb +0 -30
@@ -1,180 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'better_html/tokenizer/html_erb'
|
3
|
-
|
4
|
-
module BetterHtml
|
5
|
-
module Tokenizer
|
6
|
-
class HtmlErbTest < ActiveSupport::TestCase
|
7
|
-
test "text" do
|
8
|
-
scanner = HtmlErb.new(buffer("just some text"))
|
9
|
-
assert_equal 1, scanner.tokens.size
|
10
|
-
|
11
|
-
assert_attributes ({
|
12
|
-
type: :text,
|
13
|
-
loc: { begin_pos: 0, end_pos: 14, source: 'just some text' }
|
14
|
-
}), scanner.tokens[0]
|
15
|
-
end
|
16
|
-
|
17
|
-
test "statement" do
|
18
|
-
scanner = HtmlErb.new(buffer("<% statement %>"))
|
19
|
-
assert_equal 3, scanner.tokens.size
|
20
|
-
|
21
|
-
assert_attributes ({ type: :erb_begin, loc: { begin_pos: 0, end_pos: 2, source: '<%' } }), scanner.tokens[0]
|
22
|
-
assert_attributes ({ type: :code, loc: { begin_pos: 2, end_pos: 13, source: ' statement ' } }), scanner.tokens[1]
|
23
|
-
assert_attributes ({ type: :erb_end, loc: { begin_pos: 13, end_pos: 15, source: '%>' } }), scanner.tokens[2]
|
24
|
-
end
|
25
|
-
|
26
|
-
test "debug statement" do
|
27
|
-
scanner = HtmlErb.new(buffer("<%# statement %>"))
|
28
|
-
assert_equal 4, scanner.tokens.size
|
29
|
-
|
30
|
-
assert_attributes ({ type: :erb_begin, loc: { begin_pos: 0, end_pos: 2, source: '<%' } }), scanner.tokens[0]
|
31
|
-
assert_attributes ({ type: :indicator, loc: { begin_pos: 2, end_pos: 3, source: '#' } }), scanner.tokens[1]
|
32
|
-
assert_attributes ({ type: :code, loc: { begin_pos: 3, end_pos: 14, source: ' statement ' } }), scanner.tokens[2]
|
33
|
-
assert_attributes ({ type: :erb_end, loc: { begin_pos: 14, end_pos: 16, source: '%>' } }), scanner.tokens[3]
|
34
|
-
end
|
35
|
-
|
36
|
-
test "when multi byte characters are present in erb" do
|
37
|
-
code = "<% ui_helper 'your store’s' %>"
|
38
|
-
scanner = HtmlErb.new(buffer(code))
|
39
|
-
assert_equal 3, scanner.tokens.size
|
40
|
-
|
41
|
-
assert_attributes ({ type: :erb_begin, loc: { begin_pos: 0, end_pos: 2, source: '<%' } }), scanner.tokens[0]
|
42
|
-
assert_attributes ({ type: :code, loc: { begin_pos: 2, end_pos: 28, source: " ui_helper 'your store’s' " } }), scanner.tokens[1]
|
43
|
-
assert_attributes ({ type: :erb_end, loc: { begin_pos: 28, end_pos: 30, source: '%>' } }), scanner.tokens[2]
|
44
|
-
assert_equal code.length, scanner.current_position
|
45
|
-
end
|
46
|
-
|
47
|
-
test "when multi byte characters are present in text" do
|
48
|
-
code = "your store’s"
|
49
|
-
scanner = HtmlErb.new(buffer(code))
|
50
|
-
assert_equal 1, scanner.tokens.size
|
51
|
-
|
52
|
-
assert_attributes ({ type: :text, loc: { begin_pos: 0, end_pos: 12, source: 'your store’s' } }), scanner.tokens[0]
|
53
|
-
assert_equal code.length, scanner.current_position
|
54
|
-
end
|
55
|
-
|
56
|
-
test "when multi byte characters are present in html" do
|
57
|
-
code = "<div title='your store’s'>foo</div>"
|
58
|
-
scanner = HtmlErb.new(buffer(code))
|
59
|
-
assert_equal 14, scanner.tokens.size
|
60
|
-
|
61
|
-
assert_attributes ({ type: :tag_start, loc: { begin_pos: 0, end_pos: 1, source: '<' } }), scanner.tokens[0]
|
62
|
-
assert_attributes ({ type: :tag_name, loc: { begin_pos: 1, end_pos: 4, source: "div" } }), scanner.tokens[1]
|
63
|
-
assert_attributes ({ type: :whitespace, loc: { begin_pos: 4, end_pos: 5, source: " " } }), scanner.tokens[2]
|
64
|
-
assert_attributes ({ type: :attribute_name, loc: { begin_pos: 5, end_pos: 10, source: "title" } }), scanner.tokens[3]
|
65
|
-
assert_attributes ({ type: :equal, loc: { begin_pos: 10, end_pos: 11, source: "=" } }), scanner.tokens[4]
|
66
|
-
assert_attributes ({ type: :attribute_quoted_value_start, loc: { begin_pos: 11, end_pos: 12, source: "'" } }), scanner.tokens[5]
|
67
|
-
assert_attributes ({ type: :attribute_quoted_value, loc: { begin_pos: 12, end_pos: 24, source: "your store’s" } }), scanner.tokens[6]
|
68
|
-
assert_attributes ({ type: :attribute_quoted_value_end, loc: { begin_pos: 24, end_pos: 25, source: "'" } }), scanner.tokens[7]
|
69
|
-
assert_equal code.length, scanner.current_position
|
70
|
-
end
|
71
|
-
|
72
|
-
test "expression literal" do
|
73
|
-
scanner = HtmlErb.new(buffer("<%= literal %>"))
|
74
|
-
assert_equal 4, scanner.tokens.size
|
75
|
-
|
76
|
-
assert_attributes ({ type: :erb_begin, loc: { begin_pos: 0, end_pos: 2, source: '<%' } }), scanner.tokens[0]
|
77
|
-
assert_attributes ({ type: :indicator, loc: { begin_pos: 2, end_pos: 3, source: '=' } }), scanner.tokens[1]
|
78
|
-
assert_attributes ({ type: :code, loc: { begin_pos: 3, end_pos: 12, source: ' literal ' } }), scanner.tokens[2]
|
79
|
-
assert_attributes ({ type: :erb_end, loc: { begin_pos: 12, end_pos: 14, source: '%>' } }), scanner.tokens[3]
|
80
|
-
end
|
81
|
-
|
82
|
-
test "expression escaped" do
|
83
|
-
scanner = HtmlErb.new(buffer("<%== escaped %>"))
|
84
|
-
assert_equal 4, scanner.tokens.size
|
85
|
-
|
86
|
-
assert_attributes ({ type: :erb_begin, loc: { begin_pos: 0, end_pos: 2, source: '<%' } }), scanner.tokens[0]
|
87
|
-
assert_attributes ({ type: :indicator, loc: { begin_pos: 2, end_pos: 4, source: '==' } }), scanner.tokens[1]
|
88
|
-
assert_attributes ({ type: :code, loc: { begin_pos: 4, end_pos: 13, source: ' escaped ' } }), scanner.tokens[2]
|
89
|
-
assert_attributes ({ type: :erb_end, loc: { begin_pos: 13, end_pos: 15, source: '%>' } }), scanner.tokens[3]
|
90
|
-
end
|
91
|
-
|
92
|
-
test "line number for multi-line statements" do
|
93
|
-
scanner = HtmlErb.new(buffer("before <% multi\nline %> after"))
|
94
|
-
assert_equal 5, scanner.tokens.size
|
95
|
-
|
96
|
-
assert_attributes ({ type: :text, loc: { line: 1, source: 'before ' } }), scanner.tokens[0]
|
97
|
-
assert_attributes ({ type: :erb_begin, loc: { line: 1, source: '<%' } }), scanner.tokens[1]
|
98
|
-
assert_attributes ({ type: :code, loc: { line: 1, start_line: 1, stop_line: 2, source: " multi\nline " } }), scanner.tokens[2]
|
99
|
-
assert_attributes ({ type: :erb_end, loc: { line: 2, source: "%>" } }), scanner.tokens[3]
|
100
|
-
assert_attributes ({ type: :text, loc: { line: 2, source: " after" } }), scanner.tokens[4]
|
101
|
-
end
|
102
|
-
|
103
|
-
test "multi-line statements with trim" do
|
104
|
-
scanner = HtmlErb.new(buffer("before\n<% multi\nline -%>\nafter"))
|
105
|
-
assert_equal 7, scanner.tokens.size
|
106
|
-
|
107
|
-
assert_attributes ({ type: :text, loc: { line: 1, source: "before\n" } }), scanner.tokens[0]
|
108
|
-
assert_attributes ({ type: :erb_begin, loc: { line: 2, source: '<%' } }), scanner.tokens[1]
|
109
|
-
assert_attributes ({ type: :code, loc: { line: 2, source: " multi\nline " } }), scanner.tokens[2]
|
110
|
-
assert_attributes ({ type: :trim, loc: { line: 3, source: "-" } }), scanner.tokens[3]
|
111
|
-
assert_attributes ({ type: :erb_end, loc: { line: 3, source: "%>" } }), scanner.tokens[4]
|
112
|
-
assert_attributes ({ type: :text, loc: { line: 3, source: "\n" } }), scanner.tokens[5]
|
113
|
-
assert_attributes ({ type: :text, loc: { line: 4, source: "after" } }), scanner.tokens[6]
|
114
|
-
end
|
115
|
-
|
116
|
-
test "right trim with =%>" do
|
117
|
-
scanner = HtmlErb.new(buffer("<% trim =%>"))
|
118
|
-
assert_equal 4, scanner.tokens.size
|
119
|
-
|
120
|
-
assert_attributes ({ type: :erb_begin, loc: { line: 1, source: '<%' } }), scanner.tokens[0]
|
121
|
-
assert_attributes ({ type: :code, loc: { line: 1, source: " trim " } }), scanner.tokens[1]
|
122
|
-
assert_attributes ({ type: :trim, loc: { line: 1, source: "=" } }), scanner.tokens[2]
|
123
|
-
assert_attributes ({ type: :erb_end, loc: { line: 1, source: "%>" } }), scanner.tokens[3]
|
124
|
-
end
|
125
|
-
|
126
|
-
test "multi-line expression with trim" do
|
127
|
-
scanner = HtmlErb.new(buffer("before\n<%= multi\nline -%>\nafter"))
|
128
|
-
assert_equal 8, scanner.tokens.size
|
129
|
-
|
130
|
-
assert_attributes ({ type: :text, loc: { line: 1, source: "before\n" } }), scanner.tokens[0]
|
131
|
-
assert_attributes ({ type: :erb_begin, loc: { line: 2, source: '<%' } }), scanner.tokens[1]
|
132
|
-
assert_attributes ({ type: :indicator, loc: { line: 2, source: '=' } }), scanner.tokens[2]
|
133
|
-
assert_attributes ({ type: :code, loc: { line: 2, source: " multi\nline " } }), scanner.tokens[3]
|
134
|
-
assert_attributes ({ type: :trim, loc: { line: 3, source: "-" } }), scanner.tokens[4]
|
135
|
-
assert_attributes ({ type: :erb_end, loc: { line: 3, source: "%>" } }), scanner.tokens[5]
|
136
|
-
assert_attributes ({ type: :text, loc: { line: 3, source: "\n" } }), scanner.tokens[6]
|
137
|
-
assert_attributes ({ type: :text, loc: { line: 4, source: "after" } }), scanner.tokens[7]
|
138
|
-
end
|
139
|
-
|
140
|
-
test "line counts with comments" do
|
141
|
-
scanner = HtmlErb.new(buffer("before\n<%# BO$$ Mode %>\nafter"))
|
142
|
-
assert_equal 7, scanner.tokens.size
|
143
|
-
|
144
|
-
assert_attributes ({ type: :text, loc: { line: 1, source: "before\n" } }), scanner.tokens[0]
|
145
|
-
assert_attributes ({ type: :erb_begin, loc: { line: 2, source: '<%' } }), scanner.tokens[1]
|
146
|
-
assert_attributes ({ type: :indicator, loc: { line: 2, source: '#' } }), scanner.tokens[2]
|
147
|
-
assert_attributes ({ type: :code, loc: { line: 2, source: " BO$$ Mode " } }), scanner.tokens[3]
|
148
|
-
assert_attributes ({ type: :erb_end, loc: { line: 2, source: "%>" } }), scanner.tokens[4]
|
149
|
-
assert_attributes ({ type: :text, loc: { line: 2, source: "\n" } }), scanner.tokens[5]
|
150
|
-
assert_attributes ({ type: :text, loc: { line: 3, source: "after" } }), scanner.tokens[6]
|
151
|
-
end
|
152
|
-
|
153
|
-
test "escaped opening ERB tag <%%" do
|
154
|
-
scanner = HtmlErb.new(buffer("just some <%%= text %> no erb"))
|
155
|
-
assert_equal 6, scanner.tokens.size
|
156
|
-
|
157
|
-
assert_attributes ({ type: :text, loc: { line: 1, source: "just some " } }), scanner.tokens[0]
|
158
|
-
assert_attributes ({ type: :erb_begin, loc: { line: 1, source: '<%' } }), scanner.tokens[1]
|
159
|
-
assert_attributes ({ type: :indicator, loc: { line: 1, source: "%" } }), scanner.tokens[2]
|
160
|
-
assert_attributes ({ type: :code, loc: { line: 1, source: "= text " } }), scanner.tokens[3]
|
161
|
-
assert_attributes ({ type: :erb_end, loc: { line: 1, source: "%>" } }), scanner.tokens[4]
|
162
|
-
assert_attributes ({ type: :text, loc: { line: 1, source: " no erb" } }), scanner.tokens[5]
|
163
|
-
end
|
164
|
-
|
165
|
-
private
|
166
|
-
|
167
|
-
def assert_attributes(attributes, token)
|
168
|
-
attributes.each do |key, value|
|
169
|
-
if value.nil?
|
170
|
-
assert_nil token.send(key)
|
171
|
-
elsif value.is_a?(Hash)
|
172
|
-
assert_attributes(value, token.send(key))
|
173
|
-
else
|
174
|
-
assert_equal value, token.send(key)
|
175
|
-
end
|
176
|
-
end
|
177
|
-
end
|
178
|
-
end
|
179
|
-
end
|
180
|
-
end
|
@@ -1,98 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'better_html/tokenizer/html_lodash'
|
3
|
-
|
4
|
-
module BetterHtml
|
5
|
-
module Tokenizer
|
6
|
-
class HtmlLodashTest < ActiveSupport::TestCase
|
7
|
-
test "matches text" do
|
8
|
-
scanner = HtmlLodash.new(buffer("just some text"))
|
9
|
-
assert_equal 1, scanner.tokens.size
|
10
|
-
|
11
|
-
assert_attributes ({ type: :text, loc: { begin_pos: 0, end_pos: 14, source: "just some text" } }), scanner.tokens[0]
|
12
|
-
end
|
13
|
-
|
14
|
-
test "matches strings to be escaped" do
|
15
|
-
scanner = HtmlLodash.new(buffer("[%= foo %]"))
|
16
|
-
assert_equal 4, scanner.tokens.size
|
17
|
-
|
18
|
-
assert_attributes ({ type: :lodash_begin, loc: { begin_pos: 0, end_pos: 2, source: "[%" } }), scanner.tokens[0]
|
19
|
-
assert_attributes ({ type: :indicator, loc: { begin_pos: 2, end_pos: 3, source: "=" } }), scanner.tokens[1]
|
20
|
-
assert_attributes ({ type: :code, loc: { begin_pos: 3, end_pos: 8, source: " foo " } }), scanner.tokens[2]
|
21
|
-
assert_attributes ({ type: :lodash_end, loc: { begin_pos: 8, end_pos: 10, source: "%]" } }), scanner.tokens[3]
|
22
|
-
end
|
23
|
-
|
24
|
-
test "matches interpolate" do
|
25
|
-
scanner = HtmlLodash.new(buffer("[%! foo %]"))
|
26
|
-
assert_equal 4, scanner.tokens.size
|
27
|
-
|
28
|
-
assert_attributes ({ type: :lodash_begin, loc: { begin_pos: 0, end_pos: 2, source: "[%" } }), scanner.tokens[0]
|
29
|
-
assert_attributes ({ type: :indicator, loc: { begin_pos: 2, end_pos: 3, source: "!" } }), scanner.tokens[1]
|
30
|
-
assert_attributes ({ type: :code, loc: { begin_pos: 3, end_pos: 8, source: " foo " } }), scanner.tokens[2]
|
31
|
-
assert_attributes ({ type: :lodash_end, loc: { begin_pos: 8, end_pos: 10, source: "%]" } }), scanner.tokens[3]
|
32
|
-
end
|
33
|
-
|
34
|
-
test "matches statement" do
|
35
|
-
scanner = HtmlLodash.new(buffer("[% foo %]"))
|
36
|
-
assert_equal 3, scanner.tokens.size
|
37
|
-
|
38
|
-
assert_attributes ({ type: :lodash_begin, loc: { begin_pos: 0, end_pos: 2, source: "[%" } }), scanner.tokens[0]
|
39
|
-
assert_attributes ({ type: :code, loc: { begin_pos: 2, end_pos: 7, source: " foo " } }), scanner.tokens[1]
|
40
|
-
assert_attributes ({ type: :lodash_end, loc: { begin_pos: 7, end_pos: 9, source: "%]" } }), scanner.tokens[2]
|
41
|
-
end
|
42
|
-
|
43
|
-
test "matches text before and after" do
|
44
|
-
scanner = HtmlLodash.new(buffer("before\n[%= foo %]\nafter"))
|
45
|
-
assert_equal 6, scanner.tokens.size
|
46
|
-
|
47
|
-
assert_attributes ({ type: :text, loc: { begin_pos: 0, end_pos: 7, source: "before\n" } }), scanner.tokens[0]
|
48
|
-
assert_attributes ({ type: :lodash_begin, loc: { begin_pos: 7, end_pos: 9, source: "[%" } }), scanner.tokens[1]
|
49
|
-
assert_attributes ({ type: :indicator, loc: { begin_pos: 9, end_pos: 10, source: "=" } }), scanner.tokens[2]
|
50
|
-
assert_attributes ({ type: :code, loc: { begin_pos: 10, end_pos: 15, source: " foo " } }), scanner.tokens[3]
|
51
|
-
assert_attributes ({ type: :lodash_end, loc: { begin_pos: 15, end_pos: 17, source: "%]" } }), scanner.tokens[4]
|
52
|
-
assert_attributes ({ type: :text, loc: { begin_pos: 17, end_pos: 23, source: "\nafter" } }), scanner.tokens[5]
|
53
|
-
end
|
54
|
-
|
55
|
-
test "matches multiple" do
|
56
|
-
scanner = HtmlLodash.new(buffer("[% if() { %][%= foo %][% } %]"))
|
57
|
-
assert_equal 10, scanner.tokens.size
|
58
|
-
|
59
|
-
assert_attributes ({ type: :lodash_begin, loc: { source: "[%" } }), scanner.tokens[0]
|
60
|
-
assert_attributes ({ type: :code, loc: { source: " if() { " } }), scanner.tokens[1]
|
61
|
-
assert_attributes ({ type: :lodash_end, loc: { source: "%]" } }), scanner.tokens[2]
|
62
|
-
|
63
|
-
assert_attributes ({ type: :lodash_begin, loc: { source: "[%" } }), scanner.tokens[3]
|
64
|
-
assert_attributes ({ type: :indicator, loc: { source: "=" } }), scanner.tokens[4]
|
65
|
-
assert_attributes ({ type: :code, loc: { source: " foo " } }), scanner.tokens[5]
|
66
|
-
assert_attributes ({ type: :lodash_end, loc: { source: "%]" } }), scanner.tokens[6]
|
67
|
-
|
68
|
-
assert_attributes ({ type: :lodash_begin, loc: { source: "[%" } }), scanner.tokens[7]
|
69
|
-
assert_attributes ({ type: :code, loc: { source: " } " } }), scanner.tokens[8]
|
70
|
-
assert_attributes ({ type: :lodash_end, loc: { source: "%]" } }), scanner.tokens[9]
|
71
|
-
end
|
72
|
-
|
73
|
-
test "parses out html correctly" do
|
74
|
-
scanner = HtmlLodash.new(buffer('<div class="[%= foo %]">'))
|
75
|
-
assert_equal 12, scanner.tokens.size
|
76
|
-
assert_equal [:tag_start, :tag_name, :whitespace, :attribute_name,
|
77
|
-
:equal, :attribute_quoted_value_start,
|
78
|
-
:lodash_begin, :indicator, :code, :lodash_end,
|
79
|
-
:attribute_quoted_value_end, :tag_end], scanner.tokens.map(&:type)
|
80
|
-
assert_equal ["<", "div", " ", "class", "=", "\"", "[%", "=", " foo ", "%]", "\"", ">"], scanner.tokens.map(&:loc).map(&:source)
|
81
|
-
end
|
82
|
-
|
83
|
-
private
|
84
|
-
|
85
|
-
def assert_attributes(attributes, token)
|
86
|
-
attributes.each do |key, value|
|
87
|
-
if value.nil?
|
88
|
-
assert_nil token.send(key)
|
89
|
-
elsif value.is_a?(Hash)
|
90
|
-
assert_attributes(value, token.send(key))
|
91
|
-
else
|
92
|
-
assert_equal value, token.send(key)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
@@ -1,75 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'better_html/tokenizer/location'
|
3
|
-
|
4
|
-
module BetterHtml
|
5
|
-
module Tokenizer
|
6
|
-
class LocationTest < ActiveSupport::TestCase
|
7
|
-
test "location start out of bounds" do
|
8
|
-
e = assert_raises(ArgumentError) do
|
9
|
-
Location.new(buffer("foo"), 5, 6)
|
10
|
-
end
|
11
|
-
assert_equal "begin_pos location 5 is out of range for document of size 3", e.message
|
12
|
-
end
|
13
|
-
|
14
|
-
test "location stop out of bounds" do
|
15
|
-
e = assert_raises(ArgumentError) do
|
16
|
-
Location.new(buffer("foo"), 2, 6)
|
17
|
-
end
|
18
|
-
assert_equal "end_pos location 6 is out of range for document of size 3", e.message
|
19
|
-
end
|
20
|
-
|
21
|
-
test "location stop < start" do
|
22
|
-
e = assert_raises(ArgumentError) do
|
23
|
-
Location.new(buffer("aaaaaa"), 5, 2)
|
24
|
-
end
|
25
|
-
assert_equal "Parser::Source::Range: end_pos must not be less than begin_pos", e.message
|
26
|
-
end
|
27
|
-
|
28
|
-
test "location stop == start" do
|
29
|
-
loc = Location.new(buffer("aaaaaa"), 5, 5)
|
30
|
-
assert_equal "", loc.source
|
31
|
-
assert_equal 0, loc.size
|
32
|
-
end
|
33
|
-
|
34
|
-
test "end_pos is stop+1" do
|
35
|
-
loc = Location.new(buffer("aaaaaa"), 5, 5)
|
36
|
-
assert_equal 5, loc.end_pos
|
37
|
-
end
|
38
|
-
|
39
|
-
test "range is exclusive of last char" do
|
40
|
-
loc = Location.new(buffer("aaaaaa"), 5, 5)
|
41
|
-
assert_equal 5...5, loc.range
|
42
|
-
end
|
43
|
-
|
44
|
-
test "location calulates start and stop line and column" do
|
45
|
-
loc = Location.new(buffer("foo\nbar\nbaz"), 5, 10)
|
46
|
-
|
47
|
-
assert_equal "ar\nba", loc.source
|
48
|
-
assert_equal 2, loc.start_line
|
49
|
-
assert_equal 1, loc.start_column
|
50
|
-
assert_equal 3, loc.stop_line
|
51
|
-
assert_equal 2, loc.stop_column
|
52
|
-
end
|
53
|
-
|
54
|
-
test "line_source_with_underline" do
|
55
|
-
loc = Location.new(buffer("ui_helper(foo)"), 10, 13)
|
56
|
-
|
57
|
-
assert_equal "foo", loc.source
|
58
|
-
assert_equal <<~EOL.strip, loc.line_source_with_underline
|
59
|
-
ui_helper(foo)
|
60
|
-
^^^
|
61
|
-
EOL
|
62
|
-
end
|
63
|
-
|
64
|
-
test "line_source_with_underline removes empty spaces" do
|
65
|
-
loc = Location.new(buffer(" \t ui_helper(foo)"), 17, 20)
|
66
|
-
|
67
|
-
assert_equal "foo", loc.source
|
68
|
-
assert_equal <<~EOL.strip, loc.line_source_with_underline
|
69
|
-
ui_helper(foo)
|
70
|
-
^^^
|
71
|
-
EOL
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
@@ -1,145 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'better_html/tokenizer/token'
|
3
|
-
require 'better_html/tokenizer/location'
|
4
|
-
require 'better_html/tokenizer/token_array'
|
5
|
-
|
6
|
-
module BetterHtml
|
7
|
-
module Tokenizer
|
8
|
-
class TokenArrayTest < ActiveSupport::TestCase
|
9
|
-
setup do
|
10
|
-
@document = "<x>"
|
11
|
-
@buffer = buffer(@document)
|
12
|
-
@tokens = [
|
13
|
-
Token.new(type: :lquote, loc: Location.new(@buffer, 0, 0)),
|
14
|
-
Token.new(type: :name, loc: Location.new(@buffer, 1, 1)),
|
15
|
-
Token.new(type: :rquote, loc: Location.new(@buffer, 2, 2)),
|
16
|
-
]
|
17
|
-
@array = TokenArray.new(@tokens)
|
18
|
-
end
|
19
|
-
|
20
|
-
test "size" do
|
21
|
-
assert_equal 3, @array.size
|
22
|
-
end
|
23
|
-
|
24
|
-
test "size for empty array" do
|
25
|
-
@array = TokenArray.new([])
|
26
|
-
assert_equal 0, @array.size
|
27
|
-
end
|
28
|
-
|
29
|
-
test "shift returns element from beginning of array" do
|
30
|
-
assert_equal @tokens.first, @array.shift
|
31
|
-
end
|
32
|
-
|
33
|
-
test "shift decreases the size by 1" do
|
34
|
-
assert_difference(-> { @array.size }, -1) do
|
35
|
-
@array.shift
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
test "after shift, current element is changed" do
|
40
|
-
assert_equal @tokens.first, @array.current
|
41
|
-
assert_equal @tokens.first, @array.shift
|
42
|
-
assert_equal @tokens.second, @array.current
|
43
|
-
end
|
44
|
-
|
45
|
-
test "pop returns element from the end of array" do
|
46
|
-
assert_equal @tokens.last, @array.pop
|
47
|
-
end
|
48
|
-
|
49
|
-
test "pop decreases the size by 1" do
|
50
|
-
assert_difference(-> { @array.size }, -1) do
|
51
|
-
@array.pop
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
test "after pop, last element is changed" do
|
56
|
-
assert_equal @tokens.last, @array.last
|
57
|
-
assert_equal @tokens.last, @array.pop
|
58
|
-
assert_equal @tokens.second, @array.last
|
59
|
-
end
|
60
|
-
|
61
|
-
test "current returns element from beginning of array" do
|
62
|
-
assert_equal @tokens.first, @array.current
|
63
|
-
end
|
64
|
-
|
65
|
-
test "current does not change size" do
|
66
|
-
assert_no_difference(-> { @array.size }) do
|
67
|
-
@array.current
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
test "last returns element from the end of array" do
|
72
|
-
assert_equal @tokens.last, @array.last
|
73
|
-
end
|
74
|
-
|
75
|
-
test "last does not change size" do
|
76
|
-
assert_no_difference(-> { @array.size }) do
|
77
|
-
@array.last
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
test "empty?" do
|
82
|
-
refute_predicate @array, :empty?
|
83
|
-
3.times { @array.shift }
|
84
|
-
assert_predicate @array, :empty?
|
85
|
-
end
|
86
|
-
|
87
|
-
test "any?" do
|
88
|
-
assert_predicate @array, :any?
|
89
|
-
3.times { @array.shift }
|
90
|
-
refute_predicate @array, :any?
|
91
|
-
end
|
92
|
-
|
93
|
-
test "current is nil when array is empty" do
|
94
|
-
@array = TokenArray.new([])
|
95
|
-
assert_nil @array.current
|
96
|
-
end
|
97
|
-
|
98
|
-
test "last is nil when array is empty" do
|
99
|
-
@array = TokenArray.new([])
|
100
|
-
assert_nil @array.last
|
101
|
-
end
|
102
|
-
|
103
|
-
test "shift raises for empty array" do
|
104
|
-
@array = TokenArray.new([])
|
105
|
-
e = assert_raises(RuntimeError) do
|
106
|
-
@array.shift
|
107
|
-
end
|
108
|
-
assert_equal 'no tokens left to shift', e.message
|
109
|
-
end
|
110
|
-
|
111
|
-
test "pop raises for empty array" do
|
112
|
-
@array = TokenArray.new([])
|
113
|
-
e = assert_raises(RuntimeError) do
|
114
|
-
@array.pop
|
115
|
-
end
|
116
|
-
assert_equal 'no tokens left to pop', e.message
|
117
|
-
end
|
118
|
-
|
119
|
-
test "trim takes elements from the beginning of array" do
|
120
|
-
assert_difference(-> { @array.size }, -1) do
|
121
|
-
@array.trim(:lquote)
|
122
|
-
end
|
123
|
-
|
124
|
-
assert_equal @tokens.second, @array.current
|
125
|
-
end
|
126
|
-
|
127
|
-
test "trim takes elements from the end of array" do
|
128
|
-
assert_difference(-> { @array.size }, -1) do
|
129
|
-
@array.trim(:rquote)
|
130
|
-
end
|
131
|
-
|
132
|
-
assert_equal @tokens.second, @array.last
|
133
|
-
end
|
134
|
-
|
135
|
-
test "trim does not change array when element types are not at front nor back of array" do
|
136
|
-
assert_no_difference(-> { @array.size }) do
|
137
|
-
@array.trim(:name)
|
138
|
-
end
|
139
|
-
|
140
|
-
assert_equal @tokens.first, @array.current
|
141
|
-
assert_equal @tokens.last, @array.last
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'better_html/tokenizer/location'
|
3
|
-
require 'better_html/tokenizer/token'
|
4
|
-
|
5
|
-
module BetterHtml
|
6
|
-
module Tokenizer
|
7
|
-
class TokenTest < ActiveSupport::TestCase
|
8
|
-
test "token inspect" do
|
9
|
-
loc = Location.new(buffer("foo"), 0, 3)
|
10
|
-
token = Token.new(type: :foo, loc: loc)
|
11
|
-
assert_equal "t(:foo, \"foo\")", token.inspect
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
data/test/dummy/README.rdoc
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
== README
|
2
|
-
|
3
|
-
This README would normally document whatever steps are necessary to get the
|
4
|
-
application up and running.
|
5
|
-
|
6
|
-
Things you may want to cover:
|
7
|
-
|
8
|
-
* Ruby version
|
9
|
-
|
10
|
-
* System dependencies
|
11
|
-
|
12
|
-
* Configuration
|
13
|
-
|
14
|
-
* Database creation
|
15
|
-
|
16
|
-
* Database initialization
|
17
|
-
|
18
|
-
* How to run the test suite
|
19
|
-
|
20
|
-
* Services (job queues, cache servers, search engines, etc.)
|
21
|
-
|
22
|
-
* Deployment instructions
|
23
|
-
|
24
|
-
* ...
|
25
|
-
|
26
|
-
|
27
|
-
Please feel free to use a different markup language if you do not plan to run
|
28
|
-
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
-
// listed below.
|
3
|
-
//
|
4
|
-
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
-
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
-
//
|
7
|
-
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
-
// compiled file.
|
9
|
-
//
|
10
|
-
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
-
// about supported directives.
|
12
|
-
//
|
13
|
-
//= require_tree .
|
@@ -1,15 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
-
* listed below.
|
4
|
-
*
|
5
|
-
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
-
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
-
*
|
8
|
-
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
-
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
-
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
-
* file per style scope.
|
12
|
-
*
|
13
|
-
*= require_tree .
|
14
|
-
*= require_self
|
15
|
-
*/
|
@@ -1,14 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
<html>
|
3
|
-
<head>
|
4
|
-
<title>Dummy</title>
|
5
|
-
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
6
|
-
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
7
|
-
<%= csrf_meta_tags %>
|
8
|
-
</head>
|
9
|
-
<body>
|
10
|
-
|
11
|
-
<%= yield %>
|
12
|
-
|
13
|
-
</body>
|
14
|
-
</html>
|
data/test/dummy/bin/bundle
DELETED
data/test/dummy/bin/rails
DELETED
data/test/dummy/bin/rake
DELETED
data/test/dummy/bin/setup
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
require 'pathname'
|
3
|
-
|
4
|
-
# path to your application root.
|
5
|
-
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
6
|
-
|
7
|
-
Dir.chdir APP_ROOT do
|
8
|
-
# This script is a starting point to setup your application.
|
9
|
-
# Add necessary setup steps to this file:
|
10
|
-
|
11
|
-
puts "== Installing dependencies =="
|
12
|
-
system "gem install bundler --conservative"
|
13
|
-
system "bundle check || bundle install"
|
14
|
-
|
15
|
-
# puts "\n== Copying sample files =="
|
16
|
-
# unless File.exist?("config/database.yml")
|
17
|
-
# system "cp config/database.yml.sample config/database.yml"
|
18
|
-
# end
|
19
|
-
|
20
|
-
puts "\n== Preparing database =="
|
21
|
-
system "bin/rake db:setup"
|
22
|
-
|
23
|
-
puts "\n== Removing old logs and tempfiles =="
|
24
|
-
system "rm -f log/*"
|
25
|
-
system "rm -rf tmp/cache"
|
26
|
-
|
27
|
-
puts "\n== Restarting application server =="
|
28
|
-
system "touch tmp/restart.txt"
|
29
|
-
end
|