Support table and table column sizing
All checks were successful
CI Pipeline / build (pull_request) Successful in 13s
All checks were successful
CI Pipeline / build (pull_request) Successful in 13s
This commit is contained in:
@@ -134,4 +134,170 @@ class TableTest < Minitest::Test
|
||||
assert_includes xml, "R0C0"
|
||||
assert_includes xml, "R4C4"
|
||||
end
|
||||
|
||||
def test_table_with_auto_layout
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table(layout: :auto) do
|
||||
doc.tr do
|
||||
doc.td "Short"
|
||||
doc.td "Much longer content here"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, '<w:tblLayout w:type="autofit"/>'
|
||||
assert_includes xml, '<w:tblW w:w="0" w:type="auto"/>'
|
||||
assert_includes xml, '<w:tcW w:w="0" w:type="auto"/>'
|
||||
end
|
||||
|
||||
def test_table_with_fixed_layout
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table(layout: :fixed) do
|
||||
doc.tr do
|
||||
doc.td "Cell 1"
|
||||
doc.td "Cell 2"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, '<w:tblLayout w:type="fixed"/>'
|
||||
end
|
||||
|
||||
def test_table_with_explicit_column_widths_in_inches
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table(columns: %w[2in 3in]) do
|
||||
doc.tr do
|
||||
doc.td "A"
|
||||
doc.td "B"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# 2 inches = 2880 twips, 3 inches = 4320 twips
|
||||
assert_includes xml, '<w:tblW w:w="7200" w:type="dxa"/>'
|
||||
assert_includes xml, '<w:tcW w:w="2880" w:type="dxa"/>'
|
||||
assert_includes xml, '<w:tcW w:w="4320" w:type="dxa"/>'
|
||||
end
|
||||
|
||||
def test_table_with_percentage_columns
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table(columns: ["25%", "75%"]) do
|
||||
doc.tr do
|
||||
doc.td "A"
|
||||
doc.td "B"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# 25% = 1250 (fiftieths), 75% = 3750 (fiftieths)
|
||||
assert_includes xml, '<w:tblW w:w="5000" w:type="pct"/>'
|
||||
assert_includes xml, '<w:tcW w:w="1250" w:type="pct"/>'
|
||||
assert_includes xml, '<w:tcW w:w="3750" w:type="pct"/>'
|
||||
end
|
||||
|
||||
def test_table_with_centimeter_columns
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table(columns: ["2.54cm", "5cm"]) do
|
||||
doc.tr do
|
||||
doc.td "A"
|
||||
doc.td "B"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# 2.54cm = ~1440 twips (1 inch), 5cm = ~2835 twips
|
||||
assert_includes xml, '<w:tcW w:w="1440" w:type="dxa"/>'
|
||||
assert_includes xml, '<w:tcW w:w="2835" w:type="dxa"/>'
|
||||
end
|
||||
|
||||
def test_cell_with_explicit_width
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td("Narrow", width: "1in")
|
||||
doc.td("Wide", width: "4in")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# 1 inch = 1440 twips, 4 inches = 5760 twips
|
||||
assert_includes xml, '<w:tcW w:w="1440" w:type="dxa"/>'
|
||||
assert_includes xml, '<w:tcW w:w="5760" w:type="dxa"/>'
|
||||
end
|
||||
|
||||
def test_cell_width_with_block
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td(width: "2in") { doc.b { doc.text "Bold content" } }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, '<w:tcW w:w="2880" w:type="dxa"/>'
|
||||
assert_includes xml, "Bold content"
|
||||
assert_includes xml, "<w:b/>"
|
||||
end
|
||||
|
||||
def test_mixed_cell_widths_explicit_and_auto
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td("Fixed", width: "2in")
|
||||
doc.td "Auto"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# First cell has explicit width, second gets auto
|
||||
assert_includes xml, '<w:tcW w:w="2880" w:type="dxa"/>'
|
||||
assert_includes xml, '<w:tcW w:w="0" w:type="auto"/>'
|
||||
assert_includes xml, '<w:tblW w:w="0" w:type="auto"/>'
|
||||
end
|
||||
|
||||
def test_table_columns_override_cell_widths
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table(columns: %w[3in 3in]) do
|
||||
doc.tr do
|
||||
doc.td("A", width: "1in") # This width is ignored
|
||||
doc.td "B"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Table-level columns take precedence
|
||||
assert_includes xml, '<w:tcW w:w="4320" w:type="dxa"/>'
|
||||
refute_includes xml, '<w:tcW w:w="1440" w:type="dxa"/>'
|
||||
end
|
||||
|
||||
def test_table_layout_with_columns
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table(layout: :fixed, columns: %w[2in 2in 2in]) do
|
||||
doc.tr do
|
||||
doc.td "A"
|
||||
doc.td "B"
|
||||
doc.td "C"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, '<w:tblLayout w:type="fixed"/>'
|
||||
assert_includes xml, '<w:tcW w:w="2880" w:type="dxa"/>'
|
||||
end
|
||||
|
||||
def test_default_behavior_unchanged
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td "A"
|
||||
doc.td "B"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Default: equal percentage widths (5000 / 2 = 2500 per cell)
|
||||
assert_includes xml, '<w:tblW w:w="5000" w:type="pct"/>'
|
||||
assert_includes xml, '<w:tcW w:w="2500" w:type="pct"/>'
|
||||
refute_includes xml, "<w:tblLayout" # No layout element by default
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user