From 92494ce44b731048b3ffc0df9e20a0eec0d02637 Mon Sep 17 00:00:00 2001 From: ibaker Date: Wed, 25 Mar 2020 16:38:43 +0000 Subject: [PATCH] Document that overlapping spans aren't rendered to HTML correctly Add some mitigation about why this is probably on OK limitation. PiperOrigin-RevId: 302907940 --- .../exoplayer2/ui/SpannedToHtmlConverter.java | 11 +++++++++++ .../ui/SpannedToHtmlConverterTest.java | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java index 59d5a234fb..a37fe49039 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java @@ -50,6 +50,17 @@ import java.util.List; * Convert {@code text} into HTML, adding tags and styling to match any styling spans present. * *

All textual content is HTML-escaped during the conversion. + * + *

NOTE: The current implementation does not handle overlapping spans correctly, it will + * generate overlapping HTML tags that are invalid. In most cases this won't be a problem because: + * + *

*/ public static String convert(@Nullable CharSequence text) { if (text == null) { diff --git a/library/ui/src/test/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverterTest.java b/library/ui/src/test/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverterTest.java index 382798025e..c27b9d15f9 100644 --- a/library/ui/src/test/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverterTest.java +++ b/library/ui/src/test/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverterTest.java @@ -195,4 +195,23 @@ public class SpannedToHtmlConverterTest { assertThat(html).isEqualTo("String with italic and bold section"); } + + @Test + public void convert_overlappingSpans_producesInvalidHtml() { + SpannableString spanned = new SpannableString("String with italic and bold section"); + spanned.setSpan( + new StyleSpan(Typeface.ITALIC), + 0, + "String with italic and bold".length(), + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + spanned.setSpan( + new StyleSpan(Typeface.BOLD), + "String with italic ".length(), + "String with italic and bold section".length(), + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + + String html = SpannedToHtmlConverter.convert(spanned); + + assertThat(html).isEqualTo("String with italic and bold section"); + } }