Skip to content

Commit

Permalink
Add a few safety checks to prevent NRE + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
0nko committed Oct 21, 2016
1 parent c8d6850 commit b105bf5
Showing 1 changed file with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,42 @@
* Tweaked from source at http://stackoverflow.com/questions/4044509/android-how-to-use-the-html-taghandler
*/
public class WPHtmlTagHandler implements Html.TagHandler {

private static final int SPAN_INDENT_WIDTH = 15;

private int mListItemCount = 0;
private List<String> mListParents = new ArrayList<>();

@Override
public void handleTag(final boolean opening, final String tag, Editable output,
final XMLReader xmlReader) {
if (tag.equals("ul") || tag.equals("ol") || tag.equals("dd")) {
if (opening) {
mListParents.add(tag);
} else {
mListParents.remove(tag);
if (tag != null) {
if (tag.equals("ul") || tag.equals("ol") || tag.equals("dd")) {
if (opening) {
mListParents.add(tag);
} else {
mListParents.remove(tag);
}
mListItemCount = 0;
} else if (tag.equals("li") && !opening) {
handleListTag(output);
}
mListItemCount = 0;
} else if (tag.equals("li") && !opening) {
handleListTag(output);
}
}

private void handleListTag(Editable output) {
int size = mListParents.size();
if (size > 0) {
if (mListParents.get(size - 1).equals("ul")) {
if (size > 0 && output != null) {
if ("ul".equals(mListParents.get(size - 1))) {
output.append("\n");
String[] split = output.toString().split("\n");
int start = 0;
if (split.length != 1) {
int lastIndex = split.length - 1;
start = output.length() - split[lastIndex].length() - 1;
}
output.setSpan(new BulletSpan(15 * mListParents.size()), start, output.length(), 0);
} else if (mListParents.get(size - 1).equals("ol")) {
output.setSpan(new BulletSpan(SPAN_INDENT_WIDTH * mListParents.size()), start, output.length(), 0);
} else if ("ol".equals(mListParents.get(size - 1))) {
mListItemCount++;
output.append("\n");
String[] split = output.toString().split("\n");
Expand All @@ -55,7 +60,7 @@ private void handleListTag(Editable output) {
start = output.length() - split[lastIndex].length() - 1;
}
output.insert(start, mListItemCount + ". ");
output.setSpan(new LeadingMarginSpan.Standard(15 * mListParents.size()), start,
output.setSpan(new LeadingMarginSpan.Standard(SPAN_INDENT_WIDTH * mListParents.size()), start,
output.length(), 0);
}
}
Expand Down

0 comments on commit b105bf5

Please sign in to comment.