Skip to content

Commit a74e2cf

Browse files
authored
Merge pull request #180 from ruby-docx/fix-160-table-cell-hyperlinks
Pass document_properties to table cell paragraphs so to_html works with hyperlinks (#160)
2 parents 210257f + 8bd3002 commit a74e2cf

6 files changed

Lines changed: 27 additions & 10 deletions

File tree

lib/docx/containers/table.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ def self.tag
1313
'tbl'
1414
end
1515

16-
def initialize(node)
16+
def initialize(node, document_properties = {}, doc = nil)
1717
@node = node
1818
@properties_tag = 'tblGrid'
19+
@document_properties = document_properties
20+
@document = doc
1921
end
2022

2123
# Array of row
2224
def rows
23-
@node.xpath('w:tr').map {|r_node| Containers::TableRow.new(r_node) }
25+
@node.xpath('w:tr').map {|r_node| Containers::TableRow.new(r_node, @document_properties, @document) }
2426
end
2527

2628
def row_count
@@ -31,7 +33,7 @@ def row_count
3133
def columns
3234
columns_containers = []
3335
(0..(column_count-1)).each do |i|
34-
columns_containers[i] = Containers::TableColumn.new @node.xpath("w:tr//w:tc[#{i+1}]")
36+
columns_containers[i] = Containers::TableColumn.new(@node.xpath("w:tr//w:tc[#{i+1}]"), @document_properties, @document)
3537
end
3638
columns_containers
3739
end

lib/docx/containers/table_cell.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ def self.tag
1212
'tc'
1313
end
1414

15-
def initialize(node)
15+
def initialize(node, document_properties = {}, doc = nil)
1616
@node = node
1717
@properties_tag = 'tcPr'
18+
@document_properties = document_properties
19+
@document = doc
1820
end
1921

2022
# Return text of paragraph's cell
@@ -24,7 +26,7 @@ def to_s
2426

2527
# Array of paragraphs contained within cell
2628
def paragraphs
27-
@node.xpath('w:p').map {|p_node| Containers::Paragraph.new(p_node) }
29+
@node.xpath('w:p').map {|p_node| Containers::Paragraph.new(p_node, @document_properties, @document) }
2830
end
2931

3032
# Iterate over each text run within a paragraph's cell

lib/docx/containers/table_column.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ def self.tag
1212
'w:gridCol'
1313
end
1414

15-
def initialize(cell_nodes)
15+
def initialize(cell_nodes, document_properties = {}, doc = nil)
1616
@node = ''
1717
@properties_tag = ''
18-
@cells = cell_nodes.map { |c_node| Containers::TableCell.new(c_node) }
18+
@document_properties = document_properties
19+
@document = doc
20+
@cells = cell_nodes.map { |c_node| Containers::TableCell.new(c_node, @document_properties, @document) }
1921
end
2022

2123
# Array of cells contained within row

lib/docx/containers/table_row.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ def self.tag
1212
'tr'
1313
end
1414

15-
def initialize(node)
15+
def initialize(node, document_properties = {}, doc = nil)
1616
@node = node
1717
@properties_tag = ''
18+
@document_properties = document_properties
19+
@document = doc
1820
end
1921

2022
# Array of cells contained within row
2123
def cells
22-
@node.xpath('w:tc').map {|c_node| Containers::TableCell.new(c_node) }
24+
@node.xpath('w:tc').map {|c_node| Containers::TableCell.new(c_node, @document_properties, @document) }
2325
end
2426

2527
end

lib/docx/document.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def parse_bookmark_from(b_node)
275275
end
276276

277277
def parse_table_from(t_node)
278-
Elements::Containers::Table.new(t_node)
278+
Elements::Containers::Table.new(t_node, document_properties, self)
279279
end
280280
end
281281
end

spec/docx/document_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@
202202
expect(@doc.tables[0].columns[1].cells[1].text).to match(/^Directive/)
203203
end
204204

205+
# Regression test for #160: rendering html for a cell that contains a
206+
# hyperlink must not raise (the cell's paragraphs need document_properties).
207+
it 'renders html for cells containing hyperlinks' do
208+
cell = @doc.tables[0].rows[1].cells[1]
209+
html = nil
210+
expect { html = cell.paragraphs.map(&:to_html).join }.to_not raise_error
211+
expect(html).to include('<a href="http://google.com/"')
212+
end
213+
205214
describe '#paragraphs' do
206215
it 'should not grabs paragraphs in the tables' do
207216
expect(@doc.paragraphs.map(&:text)).to_not include("Second table")

0 commit comments

Comments
 (0)