Skip to content

Commit 2c20723

Browse files
committed
Merge pull request #20 from rails/body-refute-fix
Don't envelop fragments in documents.
2 parents c926c25 + 02a40f7 commit 2c20723

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ def css_select(*args)
6262
selector = args.first
6363

6464
begin
65-
nodeset(root).css(selector)
65+
root.css(selector).tap do |matches|
66+
return nodeset(root).css(selector) if matches.empty?
67+
end
6668
rescue Nokogiri::CSS::SyntaxError => e
6769
ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2))
6870
return
@@ -308,7 +310,7 @@ def determine_root_from(args, previous_selection = nil)
308310
elsif previous_selection
309311
previous_selection
310312
else
311-
nodeset document_root_element
313+
document_root_element
312314
end
313315
end
314316

test/selector_assertions_test.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ def test_feed_item_encoded
279279
end
280280
end
281281

282+
def test_body_not_present_in_empty_document
283+
render_html ''
284+
assert_select 'body', 0
285+
end
286+
282287
protected
283288
def render_html(html)
284289
fake_render(:html, html)
@@ -290,13 +295,13 @@ def render_xml(xml)
290295

291296
def fake_render(content_type, content)
292297
@html_document = if content_type == :xml
293-
Nokogiri::XML::Document.parse(content)
298+
Nokogiri::XML::DocumentFragment.parse(content)
294299
else
295-
Nokogiri::HTML::Document.parse(content)
300+
Nokogiri::HTML::DocumentFragment.parse(content)
296301
end
297302
end
298303

299304
def document_root_element
300-
@html_document.root
305+
@html_document
301306
end
302307
end

0 commit comments

Comments
 (0)