Merge pull request #2392 from itiboi/generic-uri-parsing

Linkifying framework
This commit is contained in:
cketti 2017-03-22 02:22:04 +01:00 committed by GitHub
commit 32212a4e23
10 changed files with 898 additions and 111 deletions

View file

@ -0,0 +1,29 @@
package com.fsck.k9.message.html;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class BitcoinUriParser implements UriParser {
private static final Pattern BITCOIN_URI_PATTERN =
Pattern.compile("bitcoin:[1-9a-km-zA-HJ-NP-Z]{27,34}(\\?[a-zA-Z0-9$\\-_.+!*'(),%:@&=]*)?");
@Override
public int linkifyUri(String text, int startPos, StringBuffer outputBuffer) {
Matcher matcher = BITCOIN_URI_PATTERN.matcher(text);
if (!matcher.find(startPos) || matcher.start() != startPos) {
return startPos;
}
String bitcoinUri = matcher.group();
outputBuffer.append("<a href=\"")
.append(bitcoinUri)
.append("\">")
.append(bitcoinUri)
.append("</a>");
return matcher.end();
}
}

View file

@ -1,85 +1,30 @@
package com.fsck.k9.message.html; package com.fsck.k9.message.html;
import android.text.*;
import android.text.Html.TagHandler;
import com.fsck.k9.K9;
import org.xml.sax.XMLReader;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import android.text.Annotation;
import android.text.Editable;
import android.text.Html;
import android.text.Html.TagHandler;
import android.text.Spannable;
import android.text.Spanned;
import android.text.TextUtils;
import com.fsck.k9.K9;
import org.xml.sax.XMLReader;
/** /**
* Contains common routines to convert html to text and vice versa. * Contains common routines to convert html to text and vice versa.
*/ */
public class HtmlConverter { public class HtmlConverter {
/* This comprises most common used Unicode characters allowed in IRI
* as detailed in RFC 3987.
* Specifically, those two byte Unicode characters are not included.
*/
private static final String GOOD_IRI_CHAR =
"a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF";
/**
* Regular expression to match all IANA top-level domains for WEB_URL.
* List accurate as of 2011/01/12. List taken from:
* http://data.iana.org/TLD/tlds-alpha-by-domain.txt
* This pattern is auto-generated by frameworks/base/common/tools/make-iana-tld-pattern.py
*/
private static final String TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL =
"(?:"
+ "(?:aaa|aarp|abarth|abb|abbott|abbvie|abc|able|abogado|abudhabi|academy|accenture|accountant|accountants|aco|active|actor|adac|ads|adult|aeg|aero|aetna|afamilycompany|afl|agakhan|agency|aig|aigo|airbus|airforce|airtel|akdn|alfaromeo|alibaba|alipay|allfinanz|allstate|ally|alsace|alstom|americanexpress|americanfamily|amex|amfam|amica|amsterdam|analytics|android|anquan|anz|apartments|app|apple|aquarelle|aramco|archi|army|arpa|art|arte|asda|asia|associates|athleta|attorney|auction|audi|audible|audio|auspost|author|auto|autos|avianca|aws|axa|azure|a[cdefgilmoqrstuwxz])"
+ "|(?:baby|baidu|banamex|bananarepublic|band|bank|bar|barcelona|barclaycard|barclays|barefoot|bargains|bauhaus|bayern|bbc|bbt|bbva|bcg|bcn|beats|beauty|beer|bentley|berlin|best|bestbuy|bet|bharti|bible|bid|bike|bing|bingo|bio|biz|black|blackfriday|blanco|blockbuster|blog|bloomberg|blue|bms|bmw|bnl|bnpparibas|boats|boehringer|bofa|bom|bond|boo|book|booking|boots|bosch|bostik|bot|boutique|bradesco|bridgestone|broadway|broker|brother|brussels|budapest|bugatti|build|builders|business|buy|buzz|bzh|b[abdefghijmnorstvwyz])"
+ "|(?:cab|cafe|cal|call|calvinklein|cam|camera|camp|cancerresearch|canon|capetown|capital|capitalone|car|caravan|cards|care|career|careers|cars|cartier|casa|cash|casino|cat|catering|cba|cbn|cbre|cbs|ceb|center|ceo|cern|cfa|cfd|chanel|channel|chase|chat|cheap|chintai|chloe|christmas|chrome|chrysler|church|cipriani|circle|cisco|citadel|citi|citic|city|cityeats|claims|cleaning|click|clinic|clinique|clothing|cloud|club|clubmed|coach|codes|coffee|college|cologne|com|comcast|commbank|community|company|compare|computer|comsec|condos|construction|consulting|contact|contractors|cooking|cookingchannel|cool|coop|corsica|country|coupon|coupons|courses|credit|creditcard|creditunion|cricket|crown|crs|cruises|csc|cuisinella|cymru|cyou|c[acdfghiklmnoruvwxyz])"
+ "|(?:dabur|dad|dance|date|dating|datsun|day|dclk|dds|deal|dealer|deals|degree|delivery|dell|deloitte|delta|democrat|dental|dentist|desi|design|dev|dhl|diamonds|diet|digital|direct|directory|discount|discover|dish|diy|dnp|docs|doctor|dodge|dog|doha|domains|dot|download|drive|dtv|dubai|duck|dunlop|duns|dupont|durban|dvag|d[ejkmoz])"
+ "|(?:earth|eat|eco|edeka|edu|education|email|emerck|energy|engineer|engineering|enterprises|epost|epson|equipment|ericsson|erni|esq|estate|esurance|eurovision|eus|events|everbank|exchange|expert|exposed|express|extraspace|e[cegrstu])"
+ "|(?:fage|fail|fairwinds|faith|family|fan|fans|farm|farmers|fashion|fast|fedex|feedback|ferrari|ferrero|fiat|fidelity|film|final|finance|financial|fire|firestone|firmdale|fish|fishing|fit|fitness|flickr|flights|flir|florist|flowers|fly|foo|foodnetwork|football|ford|forex|forsale|forum|foundation|fox|fresenius|frl|frogans|frontdoor|frontier|ftr|fujitsu|fujixerox|fund|furniture|futbol|fyi|f[ijkmor])"
+ "|(?:gal|gallery|gallo|gallup|game|games|gap|garden|gbiz|gdn|gea|gent|genting|george|ggee|gift|gifts|gives|giving|glade|glass|gle|global|globo|gmail|gmbh|gmo|gmx|godaddy|gold|goldpoint|golf|goo|goodhands|goodyear|goog|google|gop|got|gov|grainger|graphics|gratis|green|gripe|group|guardian|gucci|guge|guide|guitars|guru|g[abdefghilmnpqrstuwy])"
+ "|(?:hamburg|hangout|haus|hbo|hdfc|hdfcbank|health|healthcare|help|helsinki|here|hermes|hgtv|hiphop|hisamitsu|hitachi|hiv|hkt|hockey|holdings|holiday|homedepot|homegoods|homes|homesense|honda|honeywell|horse|host|hosting|hot|hoteles|hotmail|house|how|hsbc|htc|hughes|hyatt|hyundai|h[kmnrtu])"
+ "|(?:ibm|icbc|ice|icu|ieee|ifm|iinet|ikano|imamat|imdb|immo|immobilien|industries|infiniti|info|ing|ink|institute|insurance|insure|int|intel|international|intuit|investments|ipiranga|irish|iselect|ismaili|ist|istanbul|itau|itv|iwc|i[delmnoqrst])"
+ "|(?:jaguar|java|jcb|jcp|jeep|jetzt|jewelry|jlc|jll|jmp|jnj|jobs|joburg|jot|joy|jpmorgan|jprs|juegos|juniper|j[emop])"
+ "|(?:kaufen|kddi|kerryhotels|kerrylogistics|kerryproperties|kfh|kia|kim|kinder|kindle|kitchen|kiwi|koeln|komatsu|kosher|kpmg|kpn|krd|kred|kuokgroup|kyoto|k[eghimnprwyz])"
+ "|(?:lacaixa|ladbrokes|lamborghini|lamer|lancaster|lancia|lancome|land|landrover|lanxess|lasalle|lat|latino|latrobe|law|lawyer|lds|lease|leclerc|lefrak|legal|lego|lexus|lgbt|liaison|lidl|life|lifeinsurance|lifestyle|lighting|like|lilly|limited|limo|lincoln|linde|link|lipsy|live|living|lixil|loan|loans|locker|locus|loft|lol|london|lotte|lotto|love|lpl|lplfinancial|ltd|ltda|lundbeck|lupin|luxe|luxury|l[abcikrstuvy])"
+ "|(?:macys|madrid|maif|maison|makeup|man|management|mango|market|marketing|markets|marriott|marshalls|maserati|mattel|mba|mcd|mcdonalds|mckinsey|med|media|meet|melbourne|meme|memorial|men|menu|meo|metlife|miami|microsoft|mil|mini|mint|mit|mitsubishi|mlb|mls|mma|mobi|mobily|moda|moe|moi|mom|monash|money|monster|montblanc|mopar|mormon|mortgage|moscow|motorcycles|mov|movie|movistar|msd|mtn|mtpc|mtr|museum|mutual|mutuelle|m[acdeghklmnopqrstuvwxyz])"
+ "|(?:nab|nadex|nagoya|name|nationwide|natura|navy|nba|nec|net|netbank|netflix|network|neustar|new|news|next|nextdirect|nexus|nfl|ngo|nhk|nico|nike|nikon|ninja|nissan|nissay|nokia|northwesternmutual|norton|now|nowruz|nowtv|nra|nrw|ntt|nyc|n[acefgilopruz])"
+ "|(?:obi|off|office|okinawa|olayan|olayangroup|oldnavy|ollo|omega|one|ong|onl|online|onyourside|ooo|open|oracle|orange|org|organic|orientexpress|origins|osaka|otsuka|ott|ovh|om)"
+ "|(?:page|pamperedchef|panasonic|panerai|paris|pars|partners|parts|party|passagens|pay|pccw|pet|pfizer|pharmacy|philips|photo|photography|photos|physio|piaget|pics|pictet|pictures|pid|pin|ping|pink|pioneer|pizza|place|play|playstation|plumbing|plus|pnc|pohl|poker|politie|porn|post|pramerica|praxi|press|prime|pro|prod|productions|prof|progressive|promo|properties|property|protection|pru|prudential|pub|pwc|p[aefghklmnrstwy])"
+ "|(?:qpon|quebec|quest|qvc|qa)"
+ "|(?:racing|raid|read|realestate|realtor|realty|recipes|red|redstone|redumbrella|rehab|reise|reisen|reit|ren|rent|rentals|repair|report|republican|rest|restaurant|review|reviews|rexroth|rich|richardli|ricoh|rightathome|rio|rip|rocher|rocks|rodeo|room|rsvp|ruhr|run|rwe|ryukyu|r[eosuw])"
+ "|(?:saarland|safe|safety|sakura|sale|salon|samsclub|samsung|sandvik|sandvikcoromant|sanofi|sap|sapo|sarl|sas|save|saxo|sbi|sbs|sca|scb|schaeffler|schmidt|scholarships|school|schule|schwarz|science|scjohnson|scor|scot|seat|secure|security|seek|select|sener|services|ses|seven|sew|sex|sexy|sfr|shangrila|sharp|shaw|shell|shia|shiksha|shoes|shop|shopping|shouji|show|showtime|shriram|silk|sina|singles|site|ski|skin|sky|skype|sling|smart|smile|sncf|soccer|social|softbank|software|sohu|solar|solutions|song|sony|soy|space|spiegel|spot|spreadbetting|srl|srt|stada|staples|star|starhub|statebank|statefarm|statoil|stc|stcgroup|stockholm|storage|store|stream|studio|study|style|sucks|supplies|supply|support|surf|surgery|suzuki|swatch|swiftcover|swiss|sydney|symantec|systems|s[abcdeghijklmnortuvxyz])"
+ "|(?:tab|taipei|talk|taobao|target|tatamotors|tatar|tattoo|tax|taxi|tci|tdk|team|tech|technology|tel|telecity|telefonica|temasek|tennis|teva|thd|theater|theatre|tiaa|tickets|tienda|tiffany|tips|tires|tirol|tjmaxx|tjx|tkmaxx|tmall|today|tokyo|tools|top|toray|toshiba|total|tours|town|toyota|toys|trade|trading|training|travel|travelchannel|travelers|travelersinsurance|trust|trv|tube|tui|tunes|tushu|tvs|t[cdfghjklmnortvwz])"
+ "|(?:ubank|ubs|uconnect|unicom|university|uno|uol|ups|u[agksyz])"
+ "|(?:vacations|vana|vanguard|vegas|ventures|verisign|versicherung|vet|viajes|video|vig|viking|villas|vin|vip|virgin|visa|vision|vista|vistaprint|viva|vivo|vlaanderen|vodka|volkswagen|vote|voting|voto|voyage|vuelos|v[aceginu])"
+ "|(?:wales|walmart|walter|wang|wanggou|warman|watch|watches|weather|weatherchannel|webcam|weber|website|wed|wedding|weibo|weir|whoswho|wien|wiki|williamhill|win|windows|wine|winners|wme|wolterskluwer|woodside|work|works|world|wtc|wtf|w[fs])"
+ "|(?:xbox|xerox|xfinity|xihuan|xin|xn\\-\\-11b4c3d|xn\\-\\-1ck2e1b|xn\\-\\-1qqw23a|xn\\-\\-30rr7y|xn\\-\\-3bst00m|xn\\-\\-3ds443g|xn\\-\\-3e0b707e|xn\\-\\-3oq18vl8pn36a|xn\\-\\-3pxu8k|xn\\-\\-42c2d9a|xn\\-\\-45brj9c|xn\\-\\-45q11c|xn\\-\\-4gbrim|xn\\-\\-55qw42g|xn\\-\\-55qx5d|xn\\-\\-5su34j936bgsg|xn\\-\\-5tzm5g|xn\\-\\-6frz82g|xn\\-\\-6qq986b3xl|xn\\-\\-80adxhks|xn\\-\\-80ao21a|xn\\-\\-80asehdb|xn\\-\\-80aswg|xn\\-\\-8y0a063a|xn\\-\\-90a3ac|xn\\-\\-90ae|xn\\-\\-90ais|xn\\-\\-9dbq2a|xn\\-\\-9et52u|xn\\-\\-9krt00a|xn\\-\\-b4w605ferd|xn\\-\\-bck1b9a5dre4c|xn\\-\\-c1avg|xn\\-\\-c2br7g|xn\\-\\-cck2b3b|xn\\-\\-cg4bki|xn\\-\\-clchc0ea0b2g2a9gcd|xn\\-\\-czr694b|xn\\-\\-czrs0t|xn\\-\\-czru2d|xn\\-\\-d1acj3b|xn\\-\\-d1alf|xn\\-\\-e1a4c|xn\\-\\-eckvdtc9d|xn\\-\\-efvy88h|xn\\-\\-estv75g|xn\\-\\-fct429k|xn\\-\\-fhbei|xn\\-\\-fiq228c5hs|xn\\-\\-fiq64b|xn\\-\\-fiqs8s|xn\\-\\-fiqz9s|xn\\-\\-fjq720a|xn\\-\\-flw351e|xn\\-\\-fpcrj9c3d|xn\\-\\-fzc2c9e2c|xn\\-\\-fzys8d69uvgm|xn\\-\\-g2xx48c|xn\\-\\-gckr3f0f|xn\\-\\-gecrj9c|xn\\-\\-h2brj9c|xn\\-\\-hxt814e|xn\\-\\-i1b6b1a6a2e|xn\\-\\-imr513n|xn\\-\\-io0a7i|xn\\-\\-j1aef|xn\\-\\-j1amh|xn\\-\\-j6w193g|xn\\-\\-jlq61u9w7b|xn\\-\\-jvr189m|xn\\-\\-kcrx77d1x4a|xn\\-\\-kprw13d|xn\\-\\-kpry57d|xn\\-\\-kpu716f|xn\\-\\-kput3i|xn\\-\\-l1acc|xn\\-\\-lgbbat1ad8j|xn\\-\\-mgb9awbf|xn\\-\\-mgba3a3ejt|xn\\-\\-mgba3a4f16a|xn\\-\\-mgba7c0bbn0a|xn\\-\\-mgbaam7a8h|xn\\-\\-mgbab2bd|xn\\-\\-mgbayh7gpa|xn\\-\\-mgbb9fbpob|xn\\-\\-mgbbh1a71e|xn\\-\\-mgbc0a9azcg|xn\\-\\-mgbca7dzdo|xn\\-\\-mgberp4a5d4ar|xn\\-\\-mgbpl2fh|xn\\-\\-mgbt3dhd|xn\\-\\-mgbtx2b|xn\\-\\-mgbx4cd0ab|xn\\-\\-mix891f|xn\\-\\-mk1bu44c|xn\\-\\-mxtq1m|xn\\-\\-ngbc5azd|xn\\-\\-ngbe9e0a|xn\\-\\-node|xn\\-\\-nqv7f|xn\\-\\-nqv7fs00ema|xn\\-\\-nyqy26a|xn\\-\\-o3cw4h|xn\\-\\-ogbpf8fl|xn\\-\\-p1acf|xn\\-\\-p1ai|xn\\-\\-pbt977c|xn\\-\\-pgbs0dh|xn\\-\\-pssy2u|xn\\-\\-q9jyb4c|xn\\-\\-qcka1pmc|xn\\-\\-qxam|xn\\-\\-rhqv96g|xn\\-\\-rovu88b|xn\\-\\-s9brj9c|xn\\-\\-ses554g|xn\\-\\-t60b56a|xn\\-\\-tckwe|xn\\-\\-unup4y|xn\\-\\-vermgensberater\\-ctb|xn\\-\\-vermgensberatung\\-pwb|xn\\-\\-vhquv|xn\\-\\-vuq861b|xn\\-\\-w4r85el8fhu5dnra|xn\\-\\-w4rs40l|xn\\-\\-wgbh1c|xn\\-\\-wgbl6a|xn\\-\\-xhq521b|xn\\-\\-xkc2al3hye2a|xn\\-\\-xkc2dl3a5ee0h|xn\\-\\-y9a3aq|xn\\-\\-yfro4i67o|xn\\-\\-ygbi2ammx|xn\\-\\-zfr164b|xperia|xxx|xyz)"
+ "|(?:yachts|yahoo|yamaxun|yandex|yodobashi|yoga|yokohama|you|youtube|yun|y[et])"
+ "|(?:zappos|zara|zero|zip|zippo|zone|zuerich|z[amw])))";
private static final String BITCOIN_URI_PATTERN =
"bitcoin:[1-9a-km-zA-HJ-NP-Z]{27,34}(\\?[a-zA-Z0-9$\\-_.+!*'(),%:@&=]*)?";
/**
* Regular expression pattern to match most part of RFC 3987
* Internationalized URLs, aka IRIs. Commonly used Unicode characters are
* added.
*/
private static final Pattern WEB_URL_PATTERN = Pattern.compile(
"((?:(http|https|Http|Https|rtsp|Rtsp):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)"
+ "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_"
+ "\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?"
+ "((?:(?:[" + GOOD_IRI_CHAR + "][" + GOOD_IRI_CHAR + "\\-]{0,64}\\.)+" // named host
+ TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL
+ "|(?:(?:25[0-5]|2[0-4]" // or ip address
+ "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]"
+ "|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]"
+ "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
+ "|[1-9][0-9]|[0-9])))"
+ "(?:\\:\\d{1,5})?)" // plus option port number
+ "(\\/(?:(?:[" + GOOD_IRI_CHAR + "\\;\\/\\?\\:\\@\\&\\=\\#\\~" // plus option query params
+ "\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?"
+ "(?:\\b|$)"); // and finally, a word boundary or end of
/** /**
* When generating previews, Spannable objects that can't be converted into a String are * When generating previews, Spannable objects that can't be converted into a String are
* represented as 0xfffc. When displayed, these show up as undisplayed squares. These constants * represented as 0xfffc. When displayed, these show up as undisplayed squares. These constants
@ -339,7 +284,7 @@ public class HtmlConverter {
StringBuffer sb = new StringBuffer(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH); StringBuffer sb = new StringBuffer(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH);
sb.append(htmlifyMessageHeader()); sb.append(htmlifyMessageHeader());
linkifyText(text, sb); UriLinkifier.linkifyText(text, sb);
sb.append(htmlifyMessageFooter()); sb.append(htmlifyMessageFooter());
text = sb.toString(); text = sb.toString();
@ -428,32 +373,6 @@ public class HtmlConverter {
} }
} }
/**
* Searches for link-like text in a string and turn it into a link. Append the result to
* <tt>outputBuffer</tt>. <tt>text</tt> is not modified.
* @param text Plain text to be linkified.
* @param outputBuffer Buffer to append linked text to.
*/
protected static void linkifyText(final String text, final StringBuffer outputBuffer) {
String prepared = text.replaceAll(BITCOIN_URI_PATTERN, "<a href=\"$0\">$0</a>");
Matcher m = WEB_URL_PATTERN.matcher(prepared);
while (m.find()) {
int start = m.start();
if (start == 0 || (start != 0 && prepared.charAt(start - 1) != '@')) {
if (m.group().indexOf(':') > 0) { // With no URI-schema we may get "http:/" links with the second / missing
m.appendReplacement(outputBuffer, "<a href=\"$0\">$0</a>");
} else {
m.appendReplacement(outputBuffer, "<a href=\"http://$0\">$0</a>");
}
} else {
m.appendReplacement(outputBuffer, "$0");
}
}
m.appendTail(outputBuffer);
}
/* /*
* Lightweight method to check whether the message contains emoji or not. * Lightweight method to check whether the message contains emoji or not.
* Useful to avoid calling the heavyweight convertEmoji2Img method. * Useful to avoid calling the heavyweight convertEmoji2Img method.
@ -1384,7 +1303,7 @@ public class HtmlConverter {
// Linkify the message. // Linkify the message.
StringBuffer linkified = new StringBuffer(htmlified.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH); StringBuffer linkified = new StringBuffer(htmlified.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH);
linkifyText(htmlified, linkified); UriLinkifier.linkifyText(htmlified, linkified);
// Add newlines and unescaping. // Add newlines and unescaping.
// //

View file

@ -0,0 +1,302 @@
package com.fsck.k9.message.html;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Parses and "linkifies" http links.
* <p>
* This class is in parts inspired by OkHttp's
* <a href="https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/HttpUrl.java">HttpUrl</a>.
* But much of the parsing parts have been left out.
* </p>
*/
class HttpUriParser implements UriParser {
// This string represent character group sub-delim as described in RFC 3986
private static final String SUB_DELIM = "!$&'()*+,;=";
private static final Pattern DOMAIN_PATTERN =
Pattern.compile("[\\da-z](?:[\\da-z-]*[\\da-z])*(?:\\.[\\da-z](?:[\\da-z-]*[\\da-z])*)*(?::(\\d{0,5}))?",
Pattern.CASE_INSENSITIVE);
private static final Pattern IPv4_PATTERN =
Pattern.compile("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})(:(\\d{0,5}))?");
@Override
public int linkifyUri(String text, int startPos, StringBuffer outputBuffer) {
int currentPos = startPos;
// Scheme
String shortScheme = text.substring(currentPos, Math.min(currentPos + 7, text.length()));
String longScheme = text.substring(currentPos, Math.min(currentPos + 8, text.length()));
if (shortScheme.equalsIgnoreCase("https://")) {
currentPos += "https://".length();
} else if (shortScheme.equalsIgnoreCase("http://")) {
currentPos += "http://".length();
} else if (longScheme.equalsIgnoreCase("rtsp://")) {
currentPos += "rtsp://".length();
} else {
return startPos;
}
// Authority
int matchedAuthorityEnd = tryMatchAuthority(text, currentPos);
if (matchedAuthorityEnd == currentPos) {
return startPos;
}
currentPos = matchedAuthorityEnd;
// Path
if (currentPos < text.length() && text.charAt(currentPos) == '/') {
currentPos = matchUnreservedPCTEncodedSubDelimClassesGreedy(text, currentPos + 1, "/:@");
}
// Query
if (currentPos < text.length() && text.charAt(currentPos) == '?') {
currentPos = matchUnreservedPCTEncodedSubDelimClassesGreedy(text, currentPos + 1, ":@/?");
}
// Fragment
if (currentPos < text.length() && text.charAt(currentPos) == '#') {
currentPos = matchUnreservedPCTEncodedSubDelimClassesGreedy(text, currentPos + 1, ":@/?");
}
String httpUri = text.substring(startPos, currentPos);
outputBuffer.append("<a href=\"")
.append(httpUri)
.append("\">")
.append(httpUri)
.append("</a>");
return currentPos;
}
private int tryMatchAuthority(String text, int startPos) {
int authorityLimit = text.indexOf('/', startPos);
if (authorityLimit == -1) {
authorityLimit = text.length();
}
int authorityStart = tryMatchUserInfo(text, startPos, authorityLimit);
int authorityEnd = tryMatchDomainName(text, authorityStart);
if (authorityEnd != authorityStart) {
return authorityEnd;
}
authorityEnd = tryMatchIpv4Address(text, authorityStart, true);
if (authorityEnd != authorityStart) {
return authorityEnd;
}
authorityEnd = tryMatchIpv6Address(text, authorityStart);
if (authorityEnd != authorityStart) {
return authorityEnd;
}
return startPos;
}
private int tryMatchUserInfo(String text, int startPos, int limit) {
int userInfoEnd = text.indexOf('@', startPos);
if (userInfoEnd != -1 && userInfoEnd < limit) {
if (matchUnreservedPCTEncodedSubDelimClassesGreedy(text, startPos, ":") != userInfoEnd) {
// Illegal character in user info
return startPos;
}
return userInfoEnd + 1;
}
return startPos;
}
private int tryMatchDomainName(String text, int startPos) {
try {
Matcher matcher = DOMAIN_PATTERN.matcher(text);
if (!matcher.find(startPos) || matcher.start() != startPos) {
return startPos;
}
String portString = matcher.group(1);
if (portString != null && !portString.isEmpty()) {
int port = Integer.parseInt(portString);
if (port > 65535) {
return startPos;
}
}
return matcher.end();
} catch (IllegalArgumentException e) {
return startPos;
}
}
private int tryMatchIpv4Address(String text, int startPos, boolean portAllowed) {
Matcher matcher = IPv4_PATTERN.matcher(text);
if (!matcher.find(startPos) || matcher.start() != startPos) {
return startPos;
}
for (int i = 1; i <= 4; i++) {
int segment = Integer.parseInt(matcher.group(1));
if (segment > 255) {
return startPos;
}
}
if (!portAllowed && matcher.group(5) != null) {
return startPos;
}
String portString = matcher.group(6);
if (portString != null && !portString.isEmpty()) {
int port = Integer.parseInt(portString);
if (port > 65535) {
return startPos;
}
}
return matcher.end();
}
private int tryMatchIpv6Address(String text, int startPos) {
if (startPos == text.length() || text.codePointAt(startPos) != '[') {
return startPos;
}
int addressEnd = text.indexOf(']');
if (addressEnd == -1) {
return startPos;
}
// Actual parsing
int currentPos = startPos + 1;
int beginSegmentsCount = 0;
int endSegmentsCount = 0;
// Handle :: separator and segments in front of it
int compressionPos = text.indexOf("::");
boolean compressionEnabled = compressionPos != -1 && compressionPos < addressEnd;
if (compressionEnabled) {
while (currentPos < compressionPos) {
// Check segment separator
if (beginSegmentsCount > 0) {
if (text.codePointAt(currentPos) != ':') {
return startPos;
} else {
++currentPos;
}
}
// Parse segment
int possibleSegmentEnd =
parse16BitHexSegment(text, currentPos, Math.min(currentPos + 4, compressionPos));
if (possibleSegmentEnd == currentPos) {
return startPos;
}
currentPos = possibleSegmentEnd;
++beginSegmentsCount;
}
currentPos += 2; // Skip :: separator
}
// Parse end segments
while (currentPos < addressEnd && (beginSegmentsCount + endSegmentsCount) < 8) {
// Check segment separator
if (endSegmentsCount > 0) {
if (text.codePointAt(currentPos) != ':') {
return startPos;
} else {
++currentPos;
}
}
// Small look ahead, do not run into IPv4 tail (7 is IPv4 minimum length)
int nextColon = text.indexOf(':', currentPos);
if ((nextColon == -1 || nextColon > addressEnd) && (addressEnd - currentPos) >= 7) {
break;
}
// Parse segment
int possibleSegmentEnd = parse16BitHexSegment(text, currentPos, Math.min(currentPos + 4, addressEnd));
if (possibleSegmentEnd == currentPos) {
return startPos;
}
currentPos = possibleSegmentEnd;
++endSegmentsCount;
}
// We have 3 valid cases here
if (currentPos == addressEnd) {
// 1) No compression and full address, everything fine
// 2) Compression enabled and whole address parsed, everything fine as well
if ((!compressionEnabled && beginSegmentsCount + endSegmentsCount == 8) ||
(compressionEnabled && beginSegmentsCount + endSegmentsCount < 8)) {
// Only optional port left, skip address bracket
++currentPos;
} else {
return startPos;
}
} else {
// 3) Still some stuff missing, check for IPv4 as tail necessary
if (tryMatchIpv4Address(text, currentPos, false) != addressEnd) {
return startPos;
}
currentPos = addressEnd + 1;
}
// Check optional port
if (currentPos == text.length() || text.codePointAt(currentPos) != ':') {
return currentPos;
}
++currentPos;
int port = 0;
for (; currentPos < text.length(); currentPos++) {
int c = text.codePointAt(currentPos);
if (c < '0' || c > '9') {
break;
}
port = port * 10 + c - '0';
}
return (port <= 65535) ? currentPos : startPos;
}
private int parse16BitHexSegment(String text, int startPos, int endPos) {
int currentPos = startPos;
while (isHexDigit(text.codePointAt(currentPos)) && currentPos < endPos) {
++currentPos;
}
return currentPos;
}
private int matchUnreservedPCTEncodedSubDelimClassesGreedy(String text, int startPos, String additionalCharacters) {
String allowedCharacters = SUB_DELIM + "-._~" + additionalCharacters;
int currentPos;
int shouldBeHex = 0;
for (currentPos = startPos; currentPos < text.length(); currentPos++) {
int c = text.codePointAt(currentPos);
if (isHexDigit(c)) {
shouldBeHex = Math.max(shouldBeHex - 1, 0);
} else if (shouldBeHex == 0) {
if (allowedCharacters.indexOf(c) != -1) {
// Everything ok here :)
} else if (c == '%') {
shouldBeHex = 2;
} else {
break;
}
} else {
break;
}
}
return currentPos;
}
private boolean isHexDigit(int c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
}
}

View file

@ -0,0 +1,63 @@
package com.fsck.k9.message.html;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.text.TextUtils;
public class UriLinkifier {
private static final Pattern URI_SCHEME;
private static final Map<String, UriParser> SUPPORTED_URIS;
private static final String SCHEME_SEPARATORS = " (\\n";
private static final String ALLOWED_SEPARATORS_PATTERN = "(?:^|[" + SCHEME_SEPARATORS + "])";
static {
SUPPORTED_URIS = new HashMap<>();
SUPPORTED_URIS.put("bitcoin:", new BitcoinUriParser());
UriParser httpParser = new HttpUriParser();
SUPPORTED_URIS.put("http:", httpParser);
SUPPORTED_URIS.put("https:", httpParser);
SUPPORTED_URIS.put("rtsp:", httpParser);
String allSchemes = TextUtils.join("|", SUPPORTED_URIS.keySet());
String pattern = ALLOWED_SEPARATORS_PATTERN + "(" + allSchemes + ")";
URI_SCHEME = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
}
public static void linkifyText(String text, StringBuffer outputBuffer) {
int currentPos = 0;
Matcher matcher = URI_SCHEME.matcher(text);
while (matcher.find(currentPos)) {
int startPos = matcher.start(1);
String textBeforeMatch = text.substring(currentPos, startPos);
outputBuffer.append(textBeforeMatch);
String scheme = matcher.group(1).toLowerCase(Locale.US);
UriParser parser = SUPPORTED_URIS.get(scheme);
int newPos = parser.linkifyUri(text, startPos, outputBuffer);
boolean uriWasNotLinkified = newPos <= startPos;
if (uriWasNotLinkified) {
outputBuffer.append(text.charAt(startPos));
currentPos = startPos + 1;
} else {
currentPos = (newPos > currentPos) ? newPos : currentPos + 1;
}
if (currentPos >= text.length()) {
break;
}
}
String textAfterLastMatch = text.substring(currentPos);
outputBuffer.append(textAfterLastMatch);
}
}

View file

@ -0,0 +1,19 @@
package com.fsck.k9.message.html;
public interface UriParser {
/**
* Parse and linkify scheme specific URI beginning from given position. The result will be written to given buffer.
*
* @param text
* String to parse URI from.
* @param startPos
* Position where URI starts (first letter of scheme).
* @param outputBuffer
* Buffer where linkified variant of URI is written to.
*
* @return Index where parsed URI ends (first non-URI letter). Should be {@code startPos} or smaller if no valid
* URI was found.
*/
int linkifyUri(String text, int startPos, StringBuffer outputBuffer);
}

View file

@ -0,0 +1,84 @@
package com.fsck.k9.message.html;
import org.junit.Test;
import static com.fsck.k9.message.html.UriParserTestHelper.assertLinkOnly;
import static org.junit.Assert.assertEquals;
public class BitcoinUriParserTest {
BitcoinUriParser parser = new BitcoinUriParser();
StringBuffer outputBuffer = new StringBuffer();
@Test
public void basicBitcoinUri() throws Exception {
assertLinkify("bitcoin:19W6QZkx8SYPG7BBCS7odmWGRxqRph5jFU");
}
@Test
public void bitcoinUriWithAmount() throws Exception {
assertLinkify("bitcoin:12A1MyfXbW6RhdRAZEqofac5jCQQjwEPBu?amount=1.2");
}
@Test
public void bitcoinUriWithQueryParameters() throws Exception {
assertLinkify("bitcoin:12A1MyfXbW6RhdRAZEqofac5jCQQjwEPBu?amount=1.2" +
"&message=Payment&label=Satoshi&extra=other-param");
}
@Test
public void uriInMiddleOfInput() throws Exception {
String prefix = "prefix ";
String uri = "bitcoin:12A1MyfXbW6RhdRAZEqofac5jCQQjwEPBu?amount=1.2";
String text = prefix + uri;
parser.linkifyUri(text, prefix.length(), outputBuffer);
assertLinkOnly(uri, outputBuffer);
}
@Test
public void invalidScheme() throws Exception {
assertNotLinkify("bitcion:19W6QZkx8SYPG7BBCS7odmWGRxqRph5jFU");
}
@Test
public void invalidAddress() throws Exception {
assertNotLinkify("bitcoin:[invalid]");
}
@Test
public void invalidBitcoinUri_shouldReturnStartingPosition() throws Exception {
String uri = "bitcoin:[invalid]";
int newPos = linkify(uri);
assertEquals(0, newPos);
}
@Test
public void invalidBitcoinUri_shouldNotWriteToOutputBuffer() throws Exception {
String uri = "bitcoin:[invalid]";
linkify(uri);
assertEquals(0, outputBuffer.length());
}
int linkify(String uri) {
return parser.linkifyUri(uri, 0, outputBuffer);
}
void assertLinkify(String uri) {
linkify(uri);
assertLinkOnly(uri, outputBuffer);
}
void assertNotLinkify(String text) {
int newPos = linkify(text);
assertEquals(0, newPos);
}
}

View file

@ -193,21 +193,6 @@ public class HtmlConverterTest {
+ "</pre>", result); + "</pre>", result);
} }
@Test
public void testLinkifyBitcoinAndHttpUri() {
String text = "bitcoin:19W6QZkx8SYPG7BBCS7odmWGRxqRph5jFU http://example.com/";
StringBuffer outputBuffer = new StringBuffer();
HtmlConverter.linkifyText(text, outputBuffer);
assertEquals("<a href=\"bitcoin:19W6QZkx8SYPG7BBCS7odmWGRxqRph5jFU\">" +
"bitcoin:19W6QZkx8SYPG7BBCS7odmWGRxqRph5jFU" +
"</a> " +
"<a href=\"http://example.com/\">" +
"http://example.com/" +
"</a>", outputBuffer.toString());
}
@Test @Test
public void issue2259Spec() { public void issue2259Spec() {
String text = "text\n" + String text = "text\n" +

View file

@ -0,0 +1,209 @@
package com.fsck.k9.message.html;
import org.junit.Test;
import static com.fsck.k9.message.html.UriParserTestHelper.assertLinkOnly;
import static junit.framework.Assert.assertEquals;
public class HttpUriParserTest {
private final HttpUriParser parser = new HttpUriParser();
private final StringBuffer outputBuffer = new StringBuffer();
@Test
public void emptyUriIgnored() {
assertLinkIgnored("http://");
}
@Test
public void emptyAuthorityIgnored() {
assertLinkIgnored("http:///");
}
@Test
public void simpleDomain() {
assertLinkify("http://www.google.com");
}
@Test
public void invalidDomainIgnored() {
assertLinkIgnored("http://-www.google.com");
}
@Test
public void domainWithTrailingSlash() {
assertLinkify("http://www.google.com/");
}
@Test
public void domainWithUserInfo() {
assertLinkify("http://test@google.com/");
}
@Test
public void domainWithFullUserInfo() {
assertLinkify("http://test:secret@google.com/");
}
@Test
public void domainWithoutWww() {
assertLinkify("http://google.com/");
}
@Test
public void query() {
assertLinkify("http://google.com/give/me/?q=mode&c=information");
}
@Test
public void fragment() {
assertLinkify("http://google.com/give/me#only-the-best");
}
@Test
public void queryAndFragment() {
assertLinkify("http://google.com/give/me/?q=mode&c=information#only-the-best");
}
@Test
public void ipv4Address() {
assertLinkify("http://127.0.0.1");
}
@Test
public void ipv4AddressWithTrailingSlash() {
assertLinkify("http://127.0.0.1/");
}
@Test
public void ipv4AddressWithEmptyPort() {
assertLinkify("http://127.0.0.1:");
}
@Test
public void ipv4AddressWithPort() {
assertLinkify("http://127.0.0.1:524/");
}
@Test
public void ipv6Address() {
assertLinkify("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]");
}
@Test
public void ipv6AddressWithPort() {
assertLinkify("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80");
}
@Test
public void ipv6AddressWithTrailingSlash() {
assertLinkify("http://[1080:0:0:0:8:800:200C:417A]/");
}
@Test
public void ipv6AddressWithEndCompression() {
assertLinkify("http://[3ffe:2a00:100:7031::1]");
}
@Test
public void ipv6AddressWithBeginCompression() {
assertLinkify("http://[1080::8:800:200C:417A]/");
}
@Test
public void ipv6AddressWithCompressionPort() {
assertLinkify("http://[::FFFF:129.144.52.38]:80/");
}
@Test
public void ipv6AddressWithPrependedCompression() {
assertLinkify("http://[::192.9.5.5]/");
}
@Test
public void ipv6AddressWithTrailingIp4AndPort() {
assertLinkify("http://[::192.9.5.5]:80/");
}
@Test
public void ipv6WithoutClosingSquareBracketIgnored() {
assertLinkIgnored("http://[1080:0:0:0:8:80:200C:417A/");
}
@Test
public void ipv6InvalidClosingSquareBracketIgnored() {
assertLinkIgnored("http://[1080:0:0:0:8:800:270C:417A/]");
}
@Test
public void domainWithTrailingSpace() {
String text = "http://google.com/ ";
int endPos = parser.linkifyUri(text, 0, outputBuffer);
assertLinkOnly("http://google.com/", outputBuffer);
assertEquals(text.length() - 1, endPos);
}
@Test
public void domainWithTrailingNewline() {
String text = "http://google.com/\n";
int endPos = parser.linkifyUri(text, 0, outputBuffer);
assertLinkOnly("http://google.com/", outputBuffer);
assertEquals(text.length() - 1, endPos);
}
@Test
public void domainWithTrailingAngleBracket() {
String text = "<http://google.com/>";
int endPos = parser.linkifyUri(text, 1, outputBuffer);
assertLinkOnly("http://google.com/", outputBuffer);
assertEquals(text.length() - 1, endPos);
}
@Test
public void uriInMiddleAfterInput() {
String prefix = "prefix ";
String uri = "http://google.com/";
String text = prefix + uri;
parser.linkifyUri(text, prefix.length(), outputBuffer);
assertLinkOnly(uri, outputBuffer);
}
@Test
public void uriInMiddleOfInput() {
String prefix = "prefix ";
String uri = "http://google.com/";
String postfix = " postfix";
String text = prefix + uri + postfix;
parser.linkifyUri(text, prefix.length(), outputBuffer);
assertLinkOnly(uri, outputBuffer);
}
int linkify(String uri) {
return parser.linkifyUri(uri, 0, outputBuffer);
}
void assertLinkify(String uri) {
linkify(uri);
assertLinkOnly(uri, outputBuffer);
}
void assertLinkIgnored(String uri) {
int endPos = linkify(uri);
assertEquals("", outputBuffer.toString());
assertEquals(0, endPos);
}
}

View file

@ -0,0 +1,139 @@
package com.fsck.k9.message.html;
import com.fsck.k9.K9RobolectricTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import static com.fsck.k9.message.html.UriParserTestHelper.assertLinkOnly;
import static junit.framework.Assert.assertEquals;
@RunWith(K9RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class UriLinkifierTest {
private StringBuffer outputBuffer = new StringBuffer();
@Test
public void emptyText() {
String text = "";
UriLinkifier.linkifyText(text, outputBuffer);
assertEquals(text, outputBuffer.toString());
}
@Test
public void textWithoutUri_shouldBeCopiedToOutputBuffer() {
String text = "some text here";
UriLinkifier.linkifyText(text, outputBuffer);
assertEquals(text, outputBuffer.toString());
}
@Test
public void simpleUri() {
String uri = "http://example.org";
UriLinkifier.linkifyText(uri, outputBuffer);
assertLinkOnly(uri, outputBuffer);
}
@Test
public void uriPrecededBySpace() {
String text = " http://example.org";
UriLinkifier.linkifyText(text, outputBuffer);
assertEquals(" <a href=\"http://example.org\">http://example.org</a>", outputBuffer.toString());
}
@Test
public void uriPrecededByOpeningParenthesis() {
String text = "(http://example.org";
UriLinkifier.linkifyText(text, outputBuffer);
assertEquals("(<a href=\"http://example.org\">http://example.org</a>", outputBuffer.toString());
}
@Test
public void uriPrecededBySomeText() {
String uri = "Check out my fantastic URI: http://example.org";
UriLinkifier.linkifyText(uri, outputBuffer);
assertEquals("Check out my fantastic URI: <a href=\"http://example.org\">http://example.org</a>",
outputBuffer.toString());
}
@Test
public void uriWithTrailingText() {
String uri = "http://example.org/ is the best";
UriLinkifier.linkifyText(uri, outputBuffer);
assertEquals("<a href=\"http://example.org/\">http://example.org/</a> is the best", outputBuffer.toString());
}
@Test
public void uriEmbeddedInText() {
String uri = "prefix http://example.org/ suffix";
UriLinkifier.linkifyText(uri, outputBuffer);
assertEquals("prefix <a href=\"http://example.org/\">http://example.org/</a> suffix", outputBuffer.toString());
}
@Test
public void uriWithUppercaseScheme() {
String uri = "HTTP://example.org/";
UriLinkifier.linkifyText(uri, outputBuffer);
assertEquals("<a href=\"HTTP://example.org/\">HTTP://example.org/</a>", outputBuffer.toString());
}
@Test
public void uriNotPrecededByValidSeparator_shouldNotBeLinkified() {
String text = "myhttp://example.org";
UriLinkifier.linkifyText(text, outputBuffer);
assertEquals(text, outputBuffer.toString());
}
@Test
public void uriNotPrecededByValidSeparatorFollowedByValidUri() {
String text = "myhttp: http://example.org";
UriLinkifier.linkifyText(text, outputBuffer);
assertEquals("myhttp: <a href=\"http://example.org\">http://example.org</a>", outputBuffer.toString());
}
@Test
public void schemaMatchWithInvalidUriInMiddleOfTextFollowedByValidUri() {
String text = "prefix http:42 http://example.org";
UriLinkifier.linkifyText(text, outputBuffer);
assertEquals("prefix http:42 <a href=\"http://example.org\">http://example.org</a>", outputBuffer.toString());
}
@Test
public void multipleValidUrisInRow() {
String text = "prefix http://uri1.example.org some text http://uri2.example.org/path postfix";
UriLinkifier.linkifyText(text, outputBuffer);
assertEquals(
"prefix <a href=\"http://uri1.example.org\">http://uri1.example.org</a> some text " +
"<a href=\"http://uri2.example.org/path\">http://uri2.example.org/path</a> postfix",
outputBuffer.toString());
}
}

View file

@ -0,0 +1,38 @@
package com.fsck.k9.message.html;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class UriParserTestHelper {
public static void assertContainsLink(String expected, StringBuffer actual) {
String linkifiedUri = actual.toString();
Document document = Jsoup.parseBodyFragment(linkifiedUri);
Element anchorElement = document.select("a").first();
assertNotNull("No <a> element found", anchorElement);
assertEquals(expected, anchorElement.text());
assertEquals(expected, anchorElement.attr("href"));
}
public static void assertLinkOnly(String expected, StringBuffer actual) {
String linkifiedUri = actual.toString();
Document document = Jsoup.parseBodyFragment(linkifiedUri);
Element anchorElement = document.select("a").first();
assertNotNull("No <a> element found", anchorElement);
assertEquals(expected, anchorElement.text());
assertEquals(expected, anchorElement.attr("href"));
assertAnchorElementIsSoleContent(document, anchorElement);
}
private static void assertAnchorElementIsSoleContent(Document document, Element anchorElement) {
assertEquals(document.body(), anchorElement.parent());
assertTrue("<a> element is surrounded by text", document.body().textNodes().isEmpty());
}
}