Removes empty <p> tags from product description

This commit is contained in:
Jean-Baptiste Bellet
2023-03-31 11:37:30 +02:00
parent 4dfc56954c
commit b79bd8ef05
2 changed files with 12 additions and 0 deletions

View File

@@ -10,6 +10,14 @@ class ContentScrubber < Rails::Html::PermitScrubber
self.attributes = ALLOWED_ATTRIBUTES
end
def scrub(node)
if node.name == 'p' && (node.children.empty? || node.text.blank?)
node.remove
else
super
end
end
def skip_node?(node)
node.text?
end

View File

@@ -53,5 +53,9 @@ describe ContentSanitizer do
it "echos nil if given nil" do
expect(service.sanitize_content(nil)).to be(nil)
end
it "removes empty <p> tags and keeps non-empty ones" do
expect(service.sanitize_content("<p> </p><p></p><p><b></b><p>hello</p><p></p><p>world!</p>")).to eq("<p>hello</p><p>world!</p>")
end
end
end