Skip to content

Commit d3e4c53

Browse files
committed
Support opt-in Nokogiri HTML5 parsing
In view components rendered for test, we should allow opt-in Nokogiri::HTML5 parsing Nokogiri::HTML is just an alias for Nokogiri::HTML4, so this is a non-breaking change.
1 parent cdd934c commit d3e4c53

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ nav_order: 5
2626

2727
*JP Balarini*
2828

29+
* Support opt-in Nokogiri HTML5 parsing in view components rendered for test
30+
31+
*Noah Silvera*
32+
2933
## 3.21.0
3034

3135
* Updates testing docs to include an example of how to use with RSpec.

docs/api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ or are made more publicly available via `"render.view_component"`.
272272
Will default to `false` in next major version.
273273
Defaults to `true`.
274274

275+
### `.use_html5_parsing`
276+
277+
Whether `Nokogiri::HTML5` is used for parsing generated view component HTML in test.
278+
Defaults to `false`.
279+
275280
### `.view_component_path`
276281

277282
The path in which components, their templates, and their sidecars should

lib/view_component/config.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def defaults
2525
preview_paths: default_preview_paths,
2626
test_controller: "ApplicationController",
2727
default_preview_layout: nil,
28-
capture_compatibility_patch_enabled: false
28+
capture_compatibility_patch_enabled: false,
29+
use_html5_parsing: false
2930
})
3031
end
3132

@@ -126,6 +127,11 @@ def defaults
126127
# Will default to `false` in next major version.
127128
# Defaults to `true`.
128129

130+
# @!attribute use_html5_parsing
131+
# @return [Boolean]
132+
# Whether `Nokogiri::HTML5` is used for parsing generated view component HTML in test.
133+
# Defaults to `false`.
134+
129135
# @!attribute render_monkey_patch_enabled
130136
# @return [Boolean] Whether the #render method should be monkey patched.
131137
# If this is disabled, use `#render_component` or

lib/view_component/test_helpers.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def render_inline(component, **args, &block)
6060

6161
# :nocov:
6262

63-
Nokogiri::HTML.fragment(@rendered_content)
63+
html_document_class.fragment(@rendered_content)
6464
end
6565

6666
# `JSON.parse`-d component output.
@@ -107,7 +107,7 @@ def render_preview(name, from: __vc_test_helpers_preview_class, params: {})
107107

108108
@rendered_content = result
109109

110-
Nokogiri::HTML.fragment(@rendered_content)
110+
html_document_class.fragment(@rendered_content)
111111
end
112112

113113
# Execute the given block in the view context (using `instance_exec`).
@@ -124,7 +124,7 @@ def render_preview(name, from: __vc_test_helpers_preview_class, params: {})
124124
def render_in_view_context(*args, &block)
125125
@page = nil
126126
@rendered_content = vc_test_controller.view_context.instance_exec(*args, &block)
127-
Nokogiri::HTML.fragment(@rendered_content)
127+
html_document_class.fragment(@rendered_content)
128128
end
129129
ruby2_keywords(:render_in_view_context) if respond_to?(:ruby2_keywords, true)
130130

@@ -297,5 +297,13 @@ def __vc_test_helpers_preview_class
297297
raise NameError, "`render_preview` expected to find #{result}, but it does not exist."
298298
end
299299
# :nocov:
300+
301+
def html_document_class
302+
@html_document_class ||= if Rails.application.config.view_component.use_html5_parsing
303+
Nokogiri::HTML5
304+
else
305+
Nokogiri::HTML4
306+
end
307+
end
300308
end
301309
end

test/sandbox/test/rendering_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def test_render_inline_allocations
1616
MyComponent.ensure_compiled
1717

1818
allocations = (Rails.version.to_f >= 8.0) ?
19-
{"3.5.0" => 117, "3.4.1" => 117, "3.3.7" => 129} :
20-
{"3.3.7" => 120, "3.3.0" => 120, "3.2.7" => 118, "3.1.6" => 118, "3.0.7" => 127}
19+
{"3.5.0" => 117, "3.4.1" => 119, "3.3.7" => 129} :
20+
{"3.3.7" => 120, "3.3.0" => 120, "3.2.7" => 118, "3.1.6" => 118, "3.0.7" => 130}
2121

2222
assert_allocations(**allocations) do
2323
render_inline(MyComponent.new)

0 commit comments

Comments
 (0)