Commit 61acd434 by ibaker Committed by kim-vde

Rollback of https://github.com/google/ExoPlayer/commit/252bf43bf450a0f42fd06178c1dc82062d227b29

*** Original commit ***

Stop parsing unsupported WebVTT CSS properties

The spec lists an exhaustive list of CSS properties that should be
recognised, all others must be ignored:
https://www.w3.org/TR/webvtt1/#the-cue-pseudo-element

***

PiperOrigin-RevId: 320150427
parent 90c17fbe
......@@ -38,6 +38,7 @@ import java.util.regex.Pattern;
private static final String RULE_START = "{";
private static final String RULE_END = "}";
private static final String PROPERTY_COLOR = "color";
private static final String PROPERTY_BGCOLOR = "background-color";
private static final String PROPERTY_FONT_FAMILY = "font-family";
private static final String PROPERTY_FONT_WEIGHT = "font-weight";
private static final String PROPERTY_RUBY_POSITION = "ruby-position";
......@@ -190,6 +191,8 @@ import java.util.regex.Pattern;
// At this point we have a presumably valid declaration, we need to parse it and fill the style.
if (PROPERTY_COLOR.equals(property)) {
style.setFontColor(ColorParser.parseCssColor(value));
} else if (PROPERTY_BGCOLOR.equals(property)) {
style.setBackgroundColor(ColorParser.parseCssColor(value));
} else if (PROPERTY_RUBY_POSITION.equals(property)) {
if (VALUE_OVER.equals(value)) {
style.setRubyPosition(RubySpan.POSITION_OVER);
......
......@@ -87,6 +87,8 @@ public final class WebvttCssStyle {
@Nullable private String fontFamily;
@ColorInt private int fontColor;
private boolean hasFontColor;
private int backgroundColor;
private boolean hasBackgroundColor;
@OptionalBoolean private int linethrough;
@OptionalBoolean private int underline;
@OptionalBoolean private int bold;
......@@ -103,6 +105,7 @@ public final class WebvttCssStyle {
targetVoice = "";
fontFamily = null;
hasFontColor = false;
hasBackgroundColor = false;
linethrough = UNSPECIFIED;
underline = UNSPECIFIED;
bold = UNSPECIFIED;
......@@ -234,6 +237,23 @@ public final class WebvttCssStyle {
return hasFontColor;
}
public int getBackgroundColor() {
if (!hasBackgroundColor) {
throw new IllegalStateException("Background color not defined.");
}
return backgroundColor;
}
public WebvttCssStyle setBackgroundColor(int backgroundColor) {
this.backgroundColor = backgroundColor;
hasBackgroundColor = true;
return this;
}
public boolean hasBackgroundColor() {
return hasBackgroundColor;
}
public WebvttCssStyle setFontSize(float fontSize) {
this.fontSize = fontSize;
return this;
......
......@@ -680,6 +680,14 @@ public final class WebvttCueParser {
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (style.hasBackgroundColor()) {
addOrReplaceSpan(
spannedText,
new BackgroundColorSpan(style.getBackgroundColor()),
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (style.getFontFamily() != null) {
addOrReplaceSpan(
spannedText,
......
......@@ -92,33 +92,42 @@ public final class CssParserTest {
@Test
public void parseMethodSimpleInput() {
WebvttCssStyle expectedStyle = new WebvttCssStyle();
String styleBlock1 = " ::cue { color : PapayaWhip }";
expectedStyle.setFontColor(0xFFFFEFD5);
String styleBlock1 = " ::cue { color : black; background-color: PapayaWhip }";
expectedStyle.setFontColor(0xFF000000);
expectedStyle.setBackgroundColor(0xFFFFEFD5);
assertParserProduces(styleBlock1, expectedStyle);
String styleBlock2 = " ::cue { color : black }\n\n::cue { color : invalid }";
expectedStyle = new WebvttCssStyle();
expectedStyle.setFontColor(0xFF000000);
assertParserProduces(styleBlock2, expectedStyle);
String styleBlock3 = "::cue {\n background-color\n:#00fFFe}";
expectedStyle = new WebvttCssStyle();
expectedStyle.setBackgroundColor(0xFF00FFFE);
assertParserProduces(styleBlock3, expectedStyle);
}
@Test
public void parseMethodMultipleRulesInBlockInput() {
String styleBlock = "::cue {\n color\n:#00fFFe} \n::cue {\n color\n:#00000000}\n";
String styleBlock =
"::cue {\n background-color\n:#00fFFe} \n::cue {\n background-color\n:#00000000}\n";
WebvttCssStyle expectedStyle = new WebvttCssStyle();
expectedStyle.setFontColor(0xFF00FFFE);
expectedStyle.setBackgroundColor(0xFF00FFFE);
WebvttCssStyle secondExpectedStyle = new WebvttCssStyle();
secondExpectedStyle.setFontColor(0x000000);
secondExpectedStyle.setBackgroundColor(0x000000);
assertParserProduces(styleBlock, expectedStyle, secondExpectedStyle);
}
@Test
public void multiplePropertiesInBlock() {
String styleBlock =
"::cue(#id){text-decoration:underline; color:red; font-family:Courier; font-weight:bold}";
"::cue(#id){text-decoration:underline; background-color:green;"
+ "color:red; font-family:Courier; font-weight:bold}";
WebvttCssStyle expectedStyle = new WebvttCssStyle();
expectedStyle.setTargetId("id");
expectedStyle.setUnderline(true);
expectedStyle.setBackgroundColor(0xFF008000);
expectedStyle.setFontColor(0xFFFF0000);
expectedStyle.setFontFamily("courier");
expectedStyle.setBold(true);
......@@ -128,10 +137,13 @@ public final class CssParserTest {
@Test
public void rgbaColorExpression() {
String styleBlock = "::cue(#rgb){color: rgba(\n10/* Ugly color */,11\t, 12\n,.1);}";
String styleBlock =
"::cue(#rgb){background-color: rgba(\n10/* Ugly color */,11\t, 12\n,.1);"
+ "color:rgb(1,1,\n1)}";
WebvttCssStyle expectedStyle = new WebvttCssStyle();
expectedStyle.setTargetId("rgb");
expectedStyle.setFontColor(0x190A0B0C);
expectedStyle.setBackgroundColor(0x190A0B0C);
expectedStyle.setFontColor(0xFF010101);
assertParserProduces(styleBlock, expectedStyle);
}
......@@ -237,6 +249,10 @@ public final class CssParserTest {
for (int i = 0; i < expectedStyles.length; i++) {
WebvttCssStyle expected = expectedStyles[i];
WebvttCssStyle actualElem = styles.get(i);
assertThat(actualElem.hasBackgroundColor()).isEqualTo(expected.hasBackgroundColor());
if (expected.hasBackgroundColor()) {
assertThat(actualElem.getBackgroundColor()).isEqualTo(expected.getBackgroundColor());
}
assertThat(actualElem.hasFontColor()).isEqualTo(expected.hasFontColor());
if (expected.hasFontColor()) {
assertThat(actualElem.getFontColor()).isEqualTo(expected.getFontColor());
......
......@@ -407,6 +407,9 @@ public class WebvttDecoderTest {
assertThat(firstCueText)
.hasForegroundColorSpanBetween(0, firstCueText.length())
.withColor(ColorParser.parseCssColor("papayawhip"));
assertThat(firstCueText)
.hasBackgroundColorSpanBetween(0, firstCueText.length())
.withColor(ColorParser.parseCssColor("green"));
Spanned secondCueText = getUniqueSpanTextAt(subtitle, 2_345_000);
assertThat(secondCueText.toString()).isEqualTo("This is the second subtitle.");
......@@ -421,7 +424,7 @@ public class WebvttDecoderTest {
Spanned fourthCueText = getUniqueSpanTextAt(subtitle, 25_000_000);
assertThat(fourthCueText.toString()).isEqualTo("You are an idiot\nYou don't have the guts");
assertThat(fourthCueText)
.hasForegroundColorSpanBetween(0, "You are an idiot".length())
.hasBackgroundColorSpanBetween(0, "You are an idiot".length())
.withColor(ColorParser.parseCssColor("lime"));
assertThat(fourthCueText)
.hasBoldSpanBetween("You are an idiot\n".length(), fourthCueText.length());
......
......@@ -2,6 +2,7 @@ WEBVTT
STYLE
::cue {
background-color: green;
color: papayawhip;
}
/* Style blocks cannot use blank lines nor "dash dash greater than" */
......@@ -12,7 +13,7 @@ STYLE
::cue(#id2) {
color: peachpuff;
}
::cue(v[voice="LaGord"]) { color: lime }
::cue(v[voice="LaGord"]) { background-color: lime }
STYLE
::cue(v[voice="The Frog"]) { font-weight: bold }
......
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