|
7 | 7 | import pytest |
8 | 8 |
|
9 | 9 | from pptx.exc import InvalidXmlError |
10 | | -from pptx.oxml.text import CT_TextField |
| 10 | +from pptx.oxml.text import CT_TextField, CT_TextParagraph |
11 | 11 |
|
12 | | -from ..unitutil.cxml import element |
| 12 | +from ..unitutil.cxml import element, xml |
13 | 13 |
|
14 | 14 |
|
15 | 15 | class DescribeCT_TextField(object): |
@@ -51,3 +51,106 @@ def it_returns_empty_string_for_text_when_a_t_is_absent(self): |
51 | 51 | def it_reads_the_text_of_its_a_t_child(self): |
52 | 52 | fld = cast(CT_TextField, element('a:fld{id=foo,type=slidenum}/a:t"42"')) |
53 | 53 | assert fld.text == "42" |
| 54 | + |
| 55 | + def it_adds_an_a_t_child_on_text_assignment_when_absent(self): |
| 56 | + fld = cast(CT_TextField, element("a:fld{id=foo,type=slidenum}")) |
| 57 | + assert fld.t is None |
| 58 | + |
| 59 | + fld.text = "‹#›" |
| 60 | + |
| 61 | + assert fld.t is not None |
| 62 | + assert fld.text == "‹#›" |
| 63 | + assert fld.xml == xml('a:fld{id=foo,type=slidenum}/a:t"‹#›"') |
| 64 | + |
| 65 | + def it_replaces_existing_a_t_content_on_text_assignment(self): |
| 66 | + fld = cast(CT_TextField, element('a:fld{id=foo,type=slidenum}/a:t"old"')) |
| 67 | + |
| 68 | + fld.text = "new" |
| 69 | + |
| 70 | + assert fld.text == "new" |
| 71 | + # ---only one a:t child is present; assignment replaces, not appends--- |
| 72 | + assert len(fld.findall("{http://schemas.openxmlformats.org/drawingml/2006/main}t")) == 1 |
| 73 | + |
| 74 | + @pytest.mark.parametrize( |
| 75 | + ("input_value", "expected_a_t_text"), |
| 76 | + [ |
| 77 | + ("hello", "hello"), |
| 78 | + ("a\x07b", "a_x0007_b"), # BEL escapes |
| 79 | + ("tab\there", "tab\there"), # tab pass-through |
| 80 | + ("line1\nline2", "line1\nline2"), # newline pass-through |
| 81 | + ("esc\x1bhere", "esc_x001B_here"), # ESC escapes, uppercase hex |
| 82 | + ("", ""), |
| 83 | + ], |
| 84 | + ) |
| 85 | + def it_escapes_control_chars_when_assigning_text( |
| 86 | + self, input_value: str, expected_a_t_text: str |
| 87 | + ): |
| 88 | + fld = cast(CT_TextField, element("a:fld{id=foo,type=slidenum}")) |
| 89 | + |
| 90 | + fld.text = input_value |
| 91 | + |
| 92 | + # ---round-trip: reading back returns the escaped form because the |
| 93 | + # ---escape is permanent storage form, not a presentation layer--- |
| 94 | + assert fld.text == expected_a_t_text |
| 95 | + |
| 96 | + def it_escapes_BEL_to_uppercase_hex_via__escape_ctrl_chars(self): |
| 97 | + # ---BEL is x07; expected escape is "_x0007_" with uppercase hex--- |
| 98 | + assert CT_TextField._escape_ctrl_chars("ring\x07bell") == "ring_x0007_bell" |
| 99 | + |
| 100 | + def it_passes_tab_and_newline_through__escape_ctrl_chars(self): |
| 101 | + # ---x09 (HT) and x0A (LF) are explicitly excluded from the escape |
| 102 | + # ---range per OOXML §22.9.2.19; all other x00..x1F characters escape. |
| 103 | + assert CT_TextField._escape_ctrl_chars("a\tb\nc") == "a\tb\nc" |
| 104 | + |
| 105 | + # ---verify x0B (VT) and x1F (US, the highest in-range value) DO escape |
| 106 | + assert CT_TextField._escape_ctrl_chars("\x0b") == "_x000B_" |
| 107 | + assert CT_TextField._escape_ctrl_chars("\x1f") == "_x001F_" |
| 108 | + |
| 109 | + |
| 110 | +class DescribeCT_TextParagraph(object): |
| 111 | + """Unit-test suite for `pptx.oxml.text.CT_TextParagraph` field accessor.""" |
| 112 | + |
| 113 | + def it_can_add_an_a_fld_via__add_fld(self): |
| 114 | + p = cast(CT_TextParagraph, element("a:p")) |
| 115 | + |
| 116 | + fld = p._add_fld() |
| 117 | + |
| 118 | + assert isinstance(fld, CT_TextField) |
| 119 | + assert len(p.fld_lst) == 1 |
| 120 | + assert p.fld_lst[0] is fld |
| 121 | + |
| 122 | + def it_appends_a_fld_after_existing_runs_in_document_order(self): |
| 123 | + # ---fld successors=("a:endParaRPr",) places it after a:r and a:br, |
| 124 | + # ---and before a:endParaRPr. Verifying the post-r position confirms |
| 125 | + # ---xmlchemy honored the successors tuple correctly. |
| 126 | + p = cast(CT_TextParagraph, element('a:p/(a:r/a:t"x",a:endParaRPr)')) |
| 127 | + |
| 128 | + fld = p._add_fld() |
| 129 | + fld.id = "fld-1" |
| 130 | + |
| 131 | + # ---walk children of <a:p>; ignoring pPr (none here), expect: |
| 132 | + # ---a:r, a:fld, a:endParaRPr in that order |
| 133 | + tags = [child.tag.split("}")[-1] for child in p] |
| 134 | + assert tags == ["r", "fld", "endParaRPr"] |
| 135 | + |
| 136 | + def it_returns_all_fld_children_via_fld_lst(self): |
| 137 | + p = cast(CT_TextParagraph, element("a:p")) |
| 138 | + |
| 139 | + fld_a = p._add_fld() |
| 140 | + fld_b = p._add_fld() |
| 141 | + fld_c = p._add_fld() |
| 142 | + |
| 143 | + assert p.fld_lst == [fld_a, fld_b, fld_c] |
| 144 | + |
| 145 | + def it_includes_a_fld_in_content_children(self): |
| 146 | + # ---content_children must surface a:r, a:br, and a:fld in document order |
| 147 | + # ---so _Paragraph.text concatenates field text alongside run text. |
| 148 | + p = cast(CT_TextParagraph, element("a:p")) |
| 149 | + p.add_r("before") |
| 150 | + fld = p._add_fld() |
| 151 | + fld.id = "fld-1" |
| 152 | + fld.text = "[N]" |
| 153 | + p.add_r("after") |
| 154 | + |
| 155 | + texts = [child.text for child in p.content_children] |
| 156 | + assert texts == ["before", "[N]", "after"] |
0 commit comments