We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 16e3f10 commit d0499b9Copy full SHA for d0499b9
4 files changed
features/hlk-props.feature
@@ -9,7 +9,6 @@ Feature: Access hyperlink properties
9
Then hyperlink.address is the URL of the hyperlink
10
11
12
- @wip
13
Scenario Outline: Hyperlink.contains_page_break reports presence of page-break
14
Given a hyperlink having <zero-or-more> rendered page breaks
15
Then hyperlink.contains_page_break is <value>
src/docx/oxml/text/hyperlink.py
@@ -2,7 +2,7 @@
2
3
from __future__ import annotations
4
5
-from typing import List
+from typing import TYPE_CHECKING, List
6
7
from docx.oxml.simpletypes import ST_OnOff, XsdString
8
from docx.oxml.text.run import CT_R
@@ -13,6 +13,9 @@
ZeroOrMore,
)
16
+if TYPE_CHECKING:
17
+ from docx.oxml.text.pagebreak import CT_LastRenderedPageBreak
18
+
19
20
class CT_Hyperlink(BaseOxmlElement):
21
"""`<w:hyperlink>` element, containing the text and address for a hyperlink."""
@@ -23,3 +26,8 @@ class CT_Hyperlink(BaseOxmlElement):
23
26
history = OptionalAttribute("w:history", ST_OnOff, default=True)
24
27
25
28
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
@@ -34,3 +34,15 @@ def address(self) -> str:
34
within the document.
35
"""
36
return self._parent.part.rels[self._hyperlink.rId].target_ref
37
38
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
@@ -24,6 +24,24 @@ def it_knows_the_hyperlink_URL(self, fake_parent: t.StoryChild):
assert hyperlink.address == "https://google.com/"
+ @pytest.mark.parametrize(
+ ("hlink_cxml", "expected_value"),
+ [
+ ("w:hyperlink", False),
+ ("w:hyperlink/w:r", False),
+ ('w:hyperlink/w:r/(w:t"abc",w:lastRenderedPageBreak,w:t"def")', True),
+ ('w:hyperlink/w:r/(w:lastRenderedPageBreak,w:t"abc",w:t"def")', True),
+ ('w:hyperlink/w:r/(w:t"abc",w:t"def",w:lastRenderedPageBreak)', True),
+ ],
+ )
+ def it_knows_whether_it_contains_a_page_break(
+ self, hlink_cxml: str, expected_value: bool, fake_parent: t.StoryChild
+ ):
+ hlink = cast(CT_Hyperlink, element(hlink_cxml))
+ hyperlink = Hyperlink(hlink, fake_parent)
+ assert hyperlink.contains_page_break is expected_value
# -- fixtures --------------------------------------------------------------------
@pytest.fixture
0 commit comments