From 0f565bd414a345961aead07ac4f72f2a8fc645a4 Mon Sep 17 00:00:00 2001 From: Jye Harry Date: Mon, 10 Mar 2025 22:00:43 +1100 Subject: [PATCH 1/3] Add macos to supported platforms in Gemfile.lock --- Gemfile.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile.lock b/Gemfile.lock index d977de2..63df29a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -89,6 +89,7 @@ GEM PLATFORMS aarch64-linux + x86_64-darwin-23 x86_64-linux DEPENDENCIES From 4a373224f50fa849f7bdaf461f4ebde2a704ca5a Mon Sep 17 00:00:00 2001 From: Jye Harry Date: Mon, 10 Mar 2025 22:58:09 +1100 Subject: [PATCH 2/3] Add failing test for assert_dom collapsing whitespace --- test/selector_assertions_test.rb | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/selector_assertions_test.rb b/test/selector_assertions_test.rb index 6aff0fc..6797495 100644 --- a/test/selector_assertions_test.rb +++ b/test/selector_assertions_test.rb @@ -237,6 +237,42 @@ def test_assert_select_with_invalid_minimum_and_maximum assert_equal "Range begin or :minimum cannot be greater than Range end or :maximum", error.message end + def test_assert_select_text_equality_collapses_whitespace + render_html "

Some\n line-broken\n text

" + + assert_nothing_raised do + assert_select "p", { + text: "Some line-broken text", + }, "Whitespace was not collapsed from text" + end + + render_html "

Some

line-broken

text

" + + assert_nothing_raised do + assert_select "p", { + text: "Someline-brokentext", + }, "
was not removed from text" + end + end + + def test_assert_select_html_equality_respects_whitespace + render_html "

Some\n line-broken\n text

" + + assert_nothing_raised do + assert_select "p", { + html: "Some\n line-broken\n text", + }, "Whitespace was collapsed from html" + end + + render_html "

Some

line-broken

text

" + + assert_nothing_raised do + assert_select "p", { + html: "Some

line-broken

text", + }, "
was removed from html" + end + end + # # Test assert_not_select. # From c0529222ff6520ee68c723ab7ea501be9ad6943e Mon Sep 17 00:00:00 2001 From: Jye Harry Date: Mon, 10 Mar 2025 22:44:18 +1100 Subject: [PATCH 3/3] Collapse whitespace from :text but not :html --- .../assertions/selector_assertions/html_selector.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb b/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb index b44d655..da177b7 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb @@ -51,14 +51,16 @@ def filter(matches) content_mismatch = nil text_matches = tests.has_key?(:text) + html_matches = tests.has_key?(:html) regex_matching = match_with.is_a?(Regexp) remaining = matches.reject do |match| # Preserve markup with to_s for html elements - content = text_matches ? match.text : match.children.to_s + content = text_matches ? match.text : match.inner_html content.strip! unless NO_STRIP.include?(match.name) content.delete_prefix!("\n") if text_matches && match.name == "textarea" + collapse_html_whitespace!(content) unless NO_STRIP.include?(match.name) || html_matches next if regex_matching ? (content =~ match_with) : (content == match_with) content_mismatch ||= diff(match_with, content) @@ -138,6 +140,10 @@ def extract_equality_tests(refute) comparisons end + + def collapse_html_whitespace!(text) + text.gsub!(/\s+/, " ") + end end end end