Add support for multi-line strings to SpannedToHtmlConverter

PiperOrigin-RevId: 304355717
This commit is contained in:
ibaker 2020-04-02 10:11:33 +01:00 committed by Oliver Woodman
parent f3c7c88d7c
commit 7323b5351a
2 changed files with 33 additions and 3 deletions

View File

@ -33,6 +33,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
/**
* Utility class to convert from <a
@ -44,6 +45,9 @@ import java.util.List;
// TODO: Add support for more span types - only a small selection are currently implemented.
/* 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() {}
/**
@ -67,7 +71,7 @@ import java.util.List;
return "";
}
if (!(text instanceof Spanned)) {
return Html.escapeHtml(text);
return escapeHtml(text);
}
Spanned spanned = (Spanned) text;
SparseArray<Transition> spanTransitions = findSpanTransitions(spanned);
@ -76,7 +80,7 @@ import java.util.List;
int previousTransition = 0;
for (int i = 0; i < spanTransitions.size(); 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);
Collections.sort(transition.spansRemoved, SpanInfo.FOR_CLOSING_TAGS);
@ -90,7 +94,7 @@ import java.util.List;
previousTransition = index;
}
html.append(Html.escapeHtml(spanned.subSequence(previousTransition, spanned.length())));
html.append(escapeHtml(spanned.subSequence(previousTransition, spanned.length())));
return html.toString();
}
@ -187,6 +191,11 @@ import java.util.List;
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 {
/**
* Sort by end index (descending), then by opening tag and then closing tag (both ascending, for

View File

@ -132,6 +132,20 @@ public class SpannedToHtmlConverterTest {
assertThat(html).isEqualTo("String with &lt;b&gt;bold&lt;/b&gt; tags");
}
@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() {
SpannableString spanned = new SpannableString("String with <foo>unrecognised</foo> tags");
@ -146,6 +160,13 @@ public class SpannedToHtmlConverterTest {
assertThat(html).isEqualTo("String with <i>&lt;foo&gt;unrecognised&lt;/foo&gt;</i> tags");
}
@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() {
String html =