def word_table_is_read_as_document_table_element(self): element = xml_element( "w:tbl", {}, [xml_element("w:tr", {}, [xml_element("w:tc", {}, [xml_element("w:p", {}, [])])])] ) table = _read_and_get_document_xml_element(element) expected_result = documents.table([documents.table_row([documents.table_cell([documents.paragraph([])])])]) assert_equal(expected_result, table)
def unexpected_table_children_do_not_cause_error(): table = documents.table([ documents.tab(), ]) result = convert_document_element_to_html(table) expected_html = "<table>\t</table>" assert_equal(expected_html, result.value)
def docx_table_is_converted_to_table_in_html(): table = documents.table( [ documents.table_row( [ documents.table_cell([_paragraph_with_text("Top left")]), documents.table_cell([_paragraph_with_text("Top right")]), ] ), documents.table_row( [ documents.table_cell([_paragraph_with_text("Bottom left")]), documents.table_cell([_paragraph_with_text("Bottom right")]), ] ), ] ) result = convert_document_element_to_html(table) expected_html = ( "<table>" + "<tr><td><p>Top left</p></td><td><p>Top right</p></td></tr>" + "<tr><td><p>Bottom left</p></td><td><p>Bottom right</p></td></tr>" + "</table>" ) assert_equal(expected_html, result.value)
def vmerge_accounts_for_cells_spanning_columns(self): element = xml_element("w:tbl", {}, [ w_tr(w_tc(), w_tc(), w_tc(properties=[w_vmerge("restart")])), w_tr(w_tc(properties=[w_gridspan("2")]), w_tc(properties=[w_vmerge("continue")])), w_tr(w_tc(), w_tc(), w_tc(properties=[w_vmerge("continue")])), w_tr(w_tc(), w_tc(), w_tc()), ]) result = _read_and_get_document_xml_element(element) expected_result = documents.table([ documents.table_row([ documents.table_cell([]), documents.table_cell([]), documents.table_cell([], rowspan=3) ]), documents.table_row([documents.table_cell([], colspan=2)]), documents.table_row( [documents.table_cell([]), documents.table_cell([])]), documents.table_row([ documents.table_cell([]), documents.table_cell([]), documents.table_cell([]) ]), ]) assert_equal(expected_result, result)
def tbody_is_omitted_if_all_rows_are_headers(): table = documents.table([ documents.table_row([documents.table_cell([])], is_header=True), ]) result = convert_document_element_to_html(table) expected_html = ("<table>" + "<thead><tr><th></th></tr></thead>" + "</table>") assert_equal(expected_html, result.value)
def table_style_mappings_can_be_used_to_map_tables(): table = documents.table([], style_name="Normal Table") result = convert_document_element_to_html( table, style_map=[ _style_mapping("table[style-name='Normal Table'] => table.normal-table:fresh"), ], ) expected_html = '<table class="normal-table"></table>' assert_equal(expected_html, result.value)
def tbody_is_omitted_if_all_rows_are_headers(): table = documents.table([ documents.table_row([documents.table_cell([])], is_header=True), ]) result = convert_document_element_to_html(table) expected_html = ( "<table>" + "<thead><tr><th></th></tr></thead>" + "</table>") assert_equal(expected_html, result.value)
def table_cells_are_written_with_rowspan_if_not_equal_to_one(): table = documents.table([ documents.table_row([ documents.table_cell([], rowspan=2), ]), ]) result = convert_document_element_to_html(table) expected_html = ("<table>" + "<tr><td rowspan=\"2\"></td></tr>" + "</table>") assert_equal(expected_html, result.value)
def empty_rows_are_preserved_in_table(): table = documents.table([ documents.table_row([ documents.table_cell([_paragraph_with_text("Row 1")]), ]), documents.table_row([]), ]) result = convert_document_element_to_html(table) expected_html = ("<table>" + "<tr><td><p>Row 1</p></td></tr><tr></tr>" + "</table>") assert_equal(expected_html, result.value)
def no_vertical_cell_merging_if_merged_cells_do_not_line_up(self): element = xml_element("w:tbl", {}, [ w_tr(w_tc(properties=[w_gridspan("2")]), w_tc(properties=[w_vmerge("restart")])), w_tr(w_tc(), w_tc(properties=[w_vmerge("continue")])), ]) result = _read_and_get_document_xml_element(element) expected_result = documents.table([ documents.table_row([documents.table_cell([], colspan=2), documents.table_cell([])]), documents.table_row([documents.table_cell([]), documents.table_cell([])]), ]) assert_equal(expected_result, result)
def vmerge_without_val_is_treated_as_continue(self): element = xml_element("w:tbl", {}, [ w_tr(w_tc(properties=[w_vmerge("restart")])), w_tr(w_tc(properties=[w_vmerge(None)])), ]) result = _read_and_get_document_xml_element(element) expected_result = documents.table([ documents.table_row([documents.table_cell([], rowspan=2)]), documents.table_row([]), ]) assert_equal(expected_result, result)
def header_rows_are_wrapped_in_thead(): table = documents.table([ documents.table_row([documents.table_cell([])], is_header=True), documents.table_row([documents.table_cell([])], is_header=True), documents.table_row([documents.table_cell([])], is_header=False), ]) result = convert_document_element_to_html(table) expected_html = ("<table>" + "<thead><tr><th></th></tr><tr><th></th></tr></thead>" + "<tbody><tr><td></td></tr></tbody>" + "</table>") assert_equal(expected_html, result.value)
def word_table_is_read_as_document_table_element(self): element = xml_element("w:tbl", {}, [ xml_element("w:tr", {}, [ xml_element("w:tc", {}, [xml_element("w:p", {}, [])]), ]), ]) table = _read_and_get_document_xml_element(element) expected_result = documents.table([ documents.table_row( [documents.table_cell([documents.paragraph([])])]) ]) assert_equal(expected_result, table)
def table_cells_are_written_with_rowspan_if_not_equal_to_one(): table = documents.table([ documents.table_row([ documents.table_cell([], rowspan=2), ]), ]) result = convert_document_element_to_html(table) expected_html = ( "<table>" + "<tr><td rowspan=\"2\"></td></tr>" + "</table>") assert_equal(expected_html, result.value)
def table_cells_are_written_with_colspan_if_not_equal_to_one(): table = documents.table([ documents.table_row([ documents.table_cell([_paragraph_with_text("Top left")], colspan=2), documents.table_cell([_paragraph_with_text("Top right")]), ]), ]) result = convert_document_element_to_html(table) expected_html = ( "<table>" + "<tr><td colspan=\"2\"><p>Top left</p></td><td><p>Top right</p></td></tr>" + "</table>") assert_equal(expected_html, result.value)
def header_rows_are_wrapped_in_thead(): table = documents.table([ documents.table_row([documents.table_cell([])], is_header=True), documents.table_row([documents.table_cell([])], is_header=True), documents.table_row([documents.table_cell([])], is_header=False), ]) result = convert_document_element_to_html(table) expected_html = ( "<table>" + "<thead><tr><th></th></tr><tr><th></th></tr></thead>" + "<tbody><tr><td></td></tr></tbody>" + "</table>") assert_equal(expected_html, result.value)
def empty_rows_are_preserved_in_table(): table = documents.table([ documents.table_row([ documents.table_cell([_paragraph_with_text("Row 1")]), ]), documents.table_row([]), ]) result = convert_document_element_to_html(table) expected_html = ( "<table>" + "<tr><td><p>Row 1</p></td></tr><tr></tr>" + "</table>") assert_equal(expected_html, result.value)
def empty_cells_are_preserved_in_table(): table = documents.table( [ documents.table_row( [ documents.table_cell([_paragraph_with_text("")]), documents.table_cell([_paragraph_with_text("Top right")]), ] ) ] ) result = convert_document_element_to_html(table) expected_html = "<table>" + "<tr><td></td><td><p>Top right</p></td></tr>" + "</table>" assert_equal(expected_html, result.value)
def vmerge_accounts_for_cells_spanning_columns(self): element = xml_element("w:tbl", {}, [ w_tr(w_tc(), w_tc(), w_tc(properties=[w_vmerge("restart")])), w_tr(w_tc(properties=[w_gridspan("2")]), w_tc(properties=[w_vmerge("continue")])), w_tr(w_tc(), w_tc(), w_tc(properties=[w_vmerge("continue")])), w_tr(w_tc(), w_tc(), w_tc()), ]) result = _read_and_get_document_xml_element(element) expected_result = documents.table([ documents.table_row([documents.table_cell([]), documents.table_cell([]), documents.table_cell([], rowspan=3)]), documents.table_row([documents.table_cell([], colspan=2)]), documents.table_row([documents.table_cell([]), documents.table_cell([])]), documents.table_row([documents.table_cell([]), documents.table_cell([]), documents.table_cell([])]), ]) assert_equal(expected_result, result)
def gridspan_is_read_as_colspan_for_table_cell(self): element = xml_element("w:tbl", {}, [ xml_element("w:tr", {}, [ xml_element("w:tc", {}, [ xml_element("w:tcPr", {}, [xml_element("w:gridSpan", {"w:val": "2"})]), xml_element("w:p", {}, []) ]), ]), ]) table = _read_and_get_document_xml_element(element) expected_result = documents.table([ documents.table_row( [documents.table_cell([documents.paragraph([])], colspan=2)]) ]) assert_equal(expected_result, table)
def vmerge_is_read_as_rowspan_for_table_cell(self): element = xml_element("w:tbl", {}, [ w_tr(w_tc()), w_tr(w_tc(properties=[w_vmerge("restart")])), w_tr(w_tc(properties=[w_vmerge("continue")])), w_tr(w_tc(properties=[w_vmerge("continue")])), w_tr(w_tc()) ]) result = _read_and_get_document_xml_element(element) expected_result = documents.table([ documents.table_row([documents.table_cell([])]), documents.table_row([documents.table_cell([], rowspan=3)]), documents.table_row([]), documents.table_row([]), documents.table_row([documents.table_cell([])]), ]) assert_equal(expected_result, result)
def docx_table_is_converted_to_table_in_html(): table = documents.table([ documents.table_row([ documents.table_cell([_paragraph_with_text("Top left")]), documents.table_cell([_paragraph_with_text("Top right")]), ]), documents.table_row([ documents.table_cell([_paragraph_with_text("Bottom left")]), documents.table_cell([_paragraph_with_text("Bottom right")]), ]), ]) result = convert_document_element_to_html(table) expected_html = ( "<table>" + "<tr><td><p>Top left</p></td><td><p>Top right</p></td></tr>" + "<tr><td><p>Bottom left</p></td><td><p>Bottom right</p></td></tr>" + "</table>") assert_equal(expected_html, result.value)
def gridspan_is_read_as_colspan_for_table_cell(self): element = xml_element("w:tbl", {}, [ xml_element("w:tr", {}, [ xml_element("w:tc", {}, [ xml_element("w:tcPr", {}, [ xml_element("w:gridSpan", {"w:val": "2"}) ]), xml_element("w:p", {}, []) ]), ]), ]) table = _read_and_get_document_xml_element(element) expected_result = documents.table([ documents.table_row([ documents.table_cell([ documents.paragraph([]) ], colspan=2) ]) ]) assert_equal(expected_result, table)