htrb 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +9 -9
- data/README.md +283 -281
- data/lib/htrb/component.rb +18 -18
- data/lib/htrb/html_document.rb +47 -47
- data/lib/htrb/html_elements.rb +56 -56
- data/lib/htrb/html_node.rb +194 -185
- data/lib/htrb/htrb_base.rb +15 -15
- data/lib/htrb/version.rb +3 -3
- data/lib/htrb.rb +8 -8
- data/test/htrb_test.rb +291 -253
- metadata +7 -7
data/test/htrb_test.rb
CHANGED
@@ -1,253 +1,291 @@
|
|
1
|
-
require 'simplecov'
|
2
|
-
SimpleCov.start
|
3
|
-
|
4
|
-
require 'minitest/autorun'
|
5
|
-
require 'minitest/reporters'
|
6
|
-
Minitest::Reporters.use!
|
7
|
-
|
8
|
-
require_relative '../lib/htrb'
|
9
|
-
|
10
|
-
class HtrbTest < Minitest::Test
|
11
|
-
def setup
|
12
|
-
@empty_frag = HTRB.fragment
|
13
|
-
|
14
|
-
tag = nil
|
15
|
-
@container_frag = HTRB.fragment do
|
16
|
-
tag = div! id: 'container'
|
17
|
-
end
|
18
|
-
|
19
|
-
@div = tag
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_html5_tags_are_methods
|
23
|
-
HTRB::TAGS.each do |tag|
|
24
|
-
tag = (tag.to_s + '!').to_sym
|
25
|
-
assert_equal true, @empty_frag.respond_to?(tag, true)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_fragment_is_node
|
30
|
-
assert_equal 'HTRB::HtmlNode', @empty_frag.class.name
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_fragment_to_s_empty
|
34
|
-
assert_equal '', @empty_frag.to_s
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_fragment_to_s
|
38
|
-
assert_equal '<div id="container"></div>', @container_frag.to_s
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_fragment_to_pretty_empty
|
42
|
-
assert_equal '', @empty_frag.to_pretty
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_fragment_to_pretty
|
46
|
-
@div.append 'Hello'
|
47
|
-
assert_equal "<div id=\"container\">\n Hello\n</div>", @container_frag.to_pretty
|
48
|
-
end
|
49
|
-
|
50
|
-
def
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
assert_equal
|
64
|
-
end
|
65
|
-
|
66
|
-
def
|
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
|
-
end
|
96
|
-
|
97
|
-
def
|
98
|
-
|
99
|
-
return_value = @container_frag.
|
100
|
-
assert_equal
|
101
|
-
assert_equal
|
102
|
-
end
|
103
|
-
|
104
|
-
def
|
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
|
-
assert_equal
|
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
|
-
assert_equal '
|
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
|
-
assert_equal
|
241
|
-
end
|
242
|
-
|
243
|
-
def
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'minitest/reporters'
|
6
|
+
Minitest::Reporters.use!
|
7
|
+
|
8
|
+
require_relative '../lib/htrb'
|
9
|
+
|
10
|
+
class HtrbTest < Minitest::Test
|
11
|
+
def setup
|
12
|
+
@empty_frag = HTRB.fragment
|
13
|
+
|
14
|
+
tag = nil
|
15
|
+
@container_frag = HTRB.fragment do
|
16
|
+
tag = div! id: 'container'
|
17
|
+
end
|
18
|
+
|
19
|
+
@div = tag
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_html5_tags_are_methods
|
23
|
+
HTRB::TAGS.each do |tag|
|
24
|
+
tag = (tag.to_s + '!').to_sym
|
25
|
+
assert_equal true, @empty_frag.respond_to?(tag, true)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_fragment_is_node
|
30
|
+
assert_equal 'HTRB::HtmlNode', @empty_frag.class.name
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_fragment_to_s_empty
|
34
|
+
assert_equal '', @empty_frag.to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_fragment_to_s
|
38
|
+
assert_equal '<div id="container"></div>', @container_frag.to_s
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_fragment_to_pretty_empty
|
42
|
+
assert_equal '', @empty_frag.to_pretty
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_fragment_to_pretty
|
46
|
+
@div.append 'Hello'
|
47
|
+
assert_equal "<div id=\"container\">\n Hello\n</div>", @container_frag.to_pretty
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_attribute_quote_to_s
|
51
|
+
out = HTRB.html do
|
52
|
+
div! attribute: '{"JSON": "Object"}'
|
53
|
+
end
|
54
|
+
|
55
|
+
assert_equal '<div attribute=\'{"JSON": "Object"}\'></div>', out
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_attribute_quote_to_pretty
|
59
|
+
out = HTRB.fragment do
|
60
|
+
div! attribute: '{"JSON": "Object"}'
|
61
|
+
end.to_pretty
|
62
|
+
|
63
|
+
assert_equal "<div attribute='{\"JSON\": \"Object\"}'>\n</div>", out
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_attribute_quote_escape_to_s
|
67
|
+
out = HTRB.html do
|
68
|
+
div! message: "I'd like to say \"hello\" to everyone."
|
69
|
+
end
|
70
|
+
|
71
|
+
expected =
|
72
|
+
'<div message=\'I'd like to say "hello" to everyone.\'></div>'
|
73
|
+
|
74
|
+
assert_equal expected, out
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_attribute_quote_escape_to_pretty
|
78
|
+
out = HTRB.fragment do
|
79
|
+
div! message: "I'd like to say \"hello\" to everyone."
|
80
|
+
end.to_pretty
|
81
|
+
|
82
|
+
expected =
|
83
|
+
"<div message='I'd like to say \"hello\" to everyone.'>\n</div>"
|
84
|
+
|
85
|
+
assert_equal expected, out
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_inner_html
|
89
|
+
assert_equal [@div], @container_frag.inner_html
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_inner_html_block
|
93
|
+
@container_frag.inner_html { }
|
94
|
+
refute_equal [@div], @container_frag.inner_html
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_fragment_append_node
|
98
|
+
paragraph = HTRB::Elements::P.new
|
99
|
+
return_value = @container_frag.append paragraph
|
100
|
+
assert_equal paragraph, return_value
|
101
|
+
assert_equal paragraph, @container_frag.inner_html[1]
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_fragment_append_text
|
105
|
+
text = 'Something'
|
106
|
+
return_value = @container_frag.append text
|
107
|
+
assert_equal text, return_value
|
108
|
+
assert_equal text, @container_frag.inner_html[1]
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_fragment_append_error
|
112
|
+
bad_child = nil
|
113
|
+
assert_raises(ArgumentError) { @empty_frag.append bad_child }
|
114
|
+
assert_raises HTRB::SelfClosingTagError do
|
115
|
+
HTRB::Elements::Hr.new.append @div
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_fragment_remove
|
120
|
+
return_value = @container_frag.remove @div
|
121
|
+
assert_equal @div, return_value
|
122
|
+
assert_equal 0, @container_frag.inner_html.length
|
123
|
+
|
124
|
+
return_value_empty = @container_frag.remove @div
|
125
|
+
assert_nil return_value_empty
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_fragment_insert_before
|
129
|
+
image = HTRB::Elements::Img.new
|
130
|
+
return_value = @container_frag.insert image, :before, @div
|
131
|
+
assert_equal image, return_value
|
132
|
+
assert_equal image, @container_frag.inner_html[0]
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_fragment_insert_after
|
136
|
+
text = 'after'
|
137
|
+
return_value = @container_frag.insert text, :after, @div
|
138
|
+
assert_equal text, return_value
|
139
|
+
assert_equal text, @container_frag.inner_html[1]
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_fragment_insert_errors
|
143
|
+
assert_raises(ArgumentError) { @empty_frag.insert @div, :before, @div }
|
144
|
+
assert_raises(ArgumentError) { @container_frag.insert @div, :never, @div }
|
145
|
+
assert_raises(ArgumentError) { @container_frag.insert nil, :after, @div }
|
146
|
+
|
147
|
+
link = @div.append HTRB::Elements::Link.new
|
148
|
+
assert_raises HTRB::TagParadoxError do
|
149
|
+
@div.insert @div, :before, link
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_self_closing_tag
|
154
|
+
hr = HTRB::Elements::Hr.new
|
155
|
+
assert_equal '<hr>', hr.to_s
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_self_closing_tag_error
|
159
|
+
assert_raises HTRB::SelfClosingTagError do
|
160
|
+
HTRB::Elements::Track.new {}
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_attribute_key_replace_underscore
|
165
|
+
br = HTRB::Elements::Br.new hx_post: '/clicked', 'hx_swap' => 'outerHTML'
|
166
|
+
assert_equal '<br hx-post="/clicked" hx-swap="outerHTML">', br.to_s
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_fragment_t
|
170
|
+
em = HTRB::Elements::Em.new { t! 'Testing text' }
|
171
|
+
assert_equal '<em>Testing text</em>', em.to_s
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_fagment_t_escape
|
175
|
+
strong = HTRB::Elements::Strong.new { t! 'Testing > you & < me' }
|
176
|
+
assert_equal '<strong>Testing > you & < me</strong>', strong.to_s
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_fragment_missing_method_error
|
180
|
+
assert_raises(NameError) { @empty_frag.stupid }
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_fragment_props
|
184
|
+
button = HTRB::Elements::Button.new text: 'Button Text' do
|
185
|
+
t! props[:text]
|
186
|
+
end
|
187
|
+
assert_equal '<button text="Button Text">Button Text</button>', button.to_s
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_component
|
191
|
+
eval <<-CLASS_DEFINITION
|
192
|
+
class Tester < HTRB::Component
|
193
|
+
def render(**attributes, &block)
|
194
|
+
t! 'Test'
|
195
|
+
remit &block if block_given?
|
196
|
+
end
|
197
|
+
end
|
198
|
+
CLASS_DEFINITION
|
199
|
+
|
200
|
+
assert_equal true, @empty_frag.respond_to?(:_tester!, true)
|
201
|
+
assert_equal true, Tester.new.self_closing?
|
202
|
+
|
203
|
+
@empty_frag.inner_html { _tester! }
|
204
|
+
assert_equal 'Test', @empty_frag.to_s
|
205
|
+
|
206
|
+
assert_raises HTRB::SelfClosingTagError do
|
207
|
+
Tester.new.inner_html {}
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_component_duplicate_error
|
212
|
+
assert_raises HTRB::TagExistsError do
|
213
|
+
eval <<-CLASS_DEFINITION
|
214
|
+
module One
|
215
|
+
class Exists < HTRB::Component; end
|
216
|
+
end
|
217
|
+
|
218
|
+
module Another
|
219
|
+
class Exists < HTRB::Component; end
|
220
|
+
end
|
221
|
+
CLASS_DEFINITION
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_html
|
226
|
+
return_value = HTRB.html do
|
227
|
+
div! id: 'container' do
|
228
|
+
img! src: 'lol.jpg'
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
assert_equal '<div id="container"><img src="lol.jpg"></div>', return_value
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_document_is_document
|
236
|
+
assert_kind_of HTRB::Document, HTRB.document
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_document_to_pretty
|
240
|
+
assert_equal "<!DOCTYPE html>\n<html>\n <head>\n <title>\n \n </title>\n <meta charset=\"UTF-8\">\n </head>\n <body>\n </body>\n</html>", HTRB.document.to_pretty
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_document_to_s
|
244
|
+
assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"></head><body></body></html>', HTRB.document.to_s
|
245
|
+
end
|
246
|
+
|
247
|
+
def test_document_with_title
|
248
|
+
doc = HTRB.document title: 'Has a title'
|
249
|
+
assert_equal '<!DOCTYPE html><html><head><title>Has a title</title><meta charset="UTF-8"></head><body></body></html>', doc.to_s
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_document_title_get
|
253
|
+
doc = HTRB.document title: 'Has a title'
|
254
|
+
assert_equal 'Has a title', doc.title
|
255
|
+
end
|
256
|
+
|
257
|
+
def test_document_title_set
|
258
|
+
doc = HTRB.document
|
259
|
+
old = HTRB.document.title
|
260
|
+
doc.title 'Nice'
|
261
|
+
|
262
|
+
refute_equal old, doc.title
|
263
|
+
assert_equal 'Nice', doc.title
|
264
|
+
assert_equal '<!DOCTYPE html><html><head><title>Nice</title><meta charset="UTF-8"></head><body></body></html>', doc.to_s
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_document_pass_head
|
268
|
+
head_proc = proc { link! rel: 'stylesheet', href: 'mystyle.css' }
|
269
|
+
doc = HTRB.document head: head_proc
|
270
|
+
|
271
|
+
assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"><link rel="stylesheet" href="mystyle.css"></head><body></body></html>', doc.to_s
|
272
|
+
end
|
273
|
+
|
274
|
+
def test_document_change_head
|
275
|
+
doc = HTRB.document
|
276
|
+
doc.head { link! rel: 'stylesheet', href: 'mystyle.css' }
|
277
|
+
|
278
|
+
assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"><link rel="stylesheet" href="mystyle.css"></head><body></body></html>', doc.to_s
|
279
|
+
end
|
280
|
+
|
281
|
+
def test_document_pass_body
|
282
|
+
doc = HTRB.document { t! 'This is my body' }
|
283
|
+
assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"></head><body>This is my body</body></html>', doc.to_s
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_document_change_body
|
287
|
+
doc = HTRB.document
|
288
|
+
doc.body { t! 'This is my body' }
|
289
|
+
assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"></head><body>This is my body</body></html>', doc.to_s
|
290
|
+
end
|
291
|
+
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: htrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christopher Perkins
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-10-
|
11
|
+
date: 2023-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description:
|
14
14
|
email:
|
15
15
|
- Christopher.Perkins@null.net
|
16
16
|
executables: []
|
@@ -31,7 +31,7 @@ homepage: https://github.com/QuasariNova/htrb
|
|
31
31
|
licenses:
|
32
32
|
- MIT
|
33
33
|
metadata: {}
|
34
|
-
post_install_message:
|
34
|
+
post_install_message:
|
35
35
|
rdoc_options: []
|
36
36
|
require_paths:
|
37
37
|
- lib
|
@@ -46,8 +46,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
requirements: []
|
49
|
-
rubygems_version: 3.
|
50
|
-
signing_key:
|
49
|
+
rubygems_version: 3.4.12
|
50
|
+
signing_key:
|
51
51
|
specification_version: 4
|
52
52
|
summary: HTRB is a dsl for html and webcomponents
|
53
53
|
test_files: []
|