Skip to content

Commit ba7265e

Browse files
committed
Envelop fragments in documents, and make document_root_element private again.
This reverts changes 02a40f7 and 6fc60c9.
1 parent 0867ced commit ba7265e

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,8 @@ def css_select(*args)
6060
raise ArgumentError, "you at least need a selector argument" if args.empty?
6161

6262
root = args.size == 1 ? document_root_element : args.shift
63-
selector = args.first
6463

65-
root.css(selector).tap do |matches|
66-
return nodeset(root).css(selector) if matches.empty?
67-
end
64+
nodeset(root).css(args.first)
6865
rescue Nokogiri::CSS::SyntaxError => e
6966
ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2))
7067
return
@@ -167,7 +164,7 @@ def css_select(*args)
167164
def assert_select(*args, &block)
168165
@selected ||= nil
169166

170-
selector = HTMLSelector.new(args, self, @selected)
167+
selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }
171168
selector.select.tap do |matches|
172169
assert_size_match!(matches.size, selector.tests, selector.selector, selector.message)
173170

@@ -258,14 +255,14 @@ def assert_select_email(&block)
258255
end
259256
end
260257

261-
def document_root_element
262-
raise NotImplementedError, 'Implementing document_root_element makes ' \
263-
'assert_select work without needing to specify an element to select from.'
264-
end
265-
266258
private
267259
include CountDescripable
268260

261+
def document_root_element
262+
raise NotImplementedError, 'Implementing document_root_element makes ' \
263+
'assert_select work without needing to specify an element to select from.'
264+
end
265+
269266
# +equals+ must contain :minimum, :maximum and :count keys
270267
def assert_size_match!(size, equals, css_selector, message = nil)
271268
min, max, count = equals[:minimum], equals[:maximum], equals[:count]

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
class HTMLSelector #:nodoc:
55
attr_reader :selector, :tests, :message
66

7-
def initialize(values, rootable, previous_selection = nil)
7+
def initialize(values, previous_selection = nil, &root_fallback)
88
@values = values
9-
@root = extract_root(rootable, previous_selection)
9+
@root = extract_root(previous_selection, root_fallback)
1010
@selector = extract_selector
1111
@tests = extract_equality_tests
1212
@message = @values.shift
@@ -50,7 +50,7 @@ def filter(matches)
5050
Nokogiri::XML::NodeSet.new(matches.document, remaining)
5151
end
5252

53-
def extract_root(rootable, previous_selection)
53+
def extract_root(previous_selection, root_fallback)
5454
possible_root = @values.first
5555

5656
if possible_root == nil
@@ -63,7 +63,7 @@ def extract_root(rootable, previous_selection)
6363
elsif previous_selection
6464
previous_selection
6565
else
66-
rootable.document_root_element
66+
root_fallback.call
6767
end
6868
end
6969

test/selector_assertions_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,6 @@ def test_body_class_can_be_tested_with_html
294294
assert_select '.foo'
295295
end
296296

297-
def document_root_element
298-
@html_document
299-
end
300-
301297
protected
302298
def render_html(html)
303299
fake_render(:html, html)
@@ -309,9 +305,13 @@ def render_xml(xml)
309305

310306
def fake_render(content_type, content)
311307
@html_document = if content_type == :xml
312-
Nokogiri::XML::DocumentFragment.parse(content)
308+
Nokogiri::XML::Document.parse(content)
313309
else
314-
Nokogiri::HTML::DocumentFragment.parse(content)
310+
Nokogiri::HTML::Document.parse(content)
315311
end
316312
end
313+
314+
def document_root_element
315+
@html_document.root
316+
end
317317
end

0 commit comments

Comments
 (0)