Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/docx/oxml/text/parfmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,19 @@ def __str__(self) -> str:
class CT_TabStops(BaseOxmlElement):
"""``<w:tabs>`` element, container for a sorted sequence of tab stops."""

tab_lst: list[CT_TabStop]
_new_tab: Callable[[], CT_TabStop]

tab = OneOrMore("w:tab", successors=())

def insert_tab_in_order(self, pos, align, leader):
"""Insert a newly created `w:tab` child element in `pos` order."""
new_tab = self._new_tab()
new_tab.pos, new_tab.val, new_tab.leader = pos, align, leader
return self._insert_tab_in_order(new_tab)

def _insert_tab_in_order(self, new_tab: CT_TabStop) -> CT_TabStop:
"""Insert a new or existing `w:tab` element in `pos` order."""
for tab in self.tab_lst:
if new_tab.pos < tab.pos:
tab.addprevious(new_tab)
Expand Down
4 changes: 2 additions & 2 deletions src/docx/text/tabstops.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,5 @@ def position(self):
def position(self, value):
tab = self._tab
tabs = tab.getparent()
self._tab = tabs.insert_tab_in_order(value, tab.val, tab.leader)
tabs.remove(tab)
tab.pos = value
tabs._insert_tab_in_order(tab)
30 changes: 30 additions & 0 deletions tests/text/test_tabstops.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Test suite for the docx.text.tabstops module."""

from io import BytesIO

import pytest

from docx import Document
from docx.enum.text import WD_TAB_ALIGNMENT, WD_TAB_LEADER
from docx.shared import Twips
from docx.text.tabstops import TabStop, TabStops
Expand All @@ -17,10 +20,31 @@ def it_knows_its_position(self, position_get_fixture):

def it_can_change_its_position(self, position_set_fixture):
tab_stop, value, tabs, new_idx, expected_xml = position_set_fixture
tab = tab_stop.element
tab_stop.position = value
assert tab_stop._tab is tabs[new_idx]
assert tab_stop.element is tab is tabs[new_idx]
assert tabs.xml == expected_xml

def it_keeps_existing_references_connected_when_repositioned(self):
document = Document()
tab_stops = document.add_paragraph().paragraph_format.tab_stops
tab_stop = tab_stops.add_tab_stop(Twips(720))
tab_stops.add_tab_stop(Twips(1440))
other_reference = tab_stops[0]

tab_stop.position = Twips(2160)
other_reference.leader = WD_TAB_LEADER.DOTS

stream = BytesIO()
document.save(stream)
stream.seek(0)
saved_tab_stops = Document(stream).paragraphs[0].paragraph_format.tab_stops
assert [stop.position for stop in saved_tab_stops] == [Twips(1440), Twips(2160)]
assert saved_tab_stops[1].leader == WD_TAB_LEADER.DOTS
assert other_reference.position == Twips(2160)
assert tab_stop == other_reference == tab_stops[1]

def it_knows_its_alignment(self, alignment_get_fixture):
tab_stop, expected_value = alignment_get_fixture
assert tab_stop.alignment == expected_value
Expand Down Expand Up @@ -121,6 +145,12 @@ def position_get_fixture(self, request):
1,
"w:tabs/(w:tab{w:pos=720,w:val=left},w:tab{w:pos=960,w:val=left})",
),
(
"w:tabs/(w:tab{w:pos=360,w:val=left},w:tab{w:pos=720,w:val=left})",
Twips(720),
1,
"w:tabs/(w:tab{w:pos=720,w:val=left},w:tab{w:pos=720,w:val=left})",
),
(
"w:tabs/(w:tab{w:pos=-72,w:val=left},w:tab{w:pos=-36,w:val=left})",
Twips(-48),
Expand Down