|
10 | 10 |
|
11 | 11 | from docx import Document |
12 | 12 | from docx.enum.text import WD_BREAK, WD_UNDERLINE |
13 | | -from docx.oxml.ns import qn |
| 13 | +from docx.oxml import parse_xml |
| 14 | +from docx.oxml.ns import nsdecls, qn |
| 15 | +from docx.text import Run |
14 | 16 |
|
15 | 17 | from helpers import test_docx, test_text |
16 | 18 |
|
@@ -38,6 +40,26 @@ def given_a_run_having_known_text_and_formatting(context): |
38 | 40 | context.run = run |
39 | 41 |
|
40 | 42 |
|
| 43 | +@given('a run having mixed text content') |
| 44 | +def given_a_run_having_mixed_text_content(context): |
| 45 | + """ |
| 46 | + Mixed here meaning it contains ``<w:tab/>``, ``<w:cr/>``, etc. elements. |
| 47 | + """ |
| 48 | + r_xml = """\ |
| 49 | + <w:r %s> |
| 50 | + <w:t>abc</w:t> |
| 51 | + <w:tab/> |
| 52 | + <w:t>def</w:t> |
| 53 | + <w:cr/> |
| 54 | + <w:t>ghi</w:t> |
| 55 | + <w:drawing/> |
| 56 | + <w:br/> |
| 57 | + <w:t>jkl</w:t> |
| 58 | + </w:r>""" % nsdecls('w') |
| 59 | + r = parse_xml(r_xml) |
| 60 | + context.run = Run(r) |
| 61 | + |
| 62 | + |
41 | 63 | @given('a run having {underline_type} underline') |
42 | 64 | def given_a_run_having_underline_type(context, underline_type): |
43 | 65 | run_idx = { |
@@ -91,6 +113,11 @@ def when_I_add_a_tab(context): |
91 | 113 | context.run.add_tab() |
92 | 114 |
|
93 | 115 |
|
| 116 | +@when('I assign mixed text to the text property') |
| 117 | +def when_I_assign_mixed_text_to_the_text_property(context): |
| 118 | + context.run.text = 'abc\tdef\nghi\rjkl' |
| 119 | + |
| 120 | + |
94 | 121 | @when('I assign {value_str} to its {bool_prop_name} property') |
95 | 122 | def when_assign_true_to_bool_run_prop(context, value_str, bool_prop_name): |
96 | 123 | value = {'True': True, 'False': False, 'None': None}[value_str] |
@@ -207,3 +234,10 @@ def then_the_tab_appears_at_the_end_of_the_run(context): |
207 | 234 | r = context.run._r |
208 | 235 | tab = r.find(qn('w:tab')) |
209 | 236 | assert tab is not None |
| 237 | + |
| 238 | + |
| 239 | +@then('the text of the run represents the textual run content') |
| 240 | +def then_the_text_of_the_run_represents_the_textual_run_content(context): |
| 241 | + assert context.run.text == 'abc\tdef\nghi\njkl', ( |
| 242 | + 'got \'%s\'' % context.run.text |
| 243 | + ) |
0 commit comments