From 271b5f05d16576d479530663657c31ef37f1391b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 12 Mar 2026 21:24:22 +0000 Subject: Handle hyperlink complex fields with unquoted hrefs --- .../zwobble/mammoth/tests/docx/BodyXmlTests.java | 68 +++++++++++++++++++--- 1 file changed, 60 insertions(+), 8 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java index 71d8627..135ad54 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -374,13 +374,14 @@ public class BodyXmlTests { } @Test - public void runsInAComplexFieldForHyperlinksWithoutSwitchAreReadAsExternalHyperlinks() { - XmlElement hyperlinkRunXml = runXml("this is a hyperlink"); + public void runsInAComplexFieldForHyperlinksWithoutSwitchWithQuotedLocationAreReadAsExternalHyperlinks() { XmlElement element = paragraphXml(list( BEGIN_COMPLEX_FIELD, - HYPERLINK_INSTRTEXT, + element("w:instrText", list( + XmlNodes.text(" HYPERLINK \"http://example.com\"") + )), SEPARATE_COMPLEX_FIELD, - hyperlinkRunXml, + runXml("this is a hyperlink"), END_COMPLEX_FIELD )); DocumentElement paragraph = readSuccess(bodyReader(), element); @@ -389,7 +390,7 @@ public class BodyXmlTests { isEmptyRun(), isEmptyHyperlinkedRun(), isHyperlinkedRun( - hasHref(URI), + hasHref("http://example.com"), hasChildren( isTextElement("this is a hyperlink") ) @@ -399,15 +400,66 @@ public class BodyXmlTests { } @Test - public void runsInAComplexFieldForHyperlinksWithLSwitchAreReadAsInternalHyperlinks() { - XmlElement hyperlinkRunXml = runXml("this is a hyperlink"); + public void runsInAComplexFieldForHyperlinksWithoutSwitchWithUnquotedLocationAreReadAsExternalHyperlinks() { + XmlElement element = paragraphXml(list( + BEGIN_COMPLEX_FIELD, + element("w:instrText", list( + XmlNodes.text(" HYPERLINK http://example.com") + )), + SEPARATE_COMPLEX_FIELD, + runXml("this is a hyperlink"), + END_COMPLEX_FIELD + )); + DocumentElement paragraph = readSuccess(bodyReader(), element); + + assertThat(paragraph, isParagraph(hasChildren( + isEmptyRun(), + isEmptyHyperlinkedRun(), + isHyperlinkedRun( + hasHref("http://example.com"), + hasChildren( + isTextElement("this is a hyperlink") + ) + ), + isEmptyRun() + ))); + } + + @Test + public void runsInAComplexFieldForHyperlinksWithLSwitchWithQuotedLocationAreReadAsInternalHyperlinks() { XmlElement element = paragraphXml(list( BEGIN_COMPLEX_FIELD, element("w:instrText", list( XmlNodes.text(" HYPERLINK \\l \"InternalLink\"") )), SEPARATE_COMPLEX_FIELD, - hyperlinkRunXml, + runXml("this is a hyperlink"), + END_COMPLEX_FIELD + )); + DocumentElement paragraph = readSuccess(bodyReader(), element); + + assertThat(paragraph, isParagraph(hasChildren( + isEmptyRun(), + isEmptyHyperlinkedRun(), + isHyperlinkedRun( + hasAnchor("InternalLink"), + hasChildren( + isTextElement("this is a hyperlink") + ) + ), + isEmptyRun() + ))); + } + + @Test + public void runsInAComplexFieldForHyperlinksWithLSwitchWithUnquotedLocationAreReadAsInternalHyperlinks() { + XmlElement element = paragraphXml(list( + BEGIN_COMPLEX_FIELD, + element("w:instrText", list( + XmlNodes.text(" HYPERLINK \\l InternalLink") + )), + SEPARATE_COMPLEX_FIELD, + runXml("this is a hyperlink"), END_COMPLEX_FIELD )); DocumentElement paragraph = readSuccess(bodyReader(), element); -- cgit v1.2.3