Add support for multi-line strings to SpannedToHtmlConverter
PiperOrigin-RevId: 304355717
This commit is contained in:
parent
f3c7c88d7c
commit
7323b5351a
@ -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;
|
|||||||
// 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("( )? ");
|
||||||
|
|
||||||
private SpannedToHtmlConverter() {}
|
private SpannedToHtmlConverter() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -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;
|
|||||||
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;
|
|||||||
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;
|
|||||||
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
|
||||||
|
@ -132,6 +132,20 @@ public class SpannedToHtmlConverterTest {
|
|||||||
assertThat(html).isEqualTo("String with <b>bold</b> tags");
|
assertThat(html).isEqualTo("String with <b>bold</b> 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 new line ampersand code");
|
||||||
|
|
||||||
|
assertThat(html).isEqualTo("String with&#10;new line ampersand code");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@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");
|
||||||
@ -146,6 +160,13 @@ public class SpannedToHtmlConverterTest {
|
|||||||
assertThat(html).isEqualTo("String with <i><foo>unrecognised</foo></i> tags");
|
assertThat(html).isEqualTo("String with <i><foo>unrecognised</foo></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
|
@Test
|
||||||
public void convert_convertsNonAsciiCharactersToAmpersandCodes() {
|
public void convert_convertsNonAsciiCharactersToAmpersandCodes() {
|
||||||
String html =
|
String html =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user