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;
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
......
......@@ -133,6 +133,20 @@ public class SpannedToHtmlConverterTest {
}
@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");
spanned.setSpan(
......@@ -147,6 +161,13 @@ public class SpannedToHtmlConverterTest {
}
@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 =
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