Commit 4371617e by ibaker Committed by Oliver Woodman

Add HTML support for BackgroundColorSpan

PiperOrigin-RevId: 309244135
parent 8c64d188
......@@ -20,6 +20,7 @@ import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Html;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
......@@ -125,6 +126,10 @@ import java.util.regex.Pattern;
ForegroundColorSpan colorSpan = (ForegroundColorSpan) span;
return Util.formatInvariant(
"<span style='color:%s;'>", toCssColor(colorSpan.getForegroundColor()));
} else if (span instanceof BackgroundColorSpan) {
BackgroundColorSpan colorSpan = (BackgroundColorSpan) span;
return Util.formatInvariant(
"<span style='background-color:%s;'>", toCssColor(colorSpan.getBackgroundColor()));
} else if (span instanceof HorizontalTextInVerticalContextSpan) {
return "<span style='text-combine-upright:all;'>";
} else if (span instanceof StyleSpan) {
......@@ -159,9 +164,9 @@ import java.util.regex.Pattern;
@Nullable
private static String getClosingTag(Object span) {
if (span instanceof ForegroundColorSpan) {
return "</span>";
} else if (span instanceof HorizontalTextInVerticalContextSpan) {
if (span instanceof ForegroundColorSpan
|| span instanceof BackgroundColorSpan
|| span instanceof HorizontalTextInVerticalContextSpan) {
return "</span>";
} else if (span instanceof StyleSpan) {
switch (((StyleSpan) span).getStyle()) {
......
......@@ -22,6 +22,7 @@ import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
......@@ -39,7 +40,7 @@ public class SpannedToHtmlConverterTest {
public void convert_supportsForegroundColorSpan() {
SpannableString spanned = new SpannableString("String with colored section");
spanned.setSpan(
new ForegroundColorSpan(Color.argb(128, 64, 32, 16)),
new ForegroundColorSpan(Color.argb(51, 64, 32, 16)),
"String with ".length(),
"String with colored".length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
......@@ -47,7 +48,24 @@ public class SpannedToHtmlConverterTest {
String html = SpannedToHtmlConverter.convert(spanned);
assertThat(html)
.isEqualTo("String with <span style='color:rgba(64,32,16,0.502);'>colored</span> section");
.isEqualTo("String with <span style='color:rgba(64,32,16,0.200);'>colored</span> section");
}
@Test
public void convert_supportsBackgroundColorSpan() {
SpannableString spanned = new SpannableString("String with highlighted section");
spanned.setSpan(
new BackgroundColorSpan(Color.argb(51, 64, 32, 16)),
"String with ".length(),
"String with highlighted".length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
String html = SpannedToHtmlConverter.convert(spanned);
assertThat(html)
.isEqualTo(
"String with <span style='background-color:rgba(64,32,16,0.200);'>highlighted</span>"
+ " section");
}
@Test
......
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