Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix the autolink reference for URLs with $

  • Loading branch information...
commit 725dd1e9b5035d35c5297ccbc7345b5af5df5260 2 parents 4276941 + eb71d10
Matt Sanford authored
View
18 README
@@ -0,0 +1,18 @@
+Java port of the twitter-text jandling libraries.
+
+== Copyright and License ==
+
+Copyright 2011 Twitter, Inc.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this work except in compliance with the License.
+ You may obtain a copy of the License in the LICENSE file, or at:
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
View
4 src/com/twitter/Autolink.java
@@ -177,9 +177,9 @@ public String autoLinkURLs(String text) {
matcher.appendReplacement(sb,
String.format("$%s<a href=\"%s\"%s>%s</a>",
Regex.VALID_URL_GROUP_BEFORE,
- url,
+ url.replaceAll("\\$", "\\\\\\$"),
noFollow ? NO_FOLLOW_HTML_ATTRIBUTE : "",
- url
+ url.replaceAll("\\$", "\\\\\\$")
));
continue;
}
View
12 src/com/twitter/Regex.java
@@ -28,6 +28,9 @@
private static final String URL_VALID_DOMAIN = URL_VALID_SUBDOMAIN + "*" + URL_VALID_DOMAIN_NAME + "\\.[a-z]{2,}(?::[0-9]+)?";
private static final String URL_VALID_GENERAL_PATH_CHARS = "[a-z0-9!\\*';:=\\+\\$/%#\\[\\]\\-_,~\\.\\|]";
+ private static final String URL_VALID_PATH_CHARS_WITHOUT_SLASH = "[" + URL_VALID_GENERAL_PATH_CHARS + "&&[^/]]";
+ private static final String URL_VALID_PATH_CHARS_WITHOUT_COMMA = "[" + URL_VALID_GENERAL_PATH_CHARS + "&&[^,]]";
+
/** Allow URL paths to contain balanced parens
* 1. Used in Wikipedia URLs like /Primer_(film)
* 2. Used in IIS sessions like /S(dfd346)/
@@ -35,8 +38,8 @@
private static final String URL_BALANCE_PARENS = "(?:\\(" + URL_VALID_GENERAL_PATH_CHARS + "+\\))";
private static final String URL_VALID_URL_PATH_CHARS = "(?:" +
URL_BALANCE_PARENS +
- "|@" + URL_VALID_GENERAL_PATH_CHARS + "+/" +
- "|[\\.,]?" + URL_VALID_GENERAL_PATH_CHARS + "+" +
+ "|@" + URL_VALID_PATH_CHARS_WITHOUT_SLASH + "++/" +
+ "|(?:[.,]*+" + URL_VALID_PATH_CHARS_WITHOUT_COMMA + ")++" +
")";
/** Valid end-of-path chracters (so /foo. does not gobble the period).
@@ -53,10 +56,7 @@
"(" + URL_VALID_DOMAIN + ")" + // $5 Domain(s) and optional port number
"(/" +
"(?:" +
- URL_VALID_URL_PATH_CHARS + "+" +
- URL_VALID_URL_PATH_ENDING_CHARS + "|" + // 1+ path chars and a valid last char
- URL_VALID_URL_PATH_CHARS + "+" +
- URL_VALID_URL_PATH_ENDING_CHARS + "?|" + // Optional last char to handle /@foo/ case
+ URL_VALID_URL_PATH_CHARS + "+|" + // 1+ path chars and a valid last char
URL_VALID_URL_PATH_ENDING_CHARS + // Just a # case
")?" +
")?" + // $6 URL Path and anchor
View
8 tests/com/twitter/AutolinkTest.java
@@ -33,6 +33,14 @@ public void testBlogspotWithDash() {
assertAutolink(expected, linker.autoLinkURLs(tweet));
}
+ /** See also: https://github.com/mzsanford/twitter-text-java/issues/8 */
+ public void testURLWithDollarThatLooksLikeARegex() {
+ linker.setNoFollow(false);
+ String tweet = "Url: http://example.com/$ABC";
+ String expected = "Url: <a href=\"http://example.com/$ABC\">http://example.com/$ABC</a>";
+ assertAutolink(expected, linker.autoLinkURLs(tweet));
+ }
+
protected void assertAutolink(String expected, String linked) {
assertEquals("Autolinked text should not equal the input", expected, linked);
}
View
14 tests/com/twitter/RegexTest.java
@@ -25,6 +25,20 @@ public void testValidURLDoesNotCrashOnLongPaths() {
assertTrue("Failed to correctly match a very long path", Regex.VALID_URL.matcher(longPathIsLong).find());
}
+ public void testValidUrlDoesNotTakeForeverOnRepeatedPuctuationAtEnd() {
+ String repeatedPath = "Try http://example.com/path**********************";
+
+ long start = System.currentTimeMillis();
+ Matcher matcher = Regex.VALID_URL.matcher(repeatedPath);
+ boolean isValid = matcher.find();
+ long end = System.currentTimeMillis();
+
+ assertTrue("Repeated puctuation should still produce a valid URL", isValid);
+
+ long duration = (end - start);
+ assertTrue("Matching a repeated path end should take less than 10ms (took " + duration + "ms)", (duration < 10) );
+ }
+
public void testExtractMentions() {
assertCaptureCount(3, Regex.EXTRACT_MENTIONS, "sample @user mention");
}
Please sign in to comment.
Something went wrong with that request. Please try again.