|
1 | 1 | package org.commonmark.ext.autolink; |
2 | 2 |
|
3 | 3 | import org.commonmark.Extension; |
| 4 | +import org.commonmark.node.*; |
| 5 | +import org.commonmark.parser.IncludeSourceSpans; |
4 | 6 | import org.commonmark.parser.Parser; |
5 | 7 | import org.commonmark.renderer.html.HtmlRenderer; |
6 | 8 | import org.commonmark.testutil.RenderingTestCase; |
7 | 9 | import org.junit.Test; |
8 | 10 |
|
| 11 | +import java.util.Arrays; |
9 | 12 | import java.util.Collections; |
10 | 13 | import java.util.Set; |
11 | 14 |
|
| 15 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | + |
12 | 19 | public class AutolinkTest extends RenderingTestCase { |
13 | 20 |
|
14 | 21 | private static final Set<Extension> EXTENSIONS = Collections.singleton(AutolinkExtension.create()); |
@@ -53,6 +60,59 @@ public void dontLinkTextWithinLinks() { |
53 | 60 | "<p><a href=\"http://example.com\">http://example.com</a></p>\n"); |
54 | 61 | } |
55 | 62 |
|
| 63 | + @Test |
| 64 | + public void sourceSpans() { |
| 65 | + Parser parser = Parser.builder() |
| 66 | + .extensions(EXTENSIONS) |
| 67 | + .includeSourceSpans(IncludeSourceSpans.BLOCKS_AND_INLINES) |
| 68 | + .build(); |
| 69 | + Node document = parser.parse("abc\n" + |
| 70 | + "http://example.com/one\n" + |
| 71 | + "def http://example.com/two\n" + |
| 72 | + "ghi http://example.com/three jkl"); |
| 73 | + |
| 74 | + Paragraph paragraph = (Paragraph) document.getFirstChild(); |
| 75 | + Text abc = (Text) paragraph.getFirstChild(); |
| 76 | + assertEquals(Arrays.asList(SourceSpan.of(0, 0, 3)), |
| 77 | + abc.getSourceSpans()); |
| 78 | + |
| 79 | + assertTrue(abc.getNext() instanceof SoftLineBreak); |
| 80 | + |
| 81 | + Link one = (Link) abc.getNext().getNext(); |
| 82 | + assertEquals("http://example.com/one", one.getDestination()); |
| 83 | + assertEquals(Arrays.asList(SourceSpan.of(1, 0, 22)), |
| 84 | + one.getSourceSpans()); |
| 85 | + |
| 86 | + assertTrue(one.getNext() instanceof SoftLineBreak); |
| 87 | + |
| 88 | + Text def = (Text) one.getNext().getNext(); |
| 89 | + assertEquals("def ", def.getLiteral()); |
| 90 | + assertEquals(Arrays.asList(SourceSpan.of(2, 0, 4)), |
| 91 | + def.getSourceSpans()); |
| 92 | + |
| 93 | + Link two = (Link) def.getNext(); |
| 94 | + assertEquals("http://example.com/two", two.getDestination()); |
| 95 | + assertEquals(Arrays.asList(SourceSpan.of(2, 4, 22)), |
| 96 | + two.getSourceSpans()); |
| 97 | + |
| 98 | + assertTrue(two.getNext() instanceof SoftLineBreak); |
| 99 | + |
| 100 | + Text ghi = (Text) two.getNext().getNext(); |
| 101 | + assertEquals("ghi ", ghi.getLiteral()); |
| 102 | + assertEquals(Arrays.asList(SourceSpan.of(3, 0, 4)), |
| 103 | + ghi.getSourceSpans()); |
| 104 | + |
| 105 | + Link three = (Link) ghi.getNext(); |
| 106 | + assertEquals("http://example.com/three", three.getDestination()); |
| 107 | + assertEquals(Arrays.asList(SourceSpan.of(3, 4, 24)), |
| 108 | + three.getSourceSpans()); |
| 109 | + |
| 110 | + Text jkl = (Text) three.getNext(); |
| 111 | + assertEquals(" jkl", jkl.getLiteral()); |
| 112 | + assertEquals(Arrays.asList(SourceSpan.of(3, 28, 4)), |
| 113 | + jkl.getSourceSpans()); |
| 114 | + } |
| 115 | + |
56 | 116 | @Override |
57 | 117 | protected String render(String source) { |
58 | 118 | return RENDERER.render(PARSER.parse(source)); |
|
0 commit comments