aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-05-30 11:10:05 +0100
committerMichael Williamson <mike@zwobble.org>2017-05-30 11:10:05 +0100
commitf772284b08698a47430549f18abc6e004ac06fb6 (patch)
tree6b52921e7fc9d228dfef1227bc999344fd4b3253
parent803e22c8b95faa99aad82a4c9a30aa132f123416 (diff)
Read target frame on hyperlinks
-rw-r--r--mammoth/documents.py5
-rw-r--r--mammoth/docx/body_xml.py13
-rw-r--r--tests/docx/body_xml_tests.py11
3 files changed, 25 insertions, 4 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py
index b440846..04e9d8e 100644
--- a/mammoth/documents.py
+++ b/mammoth/documents.py
@@ -41,6 +41,7 @@ class Text(Element):
class Hyperlink(HasChildren):
href = cobble.field()
anchor = cobble.field()
+ target_frame = cobble.field()
@cobble.data
class Table(HasChildren):
@@ -120,8 +121,8 @@ def tab():
image = Image
-def hyperlink(children, href=None, anchor=None):
- return Hyperlink(href=href, anchor=anchor, children=children)
+def hyperlink(children, href=None, anchor=None, target_frame=None):
+ return Hyperlink(href=href, anchor=anchor, target_frame=target_frame, children=children)
@cobble.data
diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py
index 49ff431..3e3e027 100644
--- a/mammoth/docx/body_xml.py
+++ b/mammoth/docx/body_xml.py
@@ -297,15 +297,24 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f
def hyperlink(element):
relationship_id = element.attributes.get("r:id")
anchor = element.attributes.get("w:anchor")
+ target_frame = element.attributes.get("w:tgtFrame")
children_result = _read_xml_elements(element.children)
+
+ def create(**kwargs):
+ return children_result.map(lambda children: documents.hyperlink(
+ children=children,
+ target_frame=target_frame,
+ **kwargs
+ ))
+
if relationship_id is not None:
href = relationships[relationship_id].target
if anchor is not None:
href = replace_fragment(href, anchor)
- return children_result.map(lambda children: documents.hyperlink(href=href, children=children))
+ return create(href=href)
elif anchor is not None:
- return children_result.map(lambda children: documents.hyperlink(anchor=anchor, children=children))
+ return create(anchor=anchor)
else:
return children_result
diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py
index e768bd5..2c418d2 100644
--- a/tests/docx/body_xml_tests.py
+++ b/tests/docx/body_xml_tests.py
@@ -672,6 +672,17 @@ class HyperlinkTests(object):
documents.run([]),
_read_and_get_document_xml_element(element)
)
+
+ @istest
+ def target_frame_is_read(self):
+ element = xml_element("w:hyperlink", {
+ "w:anchor": "start",
+ "w:tgtFrame": "_blank",
+ })
+ assert_that(
+ _read_and_get_document_xml_element(element),
+ is_hyperlink(target_frame="_blank"),
+ )
@istest