File tree Expand file tree Collapse file tree 2 files changed +28
-9
lines changed
src/test/java/org/nibor/autolink Expand file tree Collapse file tree 2 files changed +28
-9
lines changed Original file line number Diff line number Diff line change @@ -53,16 +53,17 @@ Extracting links:
5353``` java
5454import org.nibor.autolink.* ;
5555
56- String input = " wow, so example: http ://test.com" ;
57- LinkExtractor linkExtractor = LinkExtractor . builder()
58- .linkTypes(EnumSet . of(LinkType . URL, LinkType . WWW , LinkType . EMAIL ))
56+ var input = " two links: https ://test.com and https://example .com" ;
57+ var linkExtractor = LinkExtractor . builder()
58+ .linkTypes(EnumSet . of(LinkType . URL)) // limit to URLs
5959 .build();
60- Iterable<LinkSpan > links = linkExtractor. extractLinks(input);
61- LinkSpan link = links. iterator(). next();
62- link. getType(); // LinkType.URL
63- link. getBeginIndex(); // 17
64- link. getEndIndex(); // 32
65- input. substring(link. getBeginIndex(), link. getEndIndex()); // "http://test.com"
60+ var links = new ArrayList<> ();
61+ for (var span : linkExtractor. extractLinks(input)) {
62+ var link = input. substring(span. getBeginIndex(), span. getEndIndex());
63+ links. add(link);
64+ }
65+
66+ links; // List.of("https://test.com", "https://example.com")
6667```
6768
6869Note that by default all supported types of links are extracted. If
Original file line number Diff line number Diff line change 22
33import org .junit .Test ;
44
5+ import java .util .ArrayList ;
56import java .util .EnumSet ;
7+ import java .util .List ;
8+ import java .util .stream .Stream ;
69
710import static org .junit .Assert .assertEquals ;
811
@@ -35,6 +38,21 @@ public void linkify() {
3538 assertEquals ("wow <a href=\" http://test.com\" >http://test.com</a> such linked" , sb .toString ());
3639 }
3740
41+ @ Test
42+ public void extractLinks () {
43+ var input = "hey https://test.com and https://example.com" ;
44+ var linkExtractor = LinkExtractor .builder ()
45+ .linkTypes (EnumSet .of (LinkType .URL ))
46+ .build ();
47+ var links = new ArrayList <>();
48+ for (var linkSpan : linkExtractor .extractLinks (input )) {
49+ var link = input .substring (linkSpan .getBeginIndex (), linkSpan .getEndIndex ());
50+ links .add (link );
51+ }
52+
53+ assertEquals (List .of ("https://test.com" , "https://example.com" ), links );
54+ }
55+
3856 // Mocked here to not have to depend on owasp-java-encoder in tests
3957 private static class Encode {
4058 public static String forHtmlAttribute (String text ) {
You can’t perform that action at this time.
0 commit comments