Fix sanitizing lists contents (#11354)

* Add test

* Fix code for sanitizing nested lists stripping all tags
This commit is contained in:
ThibG 2019-07-19 01:44:58 +02:00 committed by Eugen Rochko
parent 28f3b13c63
commit 6c4a196b53
2 changed files with 6 additions and 0 deletions

View file

@ -25,6 +25,8 @@ class Sanitize
case env[:node_name] case env[:node_name]
when 'li' when 'li'
env[:node].traverse do |node| env[:node].traverse do |node|
next unless %w(p ul ol li).include?(node.name)
node.add_next_sibling('<br>') if node.next_sibling node.add_next_sibling('<br>') if node.next_sibling
node.replace(node.children) unless node.text? node.replace(node.children) unless node.text?
end end

View file

@ -22,5 +22,9 @@ describe Sanitize::Config do
it 'converts ul inside ul' do it 'converts ul inside ul' do
expect(Sanitize.fragment('<ul><li>Foo</li><li><ul><li>Bar</li><li>Baz</li></ul></li></ul>', subject)).to eq '<p>Foo<br>Bar<br>Baz</p>' expect(Sanitize.fragment('<ul><li>Foo</li><li><ul><li>Bar</li><li>Baz</li></ul></li></ul>', subject)).to eq '<p>Foo<br>Bar<br>Baz</p>'
end end
it 'keep links in lists' do
expect(Sanitize.fragment('<p>Check out:</p><ul><li><a href="https://joinmastodon.org" rel="nofollow noopener" target="_blank">joinmastodon.org</a></li><li>Bar</li></ul>', subject)).to eq '<p>Check out:</p><p><a href="https://joinmastodon.org" rel="nofollow noopener" target="_blank">joinmastodon.org</a><br>Bar</p>'
end
end end
end end