Properly handle HTML fragments with self closing tags #674
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Type of PR (feature, enhancement, bug fix, etc.)
Bug Fix
Description
Properly handle HTML fragments including self closing tags like
img
,br
,input
that Nokogiri XML parser would normallytry to fix by adding a closing tag. For example,
<div><img src='x'><span>test</span></div>
would be changed internally to<div><img src='x'><span>test</span></img></div>
by Nokogiri and then converted to<div><img src='x'></div>
by Nokogiri through theto_html
method.This is due to the use of
Nokogiri
parser for the selector morphs (as opposed toNokogiri::HTML5::Document
parser), but we cannot use theNokogiri::HTML5::Document
when handling incomplete HTML documents (see 69cb070 for more info)One could say that we should produce valid html with
/
at the end of self-closing tags (ie:<br/>
instead of<br>
) but Nokogiri itself strips the trailling/
when producing html string from XML nodes...This PR detects these instances and handle them gracefully.
Fixes #652 (issue)
Why should this be added
The main issue for the user before this PR is that html tags are sometimes removed from the reflex response because of this behaviour.
This PR allows Stimulus Reflex users to apply selector morphs with HTML containing self-closing tags with siblings.
Checklist