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:
+ *
+ *
+ * - Most subtitle formats use a tagged structure to carry formatting information (e.g. WebVTT
+ * and TTML), so the {@link Spanned} objects created by these decoders likely won't have
+ * overlapping spans.
+ *
- WebView/Chromium (the intended destination of this HTML) gracefully handles overlapping
+ * tags and usually renders the same result as spanned text in a TextView.
+ *
*/
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");
+ }
}