Commit 7323b535 by ibaker Committed by Oliver Woodman

Add support for multi-line strings to SpannedToHtmlConverter

PiperOrigin-RevId: 304355717
parent f3c7c88d
...@@ -33,6 +33,7 @@ import java.util.ArrayList; ...@@ -33,6 +33,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
/** /**
* Utility class to convert from <a * Utility class to convert from <a
...@@ -44,6 +45,9 @@ import java.util.List; ...@@ -44,6 +45,9 @@ import java.util.List;
// TODO: Add support for more span types - only a small selection are currently implemented. // TODO: Add support for more span types - only a small selection are currently implemented.
/* package */ final class SpannedToHtmlConverter { /* package */ final class SpannedToHtmlConverter {
// Matches /n and /r/n in ampersand-encoding (returned from Html.escapeHtml).
private static final Pattern NEWLINE_PATTERN = Pattern.compile("(&#13;)?&#10;");
private SpannedToHtmlConverter() {} private SpannedToHtmlConverter() {}
/** /**
...@@ -67,7 +71,7 @@ import java.util.List; ...@@ -67,7 +71,7 @@ import java.util.List;
return ""; return "";
} }
if (!(text instanceof Spanned)) { if (!(text instanceof Spanned)) {
return Html.escapeHtml(text); return escapeHtml(text);
} }
Spanned spanned = (Spanned) text; Spanned spanned = (Spanned) text;
SparseArray<Transition> spanTransitions = findSpanTransitions(spanned); SparseArray<Transition> spanTransitions = findSpanTransitions(spanned);
...@@ -76,7 +80,7 @@ import java.util.List; ...@@ -76,7 +80,7 @@ import java.util.List;
int previousTransition = 0; int previousTransition = 0;
for (int i = 0; i < spanTransitions.size(); i++) { for (int i = 0; i < spanTransitions.size(); i++) {
int index = spanTransitions.keyAt(i); int index = spanTransitions.keyAt(i);
html.append(Html.escapeHtml(spanned.subSequence(previousTransition, index))); html.append(escapeHtml(spanned.subSequence(previousTransition, index)));
Transition transition = spanTransitions.get(index); Transition transition = spanTransitions.get(index);
Collections.sort(transition.spansRemoved, SpanInfo.FOR_CLOSING_TAGS); Collections.sort(transition.spansRemoved, SpanInfo.FOR_CLOSING_TAGS);
...@@ -90,7 +94,7 @@ import java.util.List; ...@@ -90,7 +94,7 @@ import java.util.List;
previousTransition = index; previousTransition = index;
} }
html.append(Html.escapeHtml(spanned.subSequence(previousTransition, spanned.length()))); html.append(escapeHtml(spanned.subSequence(previousTransition, spanned.length())));
return html.toString(); return html.toString();
} }
...@@ -187,6 +191,11 @@ import java.util.List; ...@@ -187,6 +191,11 @@ import java.util.List;
return transition; return transition;
} }
private static String escapeHtml(CharSequence text) {
String escaped = Html.escapeHtml(text);
return NEWLINE_PATTERN.matcher(escaped).replaceAll("<br>");
}
private static final class SpanInfo { private static final class SpanInfo {
/** /**
* Sort by end index (descending), then by opening tag and then closing tag (both ascending, for * Sort by end index (descending), then by opening tag and then closing tag (both ascending, for
......
...@@ -133,6 +133,20 @@ public class SpannedToHtmlConverterTest { ...@@ -133,6 +133,20 @@ public class SpannedToHtmlConverterTest {
} }
@Test @Test
public void convert_handlesLinebreakInUnspannedString() {
String html = SpannedToHtmlConverter.convert("String with\nnew line and\r\ncrlf style too");
assertThat(html).isEqualTo("String with<br>new line and<br>crlf style too");
}
@Test
public void convert_doesntConvertAmpersandLineFeedToBrTag() {
String html = SpannedToHtmlConverter.convert("String with&#10;new line ampersand code");
assertThat(html).isEqualTo("String with&amp;#10;new line ampersand code");
}
@Test
public void convert_escapesUnrecognisedTagInSpannedString() { public void convert_escapesUnrecognisedTagInSpannedString() {
SpannableString spanned = new SpannableString("String with <foo>unrecognised</foo> tags"); SpannableString spanned = new SpannableString("String with <foo>unrecognised</foo> tags");
spanned.setSpan( spanned.setSpan(
...@@ -147,6 +161,13 @@ public class SpannedToHtmlConverterTest { ...@@ -147,6 +161,13 @@ public class SpannedToHtmlConverterTest {
} }
@Test @Test
public void convert_handlesLinebreakInSpannedString() {
String html = SpannedToHtmlConverter.convert("String with\nnew line and\r\ncrlf style too");
assertThat(html).isEqualTo("String with<br>new line and<br>crlf style too");
}
@Test
public void convert_convertsNonAsciiCharactersToAmpersandCodes() { public void convert_convertsNonAsciiCharactersToAmpersandCodes() {
String html = String html =
SpannedToHtmlConverter.convert( SpannedToHtmlConverter.convert(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment