Skip to content

Commit 2ad2795

Browse files
committed
Stop URLs at " (#21)
1 parent 5b5d3fd commit 2ad2795

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
### Changed
10+
- Stop URLs when encountering an `"`. This is consistent with RFC 3986,
11+
and it seems unlikely that a user would have an unescaped `"` in a URL
12+
anyway, as browsers escape it when you copy an URL that contains one.
13+
814
## [0.8.0] - 2018-01-10
915
### Changed
1016
- Add `Automatic-Module-Name` manifest entry so that library can be used

src/main/java/org/nibor/autolink/internal/Scanners.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public static int findUrlEnd(CharSequence input, int beginIndex) {
6262
case '\u001E':
6363
case '\u001F':
6464
case ' ':
65+
case '\"':
6566
case '<':
6667
case '>':
6768
case '\u007F':
@@ -99,9 +100,9 @@ public static int findUrlEnd(CharSequence input, int beginIndex) {
99100
case '\u009F':
100101
// The above can never be part of an URL, so stop now. See RFC 3986 and RFC 3987.
101102
// Some characters are not in the above list, even they are not in "unreserved" or "reserved":
102-
// '"', '\\', '^', '`', '{', '|', '}'
103+
// '\\', '^', '`', '{', '|', '}'
103104
// The reason for this is that other link detectors also allow them. Also see below, we require
104-
// the quote and the braces to be balanced.
105+
// the braces to be balanced.
105106
case '\u00A0': // no-break space
106107
case '\u2000': // en quad
107108
case '\u2001': // em quad
@@ -175,12 +176,6 @@ public static int findUrlEnd(CharSequence input, int beginIndex) {
175176
break loop;
176177
}
177178
break;
178-
case '"':
179-
doubleQuote = !doubleQuote;
180-
if (!doubleQuote) {
181-
last = i;
182-
}
183-
break;
184179
case '\'':
185180
singleQuote = !singleQuote;
186181
if (!singleQuote) {

src/test/java/org/nibor/autolink/AutolinkUrlTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ public void matchingPunctuation() {
119119
assertLinked("http://example.org/a(b)", "|http://example.org/a(b)|");
120120
assertLinked("http://example.org/a[b]", "|http://example.org/a[b]|");
121121
assertLinked("http://example.org/a{b}", "|http://example.org/a{b}|");
122-
assertLinked("http://example.org/a\"b\"", "|http://example.org/a\"b\"|");
123122
assertLinked("http://example.org/a'b'", "|http://example.org/a'b'|");
124123
assertLinked("(http://example.org/)", "(|http://example.org/|)");
125124
assertLinked("[http://example.org/]", "[|http://example.org/|]");
@@ -145,13 +144,15 @@ public void matchingPunctuationTricky() {
145144

146145
@Test
147146
public void quotes() {
148-
assertLinked("http://example.org/\"_(foo)", "|http://example.org/\"_(foo)|");
149-
assertLinked("http://example.org/\"_(foo)\"", "|http://example.org/\"_(foo)\"|");
150-
assertLinked("http://example.org/\"\"", "|http://example.org/\"\"|");
151-
assertLinked("http://example.org/\"\"\"", "|http://example.org/\"\"|\"");
152-
assertLinked("http://example.org/\".", "|http://example.org/|\".");
153-
assertLinked("http://example.org/\"a", "|http://example.org/\"a|");
147+
assertLinked("http://example.org/\'_(foo)", "|http://example.org/\'_(foo)|");
148+
assertLinked("http://example.org/\'_(foo)\'", "|http://example.org/\'_(foo)\'|");
149+
assertLinked("http://example.org/\'\'", "|http://example.org/\'\'|");
150+
assertLinked("http://example.org/\'\'\'", "|http://example.org/\'\'|\'");
151+
assertLinked("http://example.org/\'.", "|http://example.org/|\'.");
154152
assertLinked("http://example.org/it's", "|http://example.org/it's|");
153+
// " not allowed in URLs
154+
assertLinked("http://example.org/\"a", "|http://example.org/|\"a");
155+
assertLinked("http://example.org/\"a\"", "|http://example.org/|\"a\"");
155156
}
156157

157158
@Test

0 commit comments

Comments
 (0)