better_html 0.0.12 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/better_html.rb +0 -2
- data/lib/better_html/ast/iterator.rb +32 -0
- data/lib/better_html/ast/node.rb +14 -0
- data/lib/better_html/better_erb/runtime_checks.rb +3 -3
- data/lib/better_html/config.rb +12 -0
- data/lib/better_html/parser.rb +286 -0
- data/lib/better_html/test_helper/ruby_expr.rb +8 -5
- data/lib/better_html/test_helper/safe_erb_tester.rb +121 -108
- data/lib/better_html/test_helper/safe_lodash_tester.rb +44 -42
- data/lib/better_html/tokenizer/base_erb.rb +79 -0
- data/lib/better_html/tokenizer/html_erb.rb +31 -0
- data/lib/better_html/{node_iterator → tokenizer}/html_lodash.rb +30 -34
- data/lib/better_html/tokenizer/javascript_erb.rb +15 -0
- data/lib/better_html/{node_iterator → tokenizer}/location.rb +9 -3
- data/lib/better_html/tokenizer/token.rb +16 -0
- data/lib/better_html/tokenizer/token_array.rb +54 -0
- data/lib/better_html/tree/attribute.rb +31 -0
- data/lib/better_html/tree/attributes_list.rb +25 -0
- data/lib/better_html/tree/tag.rb +39 -0
- data/lib/better_html/version.rb +1 -1
- data/test/better_html/parser_test.rb +279 -0
- data/test/better_html/test_helper/safe_erb_tester_test.rb +11 -0
- data/test/better_html/test_helper/safe_lodash_tester_test.rb +11 -1
- data/test/better_html/tokenizer/html_erb_test.rb +158 -0
- data/test/better_html/tokenizer/html_lodash_test.rb +98 -0
- data/test/better_html/tokenizer/location_test.rb +57 -0
- data/test/better_html/tokenizer/token_array_test.rb +144 -0
- data/test/better_html/tokenizer/token_test.rb +15 -0
- metadata +45 -30
- data/lib/better_html/node_iterator.rb +0 -144
- data/lib/better_html/node_iterator/attribute.rb +0 -34
- data/lib/better_html/node_iterator/base.rb +0 -27
- data/lib/better_html/node_iterator/cdata.rb +0 -8
- data/lib/better_html/node_iterator/comment.rb +0 -8
- data/lib/better_html/node_iterator/content_node.rb +0 -13
- data/lib/better_html/node_iterator/element.rb +0 -26
- data/lib/better_html/node_iterator/html_erb.rb +0 -70
- data/lib/better_html/node_iterator/javascript_erb.rb +0 -55
- data/lib/better_html/node_iterator/text.rb +0 -8
- data/lib/better_html/node_iterator/token.rb +0 -8
- data/lib/better_html/tree.rb +0 -113
- data/test/better_html/node_iterator/html_erb_test.rb +0 -116
- data/test/better_html/node_iterator/html_lodash_test.rb +0 -132
- data/test/better_html/node_iterator/location_test.rb +0 -36
- data/test/better_html/node_iterator_test.rb +0 -221
- data/test/better_html/tree_test.rb +0 -110
@@ -1,116 +0,0 @@
|
|
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
|
-
test "line counts with comments" do
|
87
|
-
scanner = BetterHtml::NodeIterator::HtmlErb.new("before\n<%# BO$$ Mode %>\nafter")
|
88
|
-
assert_equal 4, scanner.tokens.size
|
89
|
-
|
90
|
-
assert_attributes ({ type: :text, text: "before\n" }), scanner.tokens[0]
|
91
|
-
assert_attributes ({ line: 1 }), scanner.tokens[0].location
|
92
|
-
|
93
|
-
assert_attributes ({ type: :stmt, text: "<%# BO$$ Mode %>" }), scanner.tokens[1]
|
94
|
-
assert_attributes ({ line: 2 }), scanner.tokens[1].location
|
95
|
-
|
96
|
-
assert_attributes ({ type: :text, text: "\n" }), scanner.tokens[2]
|
97
|
-
assert_attributes ({ line: 2 }), scanner.tokens[2].location
|
98
|
-
|
99
|
-
assert_attributes ({ type: :text, text: "after" }), scanner.tokens[3]
|
100
|
-
assert_attributes ({ line: 3 }), scanner.tokens[3].location
|
101
|
-
end
|
102
|
-
|
103
|
-
private
|
104
|
-
|
105
|
-
def assert_attributes(attributes, token)
|
106
|
-
attributes.each do |key, value|
|
107
|
-
if value.nil?
|
108
|
-
assert_nil token.send(key)
|
109
|
-
else
|
110
|
-
assert_equal value, token.send(key)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module BetterHtml
|
4
|
-
class NodeIterator
|
5
|
-
class HtmlLodashTest < ActiveSupport::TestCase
|
6
|
-
test "matches text" do
|
7
|
-
scanner = BetterHtml::NodeIterator::HtmlLodash.new("just some text")
|
8
|
-
assert_equal 1, scanner.tokens.size
|
9
|
-
token = scanner.tokens[0]
|
10
|
-
assert_equal :text, token.type
|
11
|
-
assert_equal "just some text", token.text
|
12
|
-
assert_nil token.code
|
13
|
-
assert_equal 0, token.location.start
|
14
|
-
assert_equal 14, token.location.stop
|
15
|
-
assert_equal 1, token.location.line
|
16
|
-
assert_equal 0, token.location.column
|
17
|
-
end
|
18
|
-
|
19
|
-
test "matches strings to be escaped" do
|
20
|
-
scanner = BetterHtml::NodeIterator::HtmlLodash.new("[%= foo %]")
|
21
|
-
assert_equal 1, scanner.tokens.size
|
22
|
-
token = scanner.tokens[0]
|
23
|
-
assert_equal :expr_literal, token.type
|
24
|
-
assert_equal "[%= foo %]", token.text
|
25
|
-
assert_equal " foo ", token.code
|
26
|
-
assert_equal 0, token.location.start
|
27
|
-
assert_equal 10, token.location.stop
|
28
|
-
assert_equal 1, token.location.line
|
29
|
-
assert_equal 0, token.location.column
|
30
|
-
end
|
31
|
-
|
32
|
-
test "matches interpolate" do
|
33
|
-
scanner = BetterHtml::NodeIterator::HtmlLodash.new("[%! foo %]")
|
34
|
-
assert_equal 1, scanner.tokens.size
|
35
|
-
token = scanner.tokens[0]
|
36
|
-
assert_equal :expr_escaped, token.type
|
37
|
-
assert_equal "[%! foo %]", token.text
|
38
|
-
assert_equal " foo ", token.code
|
39
|
-
assert_equal 0, token.location.start
|
40
|
-
assert_equal 10, token.location.stop
|
41
|
-
assert_equal 1, token.location.line
|
42
|
-
assert_equal 0, token.location.column
|
43
|
-
end
|
44
|
-
|
45
|
-
test "matches statement" do
|
46
|
-
scanner = BetterHtml::NodeIterator::HtmlLodash.new("[% foo %]")
|
47
|
-
assert_equal 1, scanner.tokens.size
|
48
|
-
token = scanner.tokens[0]
|
49
|
-
assert_equal :stmt, token.type
|
50
|
-
assert_equal "[% foo %]", token.text
|
51
|
-
assert_equal " foo ", token.code
|
52
|
-
assert_equal 0, token.location.start
|
53
|
-
assert_equal 9, token.location.stop
|
54
|
-
assert_equal 1, token.location.line
|
55
|
-
assert_equal 0, token.location.column
|
56
|
-
end
|
57
|
-
|
58
|
-
test "matches text before and after" do
|
59
|
-
scanner = BetterHtml::NodeIterator::HtmlLodash.new("before\n[%= foo %]\nafter")
|
60
|
-
assert_equal 3, scanner.tokens.size
|
61
|
-
|
62
|
-
token = scanner.tokens[0]
|
63
|
-
assert_equal :text, token.type
|
64
|
-
assert_equal "before\n", token.text
|
65
|
-
assert_nil token.code
|
66
|
-
assert_equal 0, token.location.start
|
67
|
-
assert_equal 7, token.location.stop
|
68
|
-
assert_equal 1, token.location.line
|
69
|
-
assert_equal 0, token.location.column
|
70
|
-
|
71
|
-
token = scanner.tokens[1]
|
72
|
-
assert_equal :expr_literal, token.type
|
73
|
-
assert_equal "[%= foo %]", token.text
|
74
|
-
assert_equal " foo ", token.code
|
75
|
-
assert_equal 7, token.location.start
|
76
|
-
assert_equal 17, token.location.stop
|
77
|
-
assert_equal 2, token.location.line
|
78
|
-
assert_equal 0, token.location.column
|
79
|
-
|
80
|
-
token = scanner.tokens[2]
|
81
|
-
assert_equal :text, token.type
|
82
|
-
assert_equal "\nafter", token.text
|
83
|
-
assert_nil token.code
|
84
|
-
assert_equal 17, token.location.start
|
85
|
-
assert_equal 23, token.location.stop
|
86
|
-
assert_equal 2, token.location.line
|
87
|
-
assert_equal 10, token.location.column
|
88
|
-
end
|
89
|
-
|
90
|
-
test "matches multiple" do
|
91
|
-
scanner = BetterHtml::NodeIterator::HtmlLodash.new("[% if() { %][%= foo %][% } %]")
|
92
|
-
assert_equal 3, scanner.tokens.size
|
93
|
-
|
94
|
-
token = scanner.tokens[0]
|
95
|
-
assert_equal :stmt, token.type
|
96
|
-
assert_equal "[% if() { %]", token.text
|
97
|
-
assert_equal " if() { ", token.code
|
98
|
-
assert_equal 0, token.location.start
|
99
|
-
assert_equal 12, token.location.stop
|
100
|
-
assert_equal 1, token.location.line
|
101
|
-
assert_equal 0, token.location.column
|
102
|
-
|
103
|
-
token = scanner.tokens[1]
|
104
|
-
assert_equal :expr_literal, token.type
|
105
|
-
assert_equal "[%= foo %]", token.text
|
106
|
-
assert_equal " foo ", token.code
|
107
|
-
assert_equal 12, token.location.start
|
108
|
-
assert_equal 22, token.location.stop
|
109
|
-
assert_equal 1, token.location.line
|
110
|
-
assert_equal 12, token.location.column
|
111
|
-
|
112
|
-
token = scanner.tokens[2]
|
113
|
-
assert_equal :stmt, token.type
|
114
|
-
assert_equal "[% } %]", token.text
|
115
|
-
assert_equal " } ", token.code
|
116
|
-
assert_equal 22, token.location.start
|
117
|
-
assert_equal 29, token.location.stop
|
118
|
-
assert_equal 1, token.location.line
|
119
|
-
assert_equal 22, token.location.column
|
120
|
-
end
|
121
|
-
|
122
|
-
test "parses out html correctly" do
|
123
|
-
scanner = BetterHtml::NodeIterator::HtmlLodash.new('<div class="[%= foo %]">')
|
124
|
-
assert_equal 9, scanner.tokens.size
|
125
|
-
assert_equal [:tag_start, :tag_name, :whitespace, :attribute_name,
|
126
|
-
:equal, :attribute_quoted_value_start, :expr_literal,
|
127
|
-
:attribute_quoted_value_end, :tag_end], scanner.tokens.map(&:type)
|
128
|
-
assert_equal ["<", "div", " ", "class", "=", "\"", "[%= foo %]", "\"", ">"], scanner.tokens.map(&:text)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'better_html/test_helper/safe_erb_tester'
|
3
|
-
|
4
|
-
module BetterHtml
|
5
|
-
class NodeIterator
|
6
|
-
class LocationTest < ActiveSupport::TestCase
|
7
|
-
test "location without line and column" do
|
8
|
-
loc = Location.new("foo\nbar\nbaz", 9, 10)
|
9
|
-
|
10
|
-
assert_equal "a", loc.source
|
11
|
-
assert_equal 3, loc.line
|
12
|
-
assert_equal 1, loc.column
|
13
|
-
end
|
14
|
-
|
15
|
-
test "line_source_with_underline" do
|
16
|
-
loc = Location.new("ui_helper(foo)", 10, 13)
|
17
|
-
|
18
|
-
assert_equal "foo", loc.source
|
19
|
-
assert_equal <<~EOL.strip, loc.line_source_with_underline
|
20
|
-
ui_helper(foo)
|
21
|
-
^^^
|
22
|
-
EOL
|
23
|
-
end
|
24
|
-
|
25
|
-
test "line_source_with_underline removes empty spaces" do
|
26
|
-
loc = Location.new(" \t ui_helper(foo)", 17, 20)
|
27
|
-
|
28
|
-
assert_equal "foo", loc.source
|
29
|
-
assert_equal <<~EOL.strip, loc.line_source_with_underline
|
30
|
-
ui_helper(foo)
|
31
|
-
^^^
|
32
|
-
EOL
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,221 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module BetterHtml
|
4
|
-
class NodeIteratorTest < ActiveSupport::TestCase
|
5
|
-
test "consume cdata nodes" do
|
6
|
-
tree = BetterHtml::NodeIterator.new("<![CDATA[ foo ]]>")
|
7
|
-
|
8
|
-
assert_equal 1, tree.nodes.size
|
9
|
-
assert_equal BetterHtml::NodeIterator::CData, tree.nodes.first.class
|
10
|
-
assert_equal [" foo "], tree.nodes.first.content_parts.map(&:text)
|
11
|
-
end
|
12
|
-
|
13
|
-
test "unterminated cdata nodes are consumed until end" do
|
14
|
-
tree = BetterHtml::NodeIterator.new("<![CDATA[ foo")
|
15
|
-
|
16
|
-
assert_equal 1, tree.nodes.size
|
17
|
-
assert_equal BetterHtml::NodeIterator::CData, tree.nodes.first.class
|
18
|
-
assert_equal [" foo"], tree.nodes.first.content_parts.map(&:text)
|
19
|
-
end
|
20
|
-
|
21
|
-
test "consume cdata with interpolation" do
|
22
|
-
tree = BetterHtml::NodeIterator.new("<![CDATA[ foo <%= bar %> baz ]]>")
|
23
|
-
|
24
|
-
assert_equal 1, tree.nodes.size
|
25
|
-
assert_equal BetterHtml::NodeIterator::CData, tree.nodes.first.class
|
26
|
-
assert_equal [" foo ", "<%= bar %>", " baz "], tree.nodes.first.content_parts.map(&:text)
|
27
|
-
end
|
28
|
-
|
29
|
-
test "consume comment nodes" do
|
30
|
-
tree = BetterHtml::NodeIterator.new("<!-- foo -->")
|
31
|
-
|
32
|
-
assert_equal 1, tree.nodes.size
|
33
|
-
assert_equal BetterHtml::NodeIterator::Comment, tree.nodes.first.class
|
34
|
-
assert_equal [" foo "], tree.nodes.first.content_parts.map(&:text)
|
35
|
-
end
|
36
|
-
|
37
|
-
test "unterminated comment nodes are consumed until end" do
|
38
|
-
tree = BetterHtml::NodeIterator.new("<!-- foo")
|
39
|
-
|
40
|
-
assert_equal 1, tree.nodes.size
|
41
|
-
assert_equal BetterHtml::NodeIterator::Comment, tree.nodes.first.class
|
42
|
-
assert_equal [" foo"], tree.nodes.first.content_parts.map(&:text)
|
43
|
-
end
|
44
|
-
|
45
|
-
test "consume comment with interpolation" do
|
46
|
-
tree = BetterHtml::NodeIterator.new("<!-- foo <%= bar %> baz -->")
|
47
|
-
|
48
|
-
assert_equal 1, tree.nodes.size
|
49
|
-
assert_equal BetterHtml::NodeIterator::Comment, tree.nodes.first.class
|
50
|
-
assert_equal [" foo ", "<%= bar %>", " baz "], tree.nodes.first.content_parts.map(&:text)
|
51
|
-
end
|
52
|
-
|
53
|
-
test "consume tag nodes" do
|
54
|
-
tree = BetterHtml::NodeIterator.new("<div>")
|
55
|
-
|
56
|
-
assert_equal 1, tree.nodes.size
|
57
|
-
assert_equal BetterHtml::NodeIterator::Element, tree.nodes.first.class
|
58
|
-
assert_equal ["div"], tree.nodes.first.name_parts.map(&:text)
|
59
|
-
assert_equal false, tree.nodes.first.self_closing?
|
60
|
-
end
|
61
|
-
|
62
|
-
test "consume tag nodes with solidus" do
|
63
|
-
tree = BetterHtml::NodeIterator.new("</div>")
|
64
|
-
|
65
|
-
assert_equal 1, tree.nodes.size
|
66
|
-
assert_equal BetterHtml::NodeIterator::Element, tree.nodes.first.class
|
67
|
-
assert_equal ["div"], tree.nodes.first.name_parts.map(&:text)
|
68
|
-
assert_equal true, tree.nodes.first.closing?
|
69
|
-
end
|
70
|
-
|
71
|
-
test "sets self_closing when appropriate" do
|
72
|
-
tree = BetterHtml::NodeIterator.new("<div/>")
|
73
|
-
|
74
|
-
assert_equal 1, tree.nodes.size
|
75
|
-
assert_equal BetterHtml::NodeIterator::Element, tree.nodes.first.class
|
76
|
-
assert_equal ["div"], tree.nodes.first.name_parts.map(&:text)
|
77
|
-
assert_equal true, tree.nodes.first.self_closing?
|
78
|
-
end
|
79
|
-
|
80
|
-
test "consume tag nodes until name ends" do
|
81
|
-
tree = BetterHtml::NodeIterator.new("<div/>")
|
82
|
-
assert_equal 1, tree.nodes.size
|
83
|
-
assert_equal BetterHtml::NodeIterator::Element, tree.nodes.first.class
|
84
|
-
assert_equal ["div"], tree.nodes.first.name_parts.map(&:text)
|
85
|
-
|
86
|
-
tree = BetterHtml::NodeIterator.new("<div ")
|
87
|
-
assert_equal 1, tree.nodes.size
|
88
|
-
assert_equal BetterHtml::NodeIterator::Element, tree.nodes.first.class
|
89
|
-
assert_equal ["div"], tree.nodes.first.name_parts.map(&:text)
|
90
|
-
end
|
91
|
-
|
92
|
-
test "consume tag nodes with interpolation" do
|
93
|
-
tree = BetterHtml::NodeIterator.new("<ns:<%= name %>-thing>")
|
94
|
-
|
95
|
-
assert_equal 1, tree.nodes.size
|
96
|
-
assert_equal BetterHtml::NodeIterator::Element, tree.nodes.first.class
|
97
|
-
assert_equal ["ns:", "<%= name %>", "-thing"], tree.nodes.first.name_parts.map(&:text)
|
98
|
-
end
|
99
|
-
|
100
|
-
test "consume tag attributes nodes unquoted value" do
|
101
|
-
tree = BetterHtml::NodeIterator.new("<div foo=bar>")
|
102
|
-
|
103
|
-
assert_equal 1, tree.nodes.size
|
104
|
-
tag = tree.nodes.first
|
105
|
-
assert_equal BetterHtml::NodeIterator::Element, tag.class
|
106
|
-
assert_equal 1, tag.attributes.size
|
107
|
-
attribute = tag.attributes.first
|
108
|
-
assert_equal BetterHtml::NodeIterator::Attribute, attribute.class
|
109
|
-
assert_equal ["foo"], attribute.name_parts.map(&:text)
|
110
|
-
assert_equal ["bar"], attribute.value_parts.map(&:text)
|
111
|
-
end
|
112
|
-
|
113
|
-
test "consume attributes without name" do
|
114
|
-
tree = BetterHtml::NodeIterator.new("<div 'thing'>")
|
115
|
-
|
116
|
-
assert_equal 1, tree.nodes.size
|
117
|
-
tag = tree.nodes.first
|
118
|
-
assert_equal BetterHtml::NodeIterator::Element, tag.class
|
119
|
-
assert_equal 1, tag.attributes.size
|
120
|
-
attribute = tag.attributes.first
|
121
|
-
assert_equal BetterHtml::NodeIterator::Attribute, attribute.class
|
122
|
-
assert_predicate attribute.name, :empty?
|
123
|
-
assert_equal ["'", "thing", "'"], attribute.value_parts.map(&:text)
|
124
|
-
end
|
125
|
-
|
126
|
-
test "consume tag attributes nodes quoted value" do
|
127
|
-
tree = BetterHtml::NodeIterator.new("<div foo=\"bar\">")
|
128
|
-
|
129
|
-
assert_equal 1, tree.nodes.size
|
130
|
-
tag = tree.nodes.first
|
131
|
-
assert_equal BetterHtml::NodeIterator::Element, tag.class
|
132
|
-
assert_equal 1, tag.attributes.size
|
133
|
-
attribute = tag.attributes.first
|
134
|
-
assert_equal BetterHtml::NodeIterator::Attribute, attribute.class
|
135
|
-
assert_equal ["foo"], attribute.name_parts.map(&:text)
|
136
|
-
assert_equal ['"', "bar", '"'], attribute.value_parts.map(&:text)
|
137
|
-
end
|
138
|
-
|
139
|
-
test "consume tag attributes nodes interpolation in name and value" do
|
140
|
-
tree = BetterHtml::NodeIterator.new("<div data-<%= foo %>=\"some <%= value %> foo\">")
|
141
|
-
|
142
|
-
assert_equal 1, tree.nodes.size
|
143
|
-
tag = tree.nodes.first
|
144
|
-
assert_equal BetterHtml::NodeIterator::Element, tag.class
|
145
|
-
assert_equal 1, tag.attributes.size
|
146
|
-
attribute = tag.attributes.first
|
147
|
-
assert_equal BetterHtml::NodeIterator::Attribute, attribute.class
|
148
|
-
assert_equal ["data-", "<%= foo %>"], attribute.name_parts.map(&:text)
|
149
|
-
assert_equal ['"', "some ", "<%= value %>", " foo", '"'], attribute.value_parts.map(&:text)
|
150
|
-
end
|
151
|
-
|
152
|
-
test "attributes can be accessed through [] on Element object" do
|
153
|
-
tree = BetterHtml::NodeIterator.new("<div foo=\"bar\">")
|
154
|
-
|
155
|
-
assert_equal 1, tree.nodes.size
|
156
|
-
element = tree.nodes.first
|
157
|
-
assert_equal BetterHtml::NodeIterator::Element, element.class
|
158
|
-
assert_equal 1, element.attributes.size
|
159
|
-
assert_nil element['nonexistent']
|
160
|
-
refute_nil attribute = element['foo']
|
161
|
-
assert_equal BetterHtml::NodeIterator::Attribute, attribute.class
|
162
|
-
end
|
163
|
-
|
164
|
-
test "attribute values can be read unescaped" do
|
165
|
-
tree = BetterHtml::NodeIterator.new("<div foo=\"<">\">")
|
166
|
-
|
167
|
-
element = tree.nodes.first
|
168
|
-
assert_equal 1, element.attributes.size
|
169
|
-
attribute = element['foo']
|
170
|
-
assert_equal '<">', attribute.unescaped_value
|
171
|
-
end
|
172
|
-
|
173
|
-
test "attribute values does not unescape stuff inside erb" do
|
174
|
-
tree = BetterHtml::NodeIterator.new("<div foo=\"<<%= > %>>\">")
|
175
|
-
|
176
|
-
element = tree.nodes.first
|
177
|
-
assert_equal 1, element.attributes.size
|
178
|
-
attribute = element['foo']
|
179
|
-
assert_equal '<<%= > %>>', attribute.unescaped_value
|
180
|
-
end
|
181
|
-
|
182
|
-
test "consume text nodes" do
|
183
|
-
tree = BetterHtml::NodeIterator.new("here is <%= some %> text")
|
184
|
-
|
185
|
-
assert_equal 1, tree.nodes.size
|
186
|
-
assert_equal BetterHtml::NodeIterator::Text, tree.nodes.first.class
|
187
|
-
assert_equal ["here is ", "<%= some %>", " text"], tree.nodes.first.content_parts.map(&:text)
|
188
|
-
end
|
189
|
-
|
190
|
-
test "javascript template parsing works" do
|
191
|
-
tree = BetterHtml::NodeIterator.new("here is <%= some %> text", template_language: :javascript)
|
192
|
-
|
193
|
-
assert_equal 1, tree.nodes.size
|
194
|
-
assert_equal BetterHtml::NodeIterator::Text, tree.nodes.first.class
|
195
|
-
assert_equal ["here is ", "<%= some %>", " text"], tree.nodes.first.content_parts.map(&:text)
|
196
|
-
end
|
197
|
-
|
198
|
-
test "javascript template does not consume html tags" do
|
199
|
-
tree = BetterHtml::NodeIterator.new("<div <%= some %> />", template_language: :javascript)
|
200
|
-
|
201
|
-
assert_equal 1, tree.nodes.size
|
202
|
-
assert_equal BetterHtml::NodeIterator::Text, tree.nodes.first.class
|
203
|
-
assert_equal ["<div ", "<%= some %>", " />"], tree.nodes.first.content_parts.map(&:text)
|
204
|
-
end
|
205
|
-
|
206
|
-
test "lodash template parsing works" do
|
207
|
-
tree = BetterHtml::NodeIterator.new('<div class="[%= foo %]">', template_language: :lodash)
|
208
|
-
|
209
|
-
assert_equal 1, tree.nodes.size
|
210
|
-
node = tree.nodes.first
|
211
|
-
assert_equal BetterHtml::NodeIterator::Element, node.class
|
212
|
-
assert_equal "div", node.name
|
213
|
-
assert_equal 1, node.attributes.size
|
214
|
-
attribute = node.attributes.first
|
215
|
-
assert_equal "class", attribute.name
|
216
|
-
assert_equal [:attribute_quoted_value_start, :expr_literal,
|
217
|
-
:attribute_quoted_value_end], attribute.value_parts.map(&:type)
|
218
|
-
assert_equal ["\"", "[%= foo %]", "\""], attribute.value_parts.map(&:text)
|
219
|
-
end
|
220
|
-
end
|
221
|
-
end
|