rails-dom-testing 2.1.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
- require_relative 'selector_assertions/count_describable'
2
- require_relative 'selector_assertions/html_selector'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "selector_assertions/html_selector"
3
4
 
4
5
  module Rails
5
6
  module Dom
@@ -18,257 +19,321 @@ module Rails
18
19
  # * +assert_dom_encoded+ - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions.
19
20
  # * +assert_dom_email+ - Assertions on the HTML body of an e-mail.
20
21
  module SelectorAssertions
22
+ # Select and return all matching elements.
23
+ #
24
+ # If called with a single argument, uses that argument as a selector.
25
+ # Called without an element +css_select+ selects from
26
+ # the element returned in +document_root_element+
27
+ #
28
+ # The default implementation of +document_root_element+ raises an exception explaining this.
29
+ #
30
+ # Returns an empty Nokogiri::XML::NodeSet if no match is found.
31
+ #
32
+ # If called with two arguments, uses the first argument as the root
33
+ # element and the second argument as the selector. Attempts to match the
34
+ # root element and any of its children.
35
+ # Returns an empty Nokogiri::XML::NodeSet if no match is found.
36
+ #
37
+ # The selector may be a CSS selector expression (String).
38
+ # css_select returns nil if called with an invalid css selector.
39
+ #
40
+ # # Selects all div tags
41
+ # divs = css_select("div")
42
+ #
43
+ # # Selects all paragraph tags and does something interesting
44
+ # pars = css_select("p")
45
+ # pars.each do |par|
46
+ # # Do something fun with paragraphs here...
47
+ # end
48
+ #
49
+ # # Selects all list items in unordered lists
50
+ # items = css_select("ul>li")
51
+ #
52
+ # # Selects all form tags and then all inputs inside the form
53
+ # forms = css_select("form")
54
+ # forms.each do |form|
55
+ # inputs = css_select(form, "input")
56
+ # ...
57
+ # end
58
+ def css_select(*args)
59
+ raise ArgumentError, "you at least need a selector argument" if args.empty?
21
60
 
22
- # Select and return all matching elements.
23
- #
24
- # If called with a single argument, uses that argument as a selector.
25
- # Called without an element +css_select+ selects from
26
- # the element returned in +document_root_element+
27
- #
28
- # The default implementation of +document_root_element+ raises an exception explaining this.
29
- #
30
- # Returns an empty Nokogiri::XML::NodeSet if no match is found.
31
- #
32
- # If called with two arguments, uses the first argument as the root
33
- # element and the second argument as the selector. Attempts to match the
34
- # root element and any of its children.
35
- # Returns an empty Nokogiri::XML::NodeSet if no match is found.
36
- #
37
- # The selector may be a CSS selector expression (String).
38
- # css_select returns nil if called with an invalid css selector.
39
- #
40
- # # Selects all div tags
41
- # divs = css_select("div")
42
- #
43
- # # Selects all paragraph tags and does something interesting
44
- # pars = css_select("p")
45
- # pars.each do |par|
46
- # # Do something fun with paragraphs here...
47
- # end
48
- #
49
- # # Selects all list items in unordered lists
50
- # items = css_select("ul>li")
51
- #
52
- # # Selects all form tags and then all inputs inside the form
53
- # forms = css_select("form")
54
- # forms.each do |form|
55
- # inputs = css_select(form, "input")
56
- # ...
57
- # end
58
- def css_select(*args)
59
- raise ArgumentError, "you at least need a selector argument" if args.empty?
61
+ root = args.size == 1 ? document_root_element : args.shift
60
62
 
61
- root = args.size == 1 ? document_root_element : args.shift
63
+ nodeset(root).css(args.first)
64
+ end
62
65
 
63
- nodeset(root).css(args.first)
64
- end
66
+ # An assertion that selects elements and makes one or more equality tests.
67
+ #
68
+ # If the first argument is an element, selects all matching elements
69
+ # starting from (and including) that element and all its children in
70
+ # depth-first order.
71
+ #
72
+ # If no element is specified +assert_dom+ selects from
73
+ # the element returned in +document_root_element+
74
+ # unless +assert_dom+ is called from within an +assert_dom+ block.
75
+ # Override +document_root_element+ to tell +assert_dom+ what to select from.
76
+ # The default implementation raises an exception explaining this.
77
+ #
78
+ # When called with a block +assert_dom+ passes an array of selected elements
79
+ # to the block. Calling +assert_dom+ from the block, with no element specified,
80
+ # runs the assertion on the complete set of elements selected by the enclosing assertion.
81
+ # Alternatively the array may be iterated through so that +assert_dom+ can be called
82
+ # separately for each element.
83
+ #
84
+ #
85
+ # ==== Example
86
+ # If the response contains two ordered lists, each with four list elements then:
87
+ # assert_dom "ol" do |elements|
88
+ # elements.each do |element|
89
+ # assert_dom element, "li", 4
90
+ # end
91
+ # end
92
+ #
93
+ # will pass, as will:
94
+ # assert_dom "ol" do
95
+ # assert_dom "li", 8
96
+ # end
97
+ #
98
+ # The selector may be a CSS selector expression (String, Symbol, or Numeric) or an expression
99
+ # with substitution values (Array).
100
+ # Substitution uses a custom pseudo class match. Pass in whatever attribute you want to match (enclosed in quotes) and a ? for the substitution.
101
+ # assert_dom returns nil if called with an invalid css selector.
102
+ #
103
+ # assert_dom "div:match('id', ?)", "id_string"
104
+ # assert_dom "div:match('id', ?)", :id_string
105
+ # assert_dom "div:match('id', ?)", 1
106
+ # assert_dom "div:match('id', ?)", /\d+/
107
+ #
108
+ # === Equality Tests
109
+ #
110
+ # The equality test may be one of the following:
111
+ # * <tt>true</tt> - Assertion is true if at least one element selected.
112
+ # * <tt>false</tt> - Assertion is true if no element selected.
113
+ # * <tt>String/Regexp</tt> - Assertion is true if the text value of at least
114
+ # one element matches the string or regular expression.
115
+ # * <tt>Integer</tt> - Assertion is true if exactly that number of
116
+ # elements are selected.
117
+ # * <tt>Range</tt> - Assertion is true if the number of selected
118
+ # elements fit the range.
119
+ # If no equality test specified, the assertion is true if at least one
120
+ # element selected.
121
+ #
122
+ # To perform more than one equality tests, use a hash with the following keys:
123
+ # * <tt>:text</tt> - Narrow the selection to elements that have this text
124
+ # value (string or regexp).
125
+ # * <tt>:html</tt> - Narrow the selection to elements that have this HTML
126
+ # content (string or regexp).
127
+ # * <tt>:count</tt> - Assertion is true if the number of selected elements
128
+ # is equal to this value.
129
+ # * <tt>:minimum</tt> - Assertion is true if the number of selected
130
+ # elements is at least this value.
131
+ # * <tt>:maximum</tt> - Assertion is true if the number of selected
132
+ # elements is at most this value.
133
+ #
134
+ # If the method is called with a block, once all equality tests are
135
+ # evaluated the block is called with an array of all matched elements.
136
+ #
137
+ # # At least one form element
138
+ # assert_dom "form"
139
+ #
140
+ # # Form element includes four input fields
141
+ # assert_dom "form input", 4
142
+ #
143
+ # # Page title is "Welcome"
144
+ # assert_dom "title", "Welcome"
145
+ #
146
+ # # Page title is "Welcome" and there is only one title element
147
+ # assert_dom "title", {count: 1, text: "Welcome"},
148
+ # "Wrong title or more than one title element"
149
+ #
150
+ # # Page contains no forms
151
+ # assert_dom "form", false, "This page must contain no forms"
152
+ #
153
+ # # Test the content and style
154
+ # assert_dom "body div.header ul.menu"
155
+ #
156
+ # # Use substitution values
157
+ # assert_dom "ol>li:match('id', ?)", /item-\d+/
158
+ #
159
+ # # All input fields in the form have a name
160
+ # assert_dom "form input" do
161
+ # assert_dom ":match('name', ?)", /.+/ # Not empty
162
+ # end
163
+ def assert_dom(*args, &block)
164
+ @selected ||= nil
65
165
 
66
- # An assertion that selects elements and makes one or more equality tests.
67
- #
68
- # If the first argument is an element, selects all matching elements
69
- # starting from (and including) that element and all its children in
70
- # depth-first order.
71
- #
72
- # If no element is specified +assert_dom+ selects from
73
- # the element returned in +document_root_element+
74
- # unless +assert_dom+ is called from within an +assert_dom+ block.
75
- # Override +document_root_element+ to tell +assert_dom+ what to select from.
76
- # The default implementation raises an exception explaining this.
77
- #
78
- # When called with a block +assert_dom+ passes an array of selected elements
79
- # to the block. Calling +assert_dom+ from the block, with no element specified,
80
- # runs the assertion on the complete set of elements selected by the enclosing assertion.
81
- # Alternatively the array may be iterated through so that +assert_dom+ can be called
82
- # separately for each element.
83
- #
84
- #
85
- # ==== Example
86
- # If the response contains two ordered lists, each with four list elements then:
87
- # assert_dom "ol" do |elements|
88
- # elements.each do |element|
89
- # assert_dom element, "li", 4
90
- # end
91
- # end
92
- #
93
- # will pass, as will:
94
- # assert_dom "ol" do
95
- # assert_dom "li", 8
96
- # end
97
- #
98
- # The selector may be a CSS selector expression (String, Symbol, or Numeric) or an expression
99
- # with substitution values (Array).
100
- # Substitution uses a custom pseudo class match. Pass in whatever attribute you want to match (enclosed in quotes) and a ? for the substitution.
101
- # assert_dom returns nil if called with an invalid css selector.
102
- #
103
- # assert_dom "div:match('id', ?)", "id_string"
104
- # assert_dom "div:match('id', ?)", :id_string
105
- # assert_dom "div:match('id', ?)", 1
106
- # assert_dom "div:match('id', ?)", /\d+/
107
- #
108
- # === Equality Tests
109
- #
110
- # The equality test may be one of the following:
111
- # * <tt>true</tt> - Assertion is true if at least one element selected.
112
- # * <tt>false</tt> - Assertion is true if no element selected.
113
- # * <tt>String/Regexp</tt> - Assertion is true if the text value of at least
114
- # one element matches the string or regular expression.
115
- # * <tt>Integer</tt> - Assertion is true if exactly that number of
116
- # elements are selected.
117
- # * <tt>Range</tt> - Assertion is true if the number of selected
118
- # elements fit the range.
119
- # If no equality test specified, the assertion is true if at least one
120
- # element selected.
121
- #
122
- # To perform more than one equality tests, use a hash with the following keys:
123
- # * <tt>:text</tt> - Narrow the selection to elements that have this text
124
- # value (string or regexp).
125
- # * <tt>:html</tt> - Narrow the selection to elements that have this HTML
126
- # content (string or regexp).
127
- # * <tt>:count</tt> - Assertion is true if the number of selected elements
128
- # is equal to this value.
129
- # * <tt>:minimum</tt> - Assertion is true if the number of selected
130
- # elements is at least this value.
131
- # * <tt>:maximum</tt> - Assertion is true if the number of selected
132
- # elements is at most this value.
133
- #
134
- # If the method is called with a block, once all equality tests are
135
- # evaluated the block is called with an array of all matched elements.
136
- #
137
- # # At least one form element
138
- # assert_dom "form"
139
- #
140
- # # Form element includes four input fields
141
- # assert_dom "form input", 4
142
- #
143
- # # Page title is "Welcome"
144
- # assert_dom "title", "Welcome"
145
- #
146
- # # Page title is "Welcome" and there is only one title element
147
- # assert_dom "title", {count: 1, text: "Welcome"},
148
- # "Wrong title or more than one title element"
149
- #
150
- # # Page contains no forms
151
- # assert_dom "form", false, "This page must contain no forms"
152
- #
153
- # # Test the content and style
154
- # assert_dom "body div.header ul.menu"
155
- #
156
- # # Use substitution values
157
- # assert_dom "ol>li:match('id', ?)", /item-\d+/
158
- #
159
- # # All input fields in the form have a name
160
- # assert_dom "form input" do
161
- # assert_dom ":match('name', ?)", /.+/ # Not empty
162
- # end
163
- def assert_dom(*args, &block)
164
- @selected ||= nil
166
+ selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }
167
+ dom_assertions(selector, &block)
168
+ end
169
+ alias_method :assert_select, :assert_dom
165
170
 
166
- selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }
171
+ # The negated form of +assert_dom+.
172
+ #
173
+ # === Equality Tests
174
+ #
175
+ # Supports the same equality tests as +assert_dom+ except for:
176
+ # * <tt>true</tt>
177
+ # * <tt>false</tt>
178
+ # * <tt>Integer</tt>
179
+ # * <tt>Range</tt>
180
+ # * <tt>:count</tt>
181
+ # * <tt>:minimum</tt>
182
+ # * <tt>:maximum</tt>
183
+ def assert_not_dom(*args, &block)
184
+ @selected ||= nil
167
185
 
168
- if selector.selecting_no_body?
169
- assert true
170
- return
186
+ selector = HTMLSelector.new(args, @selected, refute: true) { nodeset document_root_element }
187
+ dom_assertions(selector, &block)
171
188
  end
189
+ alias_method :refute_dom, :assert_not_dom
190
+ alias_method :assert_not_select, :assert_not_dom
191
+ alias_method :refute_select, :assert_not_dom
172
192
 
173
- selector.select.tap do |matches|
174
- assert_size_match!(matches.size, selector.tests,
175
- selector.css_selector, selector.message)
193
+ private def dom_assertions(selector, &block)
194
+ if selector.selecting_no_body?
195
+ assert true
196
+ return
197
+ end
176
198
 
177
- nest_selection(matches, &block) if block_given? && !matches.empty?
178
- end
179
- end
180
- alias_method :assert_select, :assert_dom
199
+ count, max = selector.tests.slice(:count, :maximum).values
181
200
 
182
- # Extracts the content of an element, treats it as encoded HTML and runs
183
- # nested assertion on it.
184
- #
185
- # You typically call this method within another assertion to operate on
186
- # all currently selected elements. You can also pass an element or array
187
- # of elements.
188
- #
189
- # The content of each element is un-encoded, and wrapped in the root
190
- # element +encoded+. It then calls the block with all un-encoded elements.
191
- #
192
- # # Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix)
193
- # assert_dom "feed[xmlns='http://www.w3.org/2005/Atom']" do
194
- # # Select each entry item and then the title item
195
- # assert_dom "entry>title" do
196
- # # Run assertions on the encoded title elements
197
- # assert_dom_encoded do
198
- # assert_dom "b"
199
- # end
200
- # end
201
- # end
202
- #
203
- #
204
- # # Selects all paragraph tags from within the description of an RSS feed
205
- # assert_dom "rss[version=2.0]" do
206
- # # Select description element of each feed item.
207
- # assert_dom "channel>item>description" do
208
- # # Run assertions on the encoded elements.
209
- # assert_dom_encoded do
210
- # assert_dom "p"
211
- # end
212
- # end
213
- # end
214
- def assert_dom_encoded(element = nil, &block)
215
- if !element && !@selected
216
- raise ArgumentError, "Element is required when called from a nonnested assert_dom"
201
+ selector.select.tap do |matches|
202
+ assert_size_match!(matches.size, selector.tests,
203
+ selector.css_selector, selector.message)
204
+
205
+ if block_given?
206
+ if count&.zero? || max&.zero?
207
+ raise ArgumentError, "Cannot be called with a block when asserting that a selector does not match"
208
+ end
209
+
210
+ nest_selection(matches, &block) unless matches.empty?
211
+ end
212
+ end
217
213
  end
218
214
 
219
- content = nodeset(element || @selected).map do |elem|
220
- elem.children.select do |child|
221
- child.cdata? || (child.text? && !child.blank?)
222
- end.map(&:content)
223
- end.join
215
+ # Extracts the content of an element, treats it as encoded HTML and runs
216
+ # nested assertion on it.
217
+ #
218
+ # You typically call this method within another assertion to operate on
219
+ # all currently selected elements. You can also pass an element or array
220
+ # of elements.
221
+ #
222
+ # The content of each element is un-encoded, and wrapped in the root
223
+ # element +encoded+. It then calls the block with all un-encoded elements.
224
+ #
225
+ # # Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix)
226
+ # assert_dom "feed[xmlns='http://www.w3.org/2005/Atom']" do
227
+ # # Select each entry item and then the title item
228
+ # assert_dom "entry>title" do
229
+ # # Run assertions on the encoded title elements
230
+ # assert_dom_encoded do
231
+ # assert_dom "b"
232
+ # end
233
+ # end
234
+ # end
235
+ #
236
+ #
237
+ # # Selects all paragraph tags from within the description of an RSS feed
238
+ # assert_dom "rss[version=2.0]" do
239
+ # # Select description element of each feed item.
240
+ # assert_dom "channel>item>description" do
241
+ # # Run assertions on the encoded elements.
242
+ # assert_dom_encoded do
243
+ # assert_dom "p"
244
+ # end
245
+ # end
246
+ # end
247
+ #
248
+ # The DOM is created using an HTML parser specified by
249
+ # Rails::Dom::Testing.default_html_version (either :html4 or :html5).
250
+ #
251
+ # When testing in a Rails application, the parser default can also be set by setting
252
+ # +Rails.application.config.dom_testing_default_html_version+.
253
+ #
254
+ # If you want to specify the HTML parser just for a particular assertion, pass
255
+ # <tt>html_version: :html4</tt> or <tt>html_version: :html5</tt> keyword arguments:
256
+ #
257
+ # assert_dom "feed[xmlns='http://www.w3.org/2005/Atom']" do
258
+ # assert_dom "entry>title" do
259
+ # assert_dom_encoded(html_version: :html5) do
260
+ # assert_dom "b"
261
+ # end
262
+ # end
263
+ # end
264
+ #
265
+ def assert_dom_encoded(element = nil, html_version: nil, &block)
266
+ if !element && !@selected
267
+ raise ArgumentError, "Element is required when called from a nonnested assert_dom"
268
+ end
224
269
 
225
- selected = Nokogiri::HTML::DocumentFragment.parse(content)
226
- nest_selection(selected) do
227
- if content.empty?
228
- yield selected
229
- else
230
- assert_dom ":root", &block
270
+ content = nodeset(element || @selected).map do |elem|
271
+ elem.children.select do |child|
272
+ child.cdata? || (child.text? && !child.blank?)
273
+ end.map(&:content)
274
+ end.join
275
+
276
+ selected = Rails::Dom::Testing.html_document_fragment(html_version: html_version).parse(content)
277
+ nest_selection(selected) do
278
+ if content.empty?
279
+ yield selected
280
+ else
281
+ assert_dom ":root", &block
282
+ end
231
283
  end
232
284
  end
233
- end
234
- alias_method :assert_select_encoded, :assert_dom_encoded
285
+ alias_method :assert_select_encoded, :assert_dom_encoded
235
286
 
236
- # Extracts the body of an email and runs nested assertions on it.
237
- #
238
- # You must enable deliveries for this assertion to work, use:
239
- # ActionMailer::Base.perform_deliveries = true
240
- #
241
- # assert_dom_email do
242
- # assert_dom "h1", "Email alert"
243
- # end
244
- #
245
- # assert_dom_email do
246
- # items = assert_dom "ol>li"
247
- # items.each do
248
- # # Work with items here...
249
- # end
250
- # end
251
- def assert_dom_email(&block)
252
- deliveries = ActionMailer::Base.deliveries
253
- assert !deliveries.empty?, "No e-mail in delivery list"
287
+ # Extracts the body of an email and runs nested assertions on it.
288
+ #
289
+ # You must enable deliveries for this assertion to work, use:
290
+ # ActionMailer::Base.perform_deliveries = true
291
+ #
292
+ # Example usage:
293
+ #
294
+ # assert_dom_email do
295
+ # assert_dom "h1", "Email alert"
296
+ # end
297
+ #
298
+ # assert_dom_email do
299
+ # items = assert_dom "ol>li"
300
+ # items.each do
301
+ # # Work with items here...
302
+ # end
303
+ # end
304
+ #
305
+ # The DOM is created using an HTML parser specified by
306
+ # Rails::Dom::Testing.default_html_version (either :html4 or :html5).
307
+ #
308
+ # When testing in a Rails application, the parser default can also be set by setting
309
+ # +Rails.application.config.dom_testing_default_html_version+.
310
+ #
311
+ # If you want to specify the HTML parser just for a particular assertion, pass
312
+ # <tt>html_version: :html4</tt> or <tt>html_version: :html5</tt> keyword arguments:
313
+ #
314
+ # assert_dom_email(html_version: :html5) do
315
+ # assert_dom "h1", "Email alert"
316
+ # end
317
+ #
318
+ def assert_dom_email(html_version: nil, &block)
319
+ deliveries = ActionMailer::Base.deliveries
320
+ assert !deliveries.empty?, "No e-mail in delivery list"
254
321
 
255
- deliveries.each do |delivery|
256
- (delivery.parts.empty? ? [delivery] : delivery.parts).each do |part|
257
- if part["Content-Type"].to_s =~ /^text\/html\W/
258
- root = Nokogiri::HTML::DocumentFragment.parse(part.body.to_s)
259
- assert_dom root, ":root", &block
322
+ deliveries.each do |delivery|
323
+ (delivery.parts.empty? ? [delivery] : delivery.parts).each do |part|
324
+ if /^text\/html\W/.match?(part["Content-Type"].to_s)
325
+ root = Rails::Dom::Testing.html_document_fragment(html_version: html_version).parse(part.body.to_s)
326
+ assert_dom root, ":root", &block
327
+ end
260
328
  end
261
329
  end
262
330
  end
263
- end
264
- alias_method :assert_select_email, :assert_dom_email
331
+ alias_method :assert_select_email, :assert_dom_email
265
332
 
266
333
  private
267
- include CountDescribable
268
-
269
334
  def document_root_element
270
- raise NotImplementedError, 'Implementing document_root_element makes ' \
271
- 'assert_dom work without needing to specify an element to select from.'
335
+ raise NotImplementedError, "Implementing document_root_element makes " \
336
+ "assert_dom work without needing to specify an element to select from."
272
337
  end
273
338
 
274
339
  # +equals+ must contain :minimum, :maximum and :count keys
@@ -284,6 +349,22 @@ module Rails
284
349
  end
285
350
  end
286
351
 
352
+ def count_description(min, max, count)
353
+ if min && max && (max != min)
354
+ "between #{min} and #{max} elements"
355
+ elsif min && max && max == min && count
356
+ "exactly #{count} #{pluralize_element(min)}"
357
+ elsif min && !(min == 1 && max == 1)
358
+ "at least #{min} #{pluralize_element(min)}"
359
+ elsif max
360
+ "at most #{max} #{pluralize_element(max)}"
361
+ end
362
+ end
363
+
364
+ def pluralize_element(quantity)
365
+ quantity == 1 ? "element" : "elements"
366
+ end
367
+
287
368
  def nest_selection(selection)
288
369
  # Set @selected to allow nested assert_dom.
289
370
  # Can be nested several levels deep.
@@ -1,6 +1,7 @@
1
- require 'nokogiri'
2
- require 'rails/dom/testing/assertions/dom_assertions'
3
- require 'rails/dom/testing/assertions/selector_assertions'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "assertions/dom_assertions"
4
+ require_relative "assertions/selector_assertions"
4
5
 
5
6
  module Rails
6
7
  module Dom
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Dom
5
+ module Testing
6
+ class Railtie < Rails::Railtie # :nodoc:
7
+ config.after_initialize do |app|
8
+ version = app.config.try(:dom_testing_default_html_version) # Rails 7.1+
9
+ Rails::Dom::Testing.default_html_version = version if version
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rails
2
4
  module Dom
3
5
  module Testing
4
- VERSION = "2.1.1"
6
+ VERSION = "2.3.0"
5
7
  end
6
8
  end
7
9
  end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+ require "active_support"
5
+ require "active_support/core_ext/module/attribute_accessors"
6
+
7
+ require "rails/dom/testing/assertions"
8
+
9
+ module Rails
10
+ module Dom
11
+ module Testing
12
+ mattr_accessor :default_html_version, default: :html4
13
+
14
+ class << self
15
+ def html5_support?
16
+ defined?(Nokogiri::HTML5)
17
+ end
18
+
19
+ def html_document(html_version: nil)
20
+ parser_classes = { html4: Nokogiri::HTML4::Document }
21
+ parser_classes[:html5] = Nokogiri::HTML5::Document if html5_support?
22
+
23
+ choose_html_parser(parser_classes, html_version: html_version)
24
+ end
25
+
26
+ def html_document_fragment(html_version: nil)
27
+ parser_classes = { html4: Nokogiri::HTML4::DocumentFragment }
28
+ parser_classes[:html5] = Nokogiri::HTML5::DocumentFragment if html5_support?
29
+
30
+ choose_html_parser(parser_classes, html_version: html_version)
31
+ end
32
+
33
+ private
34
+ def choose_html_parser(parser_classes, html_version: nil)
35
+ html_version ||= Rails::Dom::Testing.default_html_version
36
+
37
+ case html_version
38
+ when :html4
39
+ parser_classes[:html4]
40
+ when :html5
41
+ unless Rails::Dom::Testing.html5_support?
42
+ raise NotImplementedError, "html5 parser is not supported on this platform"
43
+ end
44
+ parser_classes[:html5]
45
+ else
46
+ raise ArgumentError, "html_version must be :html4 or :html5, received #{html_version.inspect}"
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end