Skip to content
This repository was archived by the owner on Sep 7, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,27 @@ public class HtmlFormatter {
private HtmlFormatter() {
}

public static Spanned formatHtml(@NonNull HtmlFormatterBuilder builder) {
return formatHtml(builder.getHtml(), builder.getImageGetter(), builder.getClickableTableSpan(), builder.getDrawTableLinkSpan(), builder.getOnClickATagListener(), builder.getIndent(), builder.isRemoveTrailingWhiteSpace());
public static Spanned formatHtml(@NonNull final HtmlFormatterBuilder builder) {
return formatHtml(
builder.getHtml(), builder.getImageGetter(), builder.getClickableTableSpan(),
builder.getDrawTableLinkSpan(), new TagClickListenerProvider() {
@Override public OnClickATagListener provideTagClickListener() {
return builder.getOnClickATagListener();
}
}, builder.getIndent(),
builder.isRemoveTrailingWhiteSpace()
);
}

public static Spanned formatHtml(@Nullable String html, ImageGetter imageGetter, ClickableTableSpan clickableTableSpan, DrawTableLinkSpan drawTableLinkSpan, OnClickATagListener onClickATagListener, float indent, boolean removeTrailingWhiteSpace) {
interface TagClickListenerProvider {
OnClickATagListener provideTagClickListener();
}

public static Spanned formatHtml(@Nullable String html, ImageGetter imageGetter, ClickableTableSpan clickableTableSpan, DrawTableLinkSpan drawTableLinkSpan, TagClickListenerProvider tagClickListenerProvider, float indent, boolean removeTrailingWhiteSpace) {
final HtmlTagHandler htmlTagHandler = new HtmlTagHandler();
htmlTagHandler.setClickableTableSpan(clickableTableSpan);
htmlTagHandler.setDrawTableLinkSpan(drawTableLinkSpan);
htmlTagHandler.setOnClickATagListener(onClickATagListener);
htmlTagHandler.setOnClickATagListenerProvider(tagClickListenerProvider);
htmlTagHandler.setListIndentPx(indent);

html = htmlTagHandler.overrideTags(html);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@
import android.text.style.URLSpan;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import org.xml.sax.Attributes;

import java.util.Stack;
import org.xml.sax.Attributes;

/**
* Some parts of this code are based on android.text.Html
Expand Down Expand Up @@ -117,7 +114,7 @@ String overrideTags(@Nullable String html) {
private static final BulletSpan defaultBullet = new BulletSpan(defaultIndent);
private ClickableTableSpan clickableTableSpan;
private DrawTableLinkSpan drawTableLinkSpan;
private OnClickATagListener onClickATagListener;
private HtmlFormatter.TagClickListenerProvider onClickATagListenerProvider;

private static class Ul {
}
Expand All @@ -126,9 +123,11 @@ private static class Ol {
}

private static class A {
private String text;
private String href;

private A(String href) {
private A(String text, String href) {
this.text = text;
this.href = href;
}
}
Expand Down Expand Up @@ -182,7 +181,7 @@ public boolean handleTag(boolean opening, String tag, Editable output, Attribute
}
} else if (tag.equalsIgnoreCase(A_ITEM)) {
final String href = attributes != null ? attributes.getValue("href") : null;
start(output, new A(href));
start(output, new A(output.toString(), href));
} else if (tag.equalsIgnoreCase("code")) {
start(output, new Code());
} else if (tag.equalsIgnoreCase("center")) {
Expand Down Expand Up @@ -264,14 +263,20 @@ public boolean handleTag(boolean opening, String tag, Editable output, Attribute
}
} else if (tag.equalsIgnoreCase(A_ITEM)) {
final Object a = getLast(output, A.class);
final int spanStart = output.getSpanStart(a);
final int spanEnd = output.length();
final String href = a instanceof A ? ((A) a).href : null;
final String spannedText = output.subSequence(spanStart, spanEnd).toString();
end(output, A.class, false, new URLSpan(href) {
@Override
public void onClick(View widget) {
if (onClickATagListener != null) {
onClickATagListener.onClick(widget, getURL());
} else {
super.onClick(widget);
if (onClickATagListenerProvider != null) {
boolean clickConsumed =
onClickATagListenerProvider.provideTagClickListener()
.onClick(widget, spannedText, getURL());
if (!clickConsumed) {
super.onClick(widget);
}
}
}
});
Expand Down Expand Up @@ -427,7 +432,7 @@ public void setDrawTableLinkSpan(DrawTableLinkSpan drawTableLinkSpan) {
this.drawTableLinkSpan = drawTableLinkSpan;
}

public void setOnClickATagListener(OnClickATagListener onClickATagListener) {
this.onClickATagListener = onClickATagListener;
public void setOnClickATagListenerProvider(HtmlFormatter.TagClickListenerProvider onClickATagListenerProvider) {
this.onClickATagListenerProvider = onClickATagListenerProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,14 @@ public void setHtml(@RawRes int resId, @Nullable Html.ImageGetter imageGetter) {
* HtmlLocalImageGetter and HtmlRemoteImageGetter
*/
public void setHtml(@NonNull String html, @Nullable Html.ImageGetter imageGetter) {

Spanned styledText = HtmlFormatter.formatHtml(html, imageGetter, clickableTableSpan, drawTableLinkSpan, onClickATagListener, indent, removeTrailingWhiteSpace);
Spanned styledText = HtmlFormatter.formatHtml(
html, imageGetter, clickableTableSpan, drawTableLinkSpan,
new HtmlFormatter.TagClickListenerProvider() {
@Override public OnClickATagListener provideTagClickListener() {
return onClickATagListener;
}
}, indent, removeTrailingWhiteSpace
);
replaceQuoteSpans(styledText);
setText(styledText);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
* This listener can define what happens when the a tag is clicked
*/
public interface OnClickATagListener {
void onClick(View widget, @Nullable String href);

/**
* Notifies of anchor tag click events.
* @param widget - the {@link HtmlTextView} instance
* @param spannedText - the string value of the text spanned
* @param href - the url for the anchor tag
* @return indicates whether the click event has been handled
*/
boolean onClick(View widget, String spannedText, @Nullable String href);
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ protected void onCreate(Bundle savedInstanceState) {

// a tag click listener
textView.setOnClickATagListener(new OnClickATagListener() {

@Override
public void onClick(View widget, @Nullable String href) {
public boolean onClick(View widget, String spannedText, @Nullable String href) {
final Toast toast = Toast.makeText(MainActivity.this, null, Toast.LENGTH_SHORT);
toast.setText(href);
toast.show();

return false;
}
});
textView.blockQuoteBackgroundColor=getResources().getColor(R.color.whitish);
Expand Down