8000 hlink: add Hyperlink.contains_page_break · python-openxml/python-docx@d0499b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0499b9

Browse files
committed
hlink: add Hyperlink.contains_page_break
1 parent 16e3f10 commit d0499b9

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

features/hlk-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Feature: Access hyperlink properties
99
Then hyperlink.address is the URL of the hyperlink
1010

1111

12-
@wip
1312
Scenario Outline: Hyperlink.contains_page_break reports presence of page-break
1413
Given a hyperlink having <zero-or-more> rendered page breaks
1514
Then hyperlink.contains_page_break is <value>

src/docx/oxml/text/hyperlink.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import List
5+
from typing import TYPE_CHECKING, List
66

77
from docx.oxml.simpletypes import ST_OnOff, XsdString
88
from docx.oxml.text.run import CT_R
@@ -13,6 +13,9 @@
1313
ZeroOrMore,
1414
)
1515

16+
if TYPE_CHECKING:
17+
from docx.oxml.text.pagebreak import CT_LastRenderedPageBreak
18+
1619

1720
class CT_Hyperlink(BaseOxmlElement):
1821
"""`<w:hyperlink>` element, containing the text and address for a hyperlink."""
@@ -23,3 +26,8 @@ class CT_Hyperlink(BaseOxmlElement):
2326
history = OptionalAttribute("w:history", ST_OnOff, default=True)
2427

2528
r = ZeroOrMore("w:r")
29+
30+
@property
31+
def lastRenderedPageBreaks(self) -> List[CT_LastRenderedPageBreak]:
32+
"""All `w:lastRenderedPageBreak` descendants of this hyperlink."""
33+
return self.xpath("./w:r/w:lastRenderedPageBreak")

src/docx/text/hyperlink.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,15 @@ def address(self) -> str:
3434
within the document.
3535
< 8000 span class=pl-s> """
3636
return self._parent.part.rels[self._hyperlink.rId].target_ref
37+
38+
@property
39+
def contains_page_break(self) -> bool:
40+
"""True when the text of this hyperlink is broken across page boundaries.
41+
42+
This is not uncommon and can happen for example when the hyperlink text is
43+
multiple words and occurs in the last line of a page. Theoretically, a hyperlink
44+
can contain more than one page break but that would be extremely uncommon in
45+
practice. Still, this value should be understood to mean that "one-or-more"
46+
rendered page breaks are present.
47+
"""
48+
return bool(self._hyperlink.lastRenderedPageBreaks)

tests/text/test_hyperlink.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ def it_knows_the_hyperlink_URL(self, fake_parent: t.StoryChild):
2424

2525
assert hyperlink.address == "https://google.com/"
2626

27+
@pytest.mark.parametrize(
28+
("hlink_cxml", "expected_value"),
29+
[
30+
("w:hyperlink", False),
31+
("w:hyperlink/w:r", False),
32+
('w:hyperlink/w:r/(w:t"abc",w:lastRenderedPageBreak,w:t"def")', True),
33+
('w:hyperlink/w:r/(w:lastRenderedPageBreak,w:t"abc",w:t"def")', True),
34+
('w:hyperlink/w:r/(w:t"abc",w:t"def",w:lastRenderedPageBreak)', True),
35+
],
36+
)
37+
def it_knows_whether_it_contains_a_page_break(
38+
self, hlink_cxml: str, expected_value: bool, fake_parent: t.StoryChild
39+
):
40+
hlink = cast(CT_Hyperlink, element(hlink_cxml))
41+
hyperlink = Hyperlink(hlink, fake_parent)
42+
43+
assert hyperlink.contains_page_break is expected_value
44+
2745
# -- fixtures --------------------------------------------------------------------
2846

2947
@pytest.fixture

0 commit comments

Comments
 (0)
0