Skip to content

Commit 144f8e5

Browse files
committed
doc: update documentation to include HTML parser selection
1 parent 123af88 commit 144f8e5

File tree

3 files changed

+115
-11
lines changed

3 files changed

+115
-11
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ assert_dom_email '#you-got-mail'
3333

3434
The documentation in [selector_assertions.rb](https://github.com/rails/rails-dom-testing/blob/master/lib/rails/dom/testing/assertions/selector_assertions.rb) goes into a lot more detail of how selector assertions can be used.
3535

36+
### HTML versions
37+
38+
By default, assertions will use Nokogiri's HTML4 parser.
39+
40+
If `Rails::Dom::Testing.default_html_version` is set to `:html5`, then the assertions will use
41+
Nokogiri's HTML5 parser. (If the HTML5 parser is not available on your platform, then a
42+
`NotImplementedError` will be raised.)
43+
44+
When testing in a Rails application, the parser default can also be set by setting
45+
`Rails.application.config.dom_testing_default_html_version`.
46+
47+
Some assertions support an `html_version:` keyword argument which can override the default for that
48+
assertion. For example:
49+
50+
``` ruby
51+
# compare DOMs built with the HTML5 parser
52+
assert_dom_equal(expected, actual, html_version: :html5)
53+
54+
# compare DOMs built with the HTML4 parser
55+
assert_dom_not_equal(expected, actual, html_version: :html4)
56+
```
57+
58+
Please see documentation for individual assertions for more details.
59+
3660

3761
## Installation
3862

lib/rails/dom/testing/assertions/dom_assertions.rb

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,30 @@ module DomAssertions
1010
# \Test two HTML strings for equivalency (e.g., equal even when attributes are in another order)
1111
#
1212
# # assert that the referenced method generates the appropriate HTML string
13-
# assert_dom_equal '<a href="http://www.example.com">Apples</a>', link_to("Apples", "http://www.example.com")
13+
# assert_dom_equal(
14+
# '<a href="http://www.example.com">Apples</a>',
15+
# link_to("Apples", "http://www.example.com"),
16+
# )
17+
#
18+
# By default, the matcher will not pay attention to whitespace in text nodes (e.g., spaces
19+
# and newlines). If you want stricter matching with exact matching for whitespace, pass
20+
# <tt>strict: true</tt>:
21+
#
22+
# # these assertions will both pass
23+
# assert_dom_equal "<div>\nfoo\n\</div>", "<div>foo</div>", strict: false
24+
# assert_dom_not_equal "<div>\nfoo\n\</div>", "<div>foo</div>", strict: true
25+
#
26+
# The DOMs are created using an HTML parser specified by
27+
# Rails::Dom::Testing.default_html_version (either :html4 or :html5).
28+
#
29+
# When testing in a Rails application, the parser default can also be set by setting
30+
# +Rails.application.config.dom_testing_default_html_version+.
31+
#
32+
# If you want to specify the HTML parser just for a particular assertion, pass
33+
# <tt>html_version: :html4</tt> or <tt>html_version: :html5</tt> keyword arguments:
34+
#
35+
# assert_dom_equal expected, actual, html_version: :html5
36+
#
1437
def assert_dom_equal(expected, actual, message = nil, strict: false, html_version: nil)
1538
expected_dom, actual_dom = fragment(expected, html_version: html_version), fragment(actual, html_version: html_version)
1639
message ||= "Expected: #{expected}\nActual: #{actual}"
@@ -20,7 +43,30 @@ def assert_dom_equal(expected, actual, message = nil, strict: false, html_versio
2043
# The negated form of +assert_dom_equal+.
2144
#
2245
# # assert that the referenced method does not generate the specified HTML string
23-
# assert_dom_not_equal '<a href="http://www.example.com">Apples</a>', link_to("Oranges", "http://www.example.com")
46+
# assert_dom_not_equal(
47+
# '<a href="http://www.example.com">Apples</a>',
48+
# link_to("Oranges", "http://www.example.com"),
49+
# )
50+
#
51+
# By default, the matcher will not pay attention to whitespace in text nodes (e.g., spaces
52+
# and newlines). If you want stricter matching with exact matching for whitespace, pass
53+
# <tt>strict: true</tt>:
54+
#
55+
# # these assertions will both pass
56+
# assert_dom_equal "<div>\nfoo\n\</div>", "<div>foo</div>", strict: false
57+
# assert_dom_not_equal "<div>\nfoo\n\</div>", "<div>foo</div>", strict: true
58+
#
59+
# The DOMs are created using an HTML parser specified by
60+
# Rails::Dom::Testing.default_html_version (either :html4 or :html5).
61+
#
62+
# When testing in a Rails application, the parser default can also be set by setting
63+
# +Rails.application.config.dom_testing_default_html_version+.
64+
#
65+
# If you want to specify the HTML parser just for a particular assertion, pass
66+
# <tt>html_version: :html4</tt> or <tt>html_version: :html5</tt> keyword arguments:
67+
#
68+
# assert_dom_not_equal expected, actual, html_version: :html5
69+
#
2470
def assert_dom_not_equal(expected, actual, message = nil, strict: false, html_version: nil)
2571
expected_dom, actual_dom = fragment(expected, html_version: html_version), fragment(actual, html_version: html_version)
2672
message ||= "Expected: #{expected}\nActual: #{actual}"

lib/rails/dom/testing/assertions/selector_assertions.rb

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,24 @@ def assert_dom(*args, &block)
213213
# end
214214
# end
215215
# end
216+
#
217+
# The DOM is created using an HTML parser specified by
218+
# Rails::Dom::Testing.default_html_version (either :html4 or :html5).
219+
#
220+
# When testing in a Rails application, the parser default can also be set by setting
221+
# +Rails.application.config.dom_testing_default_html_version+.
222+
#
223+
# If you want to specify the HTML parser just for a particular assertion, pass
224+
# <tt>html_version: :html4</tt> or <tt>html_version: :html5</tt> keyword arguments:
225+
#
226+
# assert_dom "feed[xmlns='http://www.w3.org/2005/Atom']" do
227+
# assert_dom "entry>title" do
228+
# assert_dom_encoded(html_version: :html5) do
229+
# assert_dom "b"
230+
# end
231+
# end
232+
# end
233+
#
216234
def assert_dom_encoded(element = nil, html_version: nil, &block)
217235
if !element && !@selected
218236
raise ArgumentError, "Element is required when called from a nonnested assert_dom"
@@ -240,16 +258,32 @@ def assert_dom_encoded(element = nil, html_version: nil, &block)
240258
# You must enable deliveries for this assertion to work, use:
241259
# ActionMailer::Base.perform_deliveries = true
242260
#
243-
# assert_dom_email do
244-
# assert_dom "h1", "Email alert"
245-
# end
261+
# Example usage:
262+
#
263+
# assert_dom_email do
264+
# assert_dom "h1", "Email alert"
265+
# end
266+
#
267+
# assert_dom_email do
268+
# items = assert_dom "ol>li"
269+
# items.each do
270+
# # Work with items here...
271+
# end
272+
# end
273+
#
274+
# The DOM is created using an HTML parser specified by
275+
# Rails::Dom::Testing.default_html_version (either :html4 or :html5).
276+
#
277+
# When testing in a Rails application, the parser default can also be set by setting
278+
# +Rails.application.config.dom_testing_default_html_version+.
279+
#
280+
# If you want to specify the HTML parser just for a particular assertion, pass
281+
# <tt>html_version: :html4</tt> or <tt>html_version: :html5</tt> keyword arguments:
282+
#
283+
# assert_dom_email(html_version: :html5) do
284+
# assert_dom "h1", "Email alert"
285+
# end
246286
#
247-
# assert_dom_email do
248-
# items = assert_dom "ol>li"
249-
# items.each do
250-
# # Work with items here...
251-
# end
252-
# end
253287
def assert_dom_email(html_version: nil, &block)
254288
deliveries = ActionMailer::Base.deliveries
255289
assert !deliveries.empty?, "No e-mail in delivery list"

0 commit comments

Comments
 (0)