Skip to content

Commit

Permalink
Implement nokogiri-html5-inference to properly support HTML fragmen…
Browse files Browse the repository at this point in the history
…t parsing (#696)

This pull request implements the
[`nokogiri-html5-inference`](https://github.com/flavorjones/nokogiri-html5-inference)
to deal with parsing HTML5 fragments to be used with Selector Morphs.

The `nokogiri-html5-inference` gem come about out of the discussions in
#652 and sparklemotion/nokogiri#3023.

Fixes #652, Resolves
#692, Resolves
#674

Big thanks to @flavorjones to talking and working this through with me!

## Why should this be added

This pull request makes the handling of document fragments for selector
morphs more natural/predictable and resolves the last open issues to
release the final versions of StimulusReflex 3.5.
  • Loading branch information
marcoroth authored May 5, 2024
1 parent 3f1b030 commit 004355b
Show file tree
Hide file tree
Showing 7 changed files with 647 additions and 106 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/stimulus_reflex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 5 additions & 3 deletions lib/stimulus_reflex/html/document_fragment.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions stimulus_reflex.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 1 addition & 3 deletions test/broadcasters/selector_broadcaster_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ class SelectorBroadcasterTest < StimulusReflex::BroadcasterTestCase
"operations" => [
{
"selector" => "html",
# Nokogiri automatically adds a `<meta>` tag for the encoding
# See. https://github.com/sparklemotion/nokogiri/blob/6ea1449926ce97648bb2f7401c9e4fdcb0e261ba/lib/nokogiri/html4/document.rb#L34-L35
"html" => "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><title>Test</title></head><body><div><div>bar</div><div>baz</div></div></body>",
"html" => "<head><title>Test</title></head><body><div><div>bar</div><div>baz</div></div></body>",
"payload" => {},
"childrenOnly" => true,
"permanentAttributeName" => nil,
Expand Down
Loading

0 comments on commit 004355b

Please sign in to comment.