aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mammoth/compat.py9
-rw-r--r--mammoth/docx/body_xml.py5
-rw-r--r--mammoth/docx/uris.py7
-rw-r--r--tests/docx/body_xml_tests.py12
4 files changed, 21 insertions, 12 deletions
diff --git a/mammoth/compat.py b/mammoth/compat.py
deleted file mode 100644
index 5fb9acf..0000000
--- a/mammoth/compat.py
+++ /dev/null
@@ -1,9 +0,0 @@
-import sys
-
-if sys.version_info < (3, ):
- from urlparse import urldefrag
-else:
- from urllib.parse import urldefrag
-
-
-__all__ = ["urldefrag"]
diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py
index 0724ac8..cc4e98f 100644
--- a/mammoth/docx/body_xml.py
+++ b/mammoth/docx/body_xml.py
@@ -4,11 +4,10 @@ import re
from .. import documents
from .. import results
from .. import lists
-from ..compat import urldefrag
from . import complex_fields
from .xmlparser import node_types, XmlElement
from .styles_xml import Styles
-from .uris import uri_to_zip_entry_name
+from .uris import replace_fragment, uri_to_zip_entry_name
def reader(
@@ -296,7 +295,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f
if relationship_id is not None:
href = relationships[relationship_id].target
if anchor is not None:
- href = urldefrag(href)[0] + "#" + anchor
+ href = replace_fragment(href, anchor)
return children_result.map(lambda children: documents.hyperlink(href=href, children=children))
elif anchor is not None:
diff --git a/mammoth/docx/uris.py b/mammoth/docx/uris.py
index 59f3ffe..892078e 100644
--- a/mammoth/docx/uris.py
+++ b/mammoth/docx/uris.py
@@ -3,3 +3,10 @@ def uri_to_zip_entry_name(base, uri):
return uri[1:]
else:
return base + "/" + uri
+
+
+def replace_fragment(uri, fragment):
+ hash_index = uri.find("#")
+ if hash_index != -1:
+ uri = uri[:hash_index]
+ return uri + "#" + fragment
diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py
index 1277238..cccf472 100644
--- a/tests/docx/body_xml_tests.py
+++ b/tests/docx/body_xml_tests.py
@@ -624,6 +624,18 @@ class HyperlinkTests(object):
)
@istest
+ def existing_fragment_is_replaced_when_anchor_is_set_on_external_link(self):
+ relationships = Relationships({
+ "r42": Relationship(target="http://example.com/#previous")
+ })
+ run_element = xml_element("w:r")
+ element = xml_element("w:hyperlink", {"r:id": "r42", "w:anchor": "fragment"}, [run_element])
+ assert_equal(
+ documents.hyperlink(href="http://example.com/#fragment", children=[documents.run([])]),
+ _read_and_get_document_xml_element(element, relationships=relationships)
+ )
+
+ @istest
def hyperlink_is_read_as_internal_hyperlink_if_it_has_an_anchor_attribute(self):
run_element = xml_element("w:r")
element = xml_element("w:hyperlink", {"w:anchor": "start"}, [run_element])