commentparser: handle internal links with whitelist of domain

The fullQuote and quote mathers are now still run on the path segment
of the given url, if the domain is whitelisted as such.
We do this by stripping off the domain from the href if it has one.
If it ends up not matching quote patterns we still use the href as a
normal link.
fix-travis
Floens 6 years ago
parent 5dbb361630
commit e1e5ad88ee
  1. 1
      Clover/app/src/main/java/org/floens/chan/core/model/PostLinkable.java
  2. 29
      Clover/app/src/main/java/org/floens/chan/core/site/parser/CommentParser.java

@ -31,6 +31,7 @@ import org.floens.chan.ui.theme.Theme;
* PostCell has a {@link PostCell.PostViewMovementMethod}, that searches spans at the location the TextView was tapped, * PostCell has a {@link PostCell.PostViewMovementMethod}, that searches spans at the location the TextView was tapped,
* and handled if it was a PostLinkable. * and handled if it was a PostLinkable.
*/ */
@SuppressWarnings("JavadocReference")
public class PostLinkable extends ClickableSpan { public class PostLinkable extends ClickableSpan {
public enum Type { public enum Type {
QUOTE, LINK, SPOILER, THREAD QUOTE, LINK, SPOILER, THREAD

@ -53,6 +53,7 @@ public class CommentParser {
private Pattern colorPattern = Pattern.compile("color:#([0-9a-fA-F]+)"); private Pattern colorPattern = Pattern.compile("color:#([0-9a-fA-F]+)");
private Map<String, List<StyleRule>> rules = new HashMap<>(); private Map<String, List<StyleRule>> rules = new HashMap<>();
private List<String> internalDomains = new ArrayList<>(0);
public CommentParser() { public CommentParser() {
// Required tags. // Required tags.
@ -101,6 +102,10 @@ public class CommentParser {
this.fullQuotePattern = fullQuotePattern; this.fullQuotePattern = fullQuotePattern;
} }
public void addInternalDomain(String domain) {
this.internalDomains.add(domain);
}
public CharSequence handleTag(PostParser.Callback callback, public CharSequence handleTag(PostParser.Callback callback,
Theme theme, Theme theme,
Post.Builder post, Post.Builder post,
@ -233,11 +238,29 @@ public class CommentParser {
public Link matchAnchor(Post.Builder post, CharSequence text, Element anchor, PostParser.Callback callback) { public Link matchAnchor(Post.Builder post, CharSequence text, Element anchor, PostParser.Callback callback) {
String href = anchor.attr("href"); String href = anchor.attr("href");
// For inner links we handle it as relative (for sites that have multiple domains).
String path = "";
if (href.startsWith("//") || href.startsWith("http://") || href.startsWith("https://")) {
int offset = href.startsWith("//") ? 2 : (href.startsWith("http://") ? 7 : 8);
String domain = href.substring(Math.min(href.length(), offset),
Math.min(href.length(), href.indexOf('/', offset)));
// Whitelisting domains is optional.
// If you don't specify it it will purely use the quote patterns to match.
if (internalDomains.isEmpty() || internalDomains.contains(domain)) {
int pathStart = href.indexOf('/', offset);
if (pathStart >= 0) {
path = href.substring(pathStart);
}
}
} else {
path = href;
}
PostLinkable.Type t; PostLinkable.Type t;
Object value; Object value;
Matcher externalMatcher = fullQuotePattern.matcher(href); Matcher externalMatcher = fullQuotePattern.matcher(path);
if (externalMatcher.matches()) { if (externalMatcher.matches()) {
String board = externalMatcher.group(1); String board = externalMatcher.group(1);
int threadId = Integer.parseInt(externalMatcher.group(2)); int threadId = Integer.parseInt(externalMatcher.group(2));
@ -251,12 +274,12 @@ public class CommentParser {
value = new PostLinkable.ThreadLink(board, threadId, postId); value = new PostLinkable.ThreadLink(board, threadId, postId);
} }
} else { } else {
Matcher quoteMatcher = quotePattern.matcher(href); Matcher quoteMatcher = quotePattern.matcher(path);
if (quoteMatcher.matches()) { if (quoteMatcher.matches()) {
t = PostLinkable.Type.QUOTE; t = PostLinkable.Type.QUOTE;
value = Integer.parseInt(quoteMatcher.group(1)); value = Integer.parseInt(quoteMatcher.group(1));
} else { } else {
// normal link // normal link, use original href
t = PostLinkable.Type.LINK; t = PostLinkable.Type.LINK;
value = href; value = href;
} }

Loading…
Cancel
Save