diff --git a/Gemfile.lock b/Gemfile.lock index c82a4fb1..2580f44b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,6 +8,7 @@ PATH activesupport (>= 5.2, < 8) cable_ready (~> 5.0) nokogiri (~> 1.0) + nokogiri-html5-inference (~> 0.3) rack (>= 2, < 4) railties (>= 5.2, < 8) redis (>= 4.0, < 6.0) @@ -127,6 +128,8 @@ GEM racc (~> 1.4) nokogiri (1.16.2-x86_64-linux) racc (~> 1.4) + nokogiri-html5-inference (0.3.0) + nokogiri (~> 1.14) parallel (1.22.1) parser (3.2.1.0) ast (~> 2.4.1) diff --git a/lib/stimulus_reflex.rb b/lib/stimulus_reflex.rb index 8f75bc5a..50c27f6d 100644 --- a/lib/stimulus_reflex.rb +++ b/lib/stimulus_reflex.rb @@ -9,6 +9,7 @@ require "action_cable" require "action_view" require "nokogiri" +require "nokogiri/html5/inference" require "cable_ready" require "stimulus_reflex/version" require "stimulus_reflex/open_struct_fix" diff --git a/lib/stimulus_reflex/html/document_fragment.rb b/lib/stimulus_reflex/html/document_fragment.rb index 7ad6879f..846e5dda 100644 --- a/lib/stimulus_reflex/html/document_fragment.rb +++ b/lib/stimulus_reflex/html/document_fragment.rb @@ -1,12 +1,14 @@ # frozen_string_literal: true -# forzen_string_literal: true - module StimulusReflex module HTML class DocumentFragment < Document def parsing_class - Nokogiri + Nokogiri::HTML5::Inference + end + + def document_element + @document end end end diff --git a/stimulus_reflex.gemspec b/stimulus_reflex.gemspec index 5aed3d02..d2662980 100644 --- a/stimulus_reflex.gemspec +++ b/stimulus_reflex.gemspec @@ -50,6 +50,7 @@ Gem::Specification.new do |gem| gem.add_dependency "nokogiri", "~> 1.0" gem.add_dependency "rack", ">= 2", "< 4" gem.add_dependency "redis", ">= 4.0", "< 6.0" + gem.add_dependency "nokogiri-html5-inference", "~> 0.3" gem.add_development_dependency "bundler", "~> 2.0" gem.add_development_dependency "magic_frozen_string_literal", "~> 1.2" diff --git a/test/broadcasters/selector_broadcaster_test.rb b/test/broadcasters/selector_broadcaster_test.rb index ded2b4e8..bcb54a79 100644 --- a/test/broadcasters/selector_broadcaster_test.rb +++ b/test/broadcasters/selector_broadcaster_test.rb @@ -71,9 +71,7 @@ class SelectorBroadcasterTest < StimulusReflex::BroadcasterTestCase "operations" => [ { "selector" => "html", - # Nokogiri automatically adds a `` tag for the encoding - # See. https://github.com/sparklemotion/nokogiri/blob/6ea1449926ce97648bb2f7401c9e4fdcb0e261ba/lib/nokogiri/html4/document.rb#L34-L35 - "html" => "
1 |
---|
1 |
1 |
---|
1 |
1 |
---|
1 |
" do
+ html = "
1 | 2 | 1 | 2 | 1 | 2 | 1 | 2 | 1 | 2 | 1 | 2 | 1 | 2 | 1 | 2 | 1 | 2 |
+
+ )
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %(
+
+
+
+
+
+
+ )
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %(
+
+ After Input
+
+ After Input After Input tag" do + html = %( + + test + ) + fragment = StimulusReflex::HTML::DocumentFragment.new(html) + + assert_equal %( test), fragment.to_html.squish + assert_equal %( test), fragment.outer_html.squish + end + + test "non-closing tag" do + html = %( + + test + ) + fragment = StimulusReflex::HTML::DocumentFragment.new(html) + + assert_equal %( test), fragment.to_html.squish + assert_equal %( test), fragment.outer_html.squish + end + + test " in " do + html = %( + + Invalid div
+
+ )
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %(
Invalid div ), fragment.to_html.squish
+ assert_equal %(
Invalid div ), fragment.outer_html.squish
+ end
+
+ test " with attributes" do
+ html = %(
+
+ Hello World+ + ) + fragment = StimulusReflex::HTML::DocumentFragment.new(html) + + assert_equal %(Hello World), fragment.to_html.squish + assert_equal %(Hello World), fragment.outer_html.squish + end + + test " alongside " do + html = %( + + + ) + fragment = StimulusReflex::HTML::DocumentFragment.new(html) + + assert_equal %( ), fragment.to_html.squish + assert_equal %( ), fragment.outer_html.squish + end + + test " alongside with content" do + html = %( + + + + + +Hello World+ + ) + fragment = StimulusReflex::HTML::DocumentFragment.new(html) + + assert_equal %(Hello World), fragment.to_html.squish + assert_equal %(Hello World), fragment.outer_html.squish + end + + test " alongside inside " do + html = %( + + + + + ) + fragment = StimulusReflex::HTML::DocumentFragment.new(html) + + assert_equal %( ), fragment.to_html.squish + assert_equal %( ), fragment.outer_html.squish + end + + test " alongside inside with doctype and content" do + html = %( + + + +Header+ + + ) + fragment = StimulusReflex::HTML::DocumentFragment.new(html) + + assert_equal %(Header), fragment.to_html.squish + assert_equal %(Header), fragment.outer_html.squish + end + + test " alongside inside with content" do + html = %( + + + + + + +Hello World+ + + ) + fragment = StimulusReflex::HTML::DocumentFragment.new(html) + + assert_equal %(Hello World), fragment.to_html.squish + assert_equal %(Hello World), fragment.outer_html.squish + end + + test "
+
+
+
+ )
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %( tag" do
+ html = %(
+
+ )
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %(), fragment.to_html.squish
+ assert_equal %(), fragment.outer_html.squish
+ end
+
+ test "boolean attribute with attribute name as value on tag" do
+ html = %(
+
+ )
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %(), fragment.to_html.squish
+ assert_equal %(), fragment.outer_html.squish
+ end
+
+ test "boolean attribute on non-closed tag" do
+ html = %(
+
+ )
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %(), fragment.to_html.squish
+ assert_equal %(), fragment.outer_html.squish
+ end
+
+ test "boolean attribute on closed tag" do
+ html = %(
+
+ )
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %(), fragment.to_html.squish
+ assert_equal %(), fragment.outer_html.squish
+ end
+
+ test "boolean attribute with attribute name as value on non-closed tag" do
+ html = %(
+
+ )
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %(), fragment.to_html.squish
+ assert_equal %(), fragment.outer_html.squish
+ end
+
+ test "boolean attribute with attribute name as value on closed tag" do
+ html = %(
+
+ )
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %(), fragment.to_html.squish
+ assert_equal %(), fragment.outer_html.squish
+ end
+
+ test "should parse comments" do
+ html = %(
+
+ Content
+ )
+
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %( Content ), fragment.to_html.squish
+ assert_equal %( Content ), fragment.outer_html.squish
+ end
+
+ test "should parse comments with quotes" do
+ html = %(
+
+ Content
+ )
+
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %( Content ), fragment.to_html.squish
+ assert_equal %( Content ), fragment.outer_html.squish
+ end
+
+ test "case-sensitive attributes" do
+ html = %(1 )
+
+ fragment = StimulusReflex::HTML::DocumentFragment.new(html)
+
+ assert_equal %(1 ), fragment.to_html.squish
+ assert_equal %(1 ), fragment.outer_html.squish
+ end
+
+ test "case-sensitive |
---|