package cgeo.geocaching.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.jdt.annotation.NonNull;

import android.text.Html;
import android.text.Spanned;
import android.text.style.ImageSpan;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public final class HtmlUtils {

    private HtmlUtils() {
        // utility class
    }

    /**
     * Extract the text from a HTML based string. This is similar to what HTML.fromHtml(...) does, but this method also
     * removes the embedded images instead of replacing them by a small rectangular representation character.
     *
     */
    @NonNull
    public static String extractText(final CharSequence html) {
        if (StringUtils.isBlank(html)) {
            return StringUtils.EMPTY;
        }
        String result = html.toString();

        // recognize images in textview HTML contents
        if (html instanceof Spanned) {
            final Spanned text = (Spanned) html;
            final Object[] styles = text.getSpans(0, text.length(), Object.class);
            final List<Pair<Integer, Integer>> removals = new ArrayList<>();
            for (final Object style : styles) {
                if (style instanceof ImageSpan) {
                    final int start = text.getSpanStart(style);
                    final int end = text.getSpanEnd(style);
                    removals.add(Pair.of(start, end));
                }
            }

            // sort reversed and delete image spans
            Collections.sort(removals, new Comparator<Pair<Integer, Integer>>() {

                @Override
                public int compare(final Pair<Integer, Integer> lhs, final Pair<Integer, Integer> rhs) {
                    return rhs.getRight().compareTo(lhs.getRight());
                }
            });
            result = text.toString();
            for (final Pair<Integer, Integer> removal : removals) {
                result = result.substring(0, removal.getLeft()) + result.substring(removal.getRight());
            }
        }

        // now that images are gone, do a normal html to text conversion
        return Html.fromHtml(result).toString().trim();
    }

    /**
     * remove all tags that completely encapsulate the given HTML, e.g. all p and span tags around the content
     */
    @NonNull
    public static String removeExtraTags(final String htmlIn) {
        String html = StringUtils.trim(htmlIn);
        if (StringUtils.startsWith(html, "<") && StringUtils.endsWith(html, ">")) {
            final String tag = "<" + StringUtils.substringBetween(html, "<", ">") + ">";
            final int tagLength = tag.length();
            if (tagLength < 10) {
                final String endTag = "</" + StringUtils.substring(tag, 1);
                final int endTagIndex = html.length() - endTag.length();
                if (StringUtils.startsWith(html, tag) && StringUtils.endsWith(html, endTag) && StringUtils.indexOf(html, endTag) == endTagIndex) {
                    html = StringUtils.substring(html, tagLength, endTagIndex).trim();
                    return removeExtraTags(html);
                }
            }
        }
        return html;
    }
}

@@ -2517,34 +2517,37 @@
 tmlIn);%0A        
-if
+while
  (StringUtils.st
@@ -2767,17 +2767,18 @@
 gLength 
-%3C
+%3E=
  10) %7B%0A 
@@ -2772,32 +2772,65 @@
 th %3E= 10) %7B%0A    
+            break;%0A            %7D%0A
             fina
@@ -2889,36 +2889,32 @@
 1);%0A            
-    
 final int endTag
@@ -2962,32 +2962,29 @@
             
-  
-  
 if (
+!
 StringUtils.
@@ -3001,27 +3001,28 @@
 (html, tag) 
-&& 
+%7C%7C !
 StringUtils.
@@ -3044,18 +3044,18 @@
 endTag) 
-&&
+%7C%7C
  StringU
@@ -3081,17 +3081,17 @@
 endTag) 
-=
+!
 = endTag
@@ -3100,32 +3100,60 @@
 dex) %7B%0A         
+       
+break;%0A            %7D%0A
            html 
@@ -3147,16 +3147,17 @@
         
+ 
 html = S
@@ -3219,90 +3219,8 @@
 ();%0A
-                    return removeExtraTags(html);%0A                %7D%0A            %7D%0A
     

Use loop instead of tail recursion