Skip to content

Commit 6eebafc

Browse files
committed
Fix handling Uri with an anchor #209
1 parent b700a7d commit 6eebafc

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

src/Html2OpenXml/Expressions/HyperlinkExpression.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ public override IEnumerable<OpenXmlElement> Interpret (ParsingContext context)
112112
{
113113
h = new Hyperlink() { History = true, Anchor = "_top" };
114114
}
115-
// is it an anchor?
116-
else if (context.Converter.SupportsAnchorLinks && linkNode.Hash.Length > 1 && linkNode.Hash[0] == '#')
117-
{
118-
h = new Hyperlink(
119-
) { History = true, Anchor = linkNode.Hash.Substring(1) };
120-
}
121115
// ensure the links does not start with javascript:
122116
else if (AngleSharpExtensions.TryParseUrl(att, UriKind.Absolute, out var uri))
123117
{
@@ -126,6 +120,13 @@ public override IEnumerable<OpenXmlElement> Interpret (ParsingContext context)
126120
h = new Hyperlink(
127121
) { History = true, Id = extLink.Id };
128122
}
123+
// is it an anchor?
124+
else if (context.Converter.SupportsAnchorLinks && linkNode.Hash.Length > 1 && linkNode.Hash[0] == '#')
125+
{
126+
h = new Hyperlink(
127+
)
128+
{ History = true, Anchor = linkNode.Hash.Substring(1) };
129+
}
129130

130131
if (h == null)
131132
{

test/HtmlToOpenXml.Tests/LinkTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ public class LinkTests : HtmlConverterTestBase
1616
[TestCase("://www.site.com")]
1717
[TestCase("www.site.com")]
1818
[TestCase("http://www.site.com")]
19-
public void ExternalLink_ShouldSucceed (string link)
19+
[TestCase("http://www.site.com/#anchor1", "http://www.site.com/#anchor1")]
20+
public void ExternalLink_ShouldSucceed(string link, string expectedUri = "http://www.site.com/")
2021
{
2122
var elements = converter.Parse($@"<a href=""{link}"" title=""Test Tooltip"">Test Caption</a>");
22-
AssertHyperlink(mainPart, elements);
23+
AssertHyperlink(mainPart, elements, expectedUri);
2324
}
2425

2526
[TestCase(@"<a href=""javascript:alert()"">Js</a>")]
@@ -193,7 +194,7 @@ public async Task ParseIntoDocumentPart_ReturnsHyperlinkParentedToPart (Type ope
193194
throw new NotSupportedException($"Test case not supported for {openXmlPartType.FullName}");
194195
}
195196

196-
AssertHyperlink(container, host.ChildElements);
197+
AssertHyperlink(container, host.ChildElements, "http://www.site.com/");
197198
AssertThatOpenXmlDocumentIsValid();
198199
}
199200

@@ -249,7 +250,8 @@ await converter.ParseBody(@"<a href='#_top'>Move to top
249250
Assert.That(rel.Uri.ToString(), Is.EqualTo("#_top"));
250251
}
251252

252-
private static void AssertHyperlink(OpenXmlPartContainer container, IEnumerable<OpenXmlElement> elements)
253+
private static void AssertHyperlink(OpenXmlPartContainer container, IEnumerable<OpenXmlElement> elements,
254+
string expectedUri)
253255
{
254256
Assert.That(elements.Count(), Is.EqualTo(1));
255257
Assert.Multiple(() => {
@@ -272,7 +274,7 @@ private static void AssertHyperlink(OpenXmlPartContainer container, IEnumerable<
272274
var extLink = container.HyperlinkRelationships.FirstOrDefault(r => r.Id == hyperlink.Id);
273275
Assert.That(extLink, Is.Not.Null);
274276
Assert.That(extLink.IsExternal, Is.EqualTo(true));
275-
Assert.That(extLink.Uri.AbsoluteUri, Is.EqualTo("http://www.site.com/"));
277+
Assert.That(extLink.Uri.AbsoluteUri, Is.EqualTo(expectedUri));
276278
}
277279
}
278280
}

test/HtmlToOpenXml.Tests/Utilities/MockHttpMessageHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public MockHttpMessageHandler(Func<Uri, Task<HttpResponseMessage>> getResponseFu
1616

1717
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
1818
{
19-
return await _getResponseFunc(request.RequestUri);
19+
return await _getResponseFunc(request.RequestUri!);
2020
}
2121
}
2222
}

0 commit comments

Comments
 (0)