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