Revert "Wrap tables in div"
This reverts commit 2a2feb469eac7fe8b3957d11c6bc147cb63be51a.
This commit is contained in:
committed by
Jorge Manrubia
parent
21d6316daf
commit
1a9b59ebb1
@@ -1,30 +1,5 @@
|
||||
module HtmlHelper
|
||||
def format_html(html)
|
||||
fragment = Loofah::HTML5::DocumentFragment.parse(html).scrub!(AutoLinkScrubber.new)
|
||||
wrap_tables(fragment)
|
||||
fragment.to_html.html_safe
|
||||
Loofah::HTML5::DocumentFragment.parse(html).scrub!(AutoLinkScrubber.new).to_html.html_safe
|
||||
end
|
||||
|
||||
private
|
||||
def wrap_tables(fragment)
|
||||
# Collect tables first to avoid modifying the collection while iterating
|
||||
tables = fragment.css("table").to_a
|
||||
|
||||
tables.each do |table|
|
||||
# Skip if already wrapped in a table-wrapper div
|
||||
parent = table.parent
|
||||
next if parent&.name == "div" && parent["class"]&.include?("table-wrapper")
|
||||
|
||||
# Save a copy of the table before replacing
|
||||
table_copy = table.dup
|
||||
|
||||
# Create wrapper div
|
||||
wrapper = Nokogiri::XML::Node.new("div", fragment.document)
|
||||
wrapper["class"] = "table-wrapper"
|
||||
wrapper.add_child(table_copy)
|
||||
|
||||
# Replace the original table with the wrapper (which contains the copied table)
|
||||
table.replace(wrapper)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -119,26 +119,4 @@ class HtmlHelperTest < ActionView::TestCase
|
||||
assert_no_match(/<img/, output, "should not create an img element")
|
||||
assert_includes output, "<img"
|
||||
end
|
||||
|
||||
test "wrap tables in table-wrapper div" do
|
||||
assert_equal_html \
|
||||
%(<div class="table-wrapper"><table><tbody><tr><td>test</td></tr></tbody></table></div>),
|
||||
format_html("<table><tbody><tr><td>test</td></tr></tbody></table>")
|
||||
end
|
||||
|
||||
test "wrap multiple tables in separate table-wrapper divs" do
|
||||
html = "<table><tbody><tr><td>first</td></tr></tbody></table><p>text</p><table><tbody><tr><td>second</td></tr></tbody></table>"
|
||||
output = format_html(html)
|
||||
|
||||
assert_match(/<div class="table-wrapper"><table>.*?first.*?<\/table><\/div>/, output)
|
||||
assert_match(/<div class="table-wrapper"><table>.*?second.*?<\/table><\/div>/, output)
|
||||
end
|
||||
|
||||
test "don't wrap tables that are already wrapped" do
|
||||
html = %(<div class="table-wrapper"><table><tbody><tr><td>test</td></tr></tbody></table></div>)
|
||||
output = format_html(html)
|
||||
|
||||
# Should not create nested wrappers
|
||||
assert_equal 1, output.scan(/class="table-wrapper"/).length
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user