-
Notifications
You must be signed in to change notification settings - Fork 66
/
converter_spec.cr
282 lines (237 loc) · 6.8 KB
/
converter_spec.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
require "./spec_helper"
require "../vendor/html2lucky/converter"
describe HTML2Lucky::Converter do
it "converts basic html" do
input = "<div><p>Before Link<a>Link</a> After Link</p></div>"
expected_output = <<-CODE
div do
para do
text "Before Link"
a "Link"
text " After Link"
end
end
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "uses block syntax when inner text has new lines" do
input = "<div class='some-class'>First Line\nSecond Line</div>"
expected_output = <<-CODE
div class: "some-class" do
text "First Line Second Line"
end
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "handles empty tags properly" do
input = "<div></div>"
expected_output = <<-CODE
div
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "includes simple attributes" do
input = "<div class='some-class'>Hello</div>"
expected_output = <<-CODE
div "Hello", class: "some-class"
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "includes multiple attributes" do
input = "<div class='some-class-1 some-class-2' id='abc'>Hello</div>"
expected_output = <<-CODE
div "Hello", class: "some-class-1 some-class-2", id: "abc"
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "includes attributes that need quoting" do
input = "<div class='some-class-1' data-id='123'>Hello</div>"
expected_output = <<-CODE
div "Hello", class: "some-class-1", data_id: "123"
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "converts multiple empty spaces into just one space" do
input = "<div> \n\n </div>"
expected_output = <<-CODE
div " "
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "converts a tag with just new lines into a space" do
input = "<div>\n</div>"
expected_output = <<-CODE
div " "
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "converts just a tag with attributes and new lines into a tag with attributes and a space" do
input = "<div class='some-class'>\n</div>"
expected_output = <<-CODE
div " ", class: "some-class"
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "converts leading new lines into a space" do
input = "<div>\nHello</div>"
expected_output = <<-CODE
div do
text " Hello"
end
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "removes leading space" do
input = "<div> <a>Link</a></div>"
expected_output = <<-CODE
div do
a "Link"
end
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "removes trailing space" do
input = "<div><a>Link</a> </div>"
expected_output = <<-CODE
div do
a "Link"
end
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
it "converts trailing new lines into a space" do
input = "<div>Hello\n</div>"
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html <<-CODE
div do
text "Hello "
end
CODE
end
it "converts new lines inside of text into multiple text calls" do
input = "<div>First\nSecond\nThird</div>"
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html <<-CODE
div do
text "First Second Third"
end
CODE
end
it "doesn't print empty text in between tags" do
input = "<div></div>\n<div></div>"
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html <<-CODE
div
div
CODE
end
it "prints attributes for tags without children" do
input = "<div class='foo'></div>"
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html <<-CODE
div class: "foo"
CODE
end
it "prints quotes around weird attributes names" do
input = "<div @click.prevent='foo'></div>"
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html <<-CODE
div "@click.prevent": "foo"
CODE
end
it "prints quotes when using underscores" do
input = "<div underscore_attribute='foo'></div>"
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html <<-CODE
div "underscore_attribute": "foo"
CODE
end
it "works with custom tags" do
input = <<-HTML
<foo></foo>
<foo-bar></foo-bar>
<foo-bar class="foo"></foo-bar>
<foo-bar class="foo">text</foo-bar>
<div>
<foo-bar class="foo">
text
</foo-bar>
</div>
HTML
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html <<-CODE
tag "foo"
tag "foo-bar"
tag "foo-bar", class: "foo"
tag "foo-bar", "text", class: "foo"
div do
tag "foo-bar", class: "foo" do
text " text "
end
end
CODE
end
it "doesn't crash on invalid input" do
input = "<div <p>></p>"
HTML2Lucky::Converter.new(input).convert
end
it "treats renamed tags as non-custom tags" do
input = "<p>Hello</p>"
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html %q(para "Hello")
end
describe "converting comments" do
it "does not render a comment" do
input = "<!-- this comment -->"
output = HTML2Lucky::Converter.new(input).convert
output.should eq ""
end
it "strips comments from inside of tags" do
input = "<div><!-- secret --><span>with text</span></div>"
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html <<-CODE
div do
span "with text"
end
CODE
end
it "handles multiline comments" do
input = <<-HTML
<div>one</div>
<!--
comment here
and more stuff here
-->
<div>two</div>
HTML
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html <<-CODE
div "one"
div "two"
CODE
end
it "escapes text that includes quotes" do
input = "<div>Hello \"world\"</div>"
expected_output = <<-CODE
div "Hello \\"world\\""
CODE
output = HTML2Lucky::Converter.new(input).convert
output.should eq_html(expected_output.strip)
end
end
end
private def eq_html(html)
eq html + "\n"
end