Escape the rubyText when generating HTML

This should have been done before, I just missed it.

PiperOrigin-RevId: 304612519
This commit is contained in:
ibaker 2020-04-03 15:34:04 +01:00 committed by Oliver Woodman
parent bdffab1200
commit a6358dc7be
2 changed files with 18 additions and 21 deletions

View File

@ -174,7 +174,7 @@ import java.util.regex.Pattern;
}
} else if (span instanceof RubySpan) {
RubySpan rubySpan = (RubySpan) span;
return "<rt>" + rubySpan.rubyText + "</rt></ruby>";
return "<rt>" + escapeHtml(rubySpan.rubyText) + "</rt></ruby>";
} else if (span instanceof UnderlineSpan) {
return "</u>";
}

View File

@ -95,38 +95,35 @@ public class SpannedToHtmlConverterTest {
}
@Test
public void convert_supportsRubySpan_over() {
SpannableString spanned = new SpannableString("String with over-annotated section");
public void convert_supportsRubySpan() {
SpannableString spanned =
new SpannableString("String with over-annotated and under-annotated section");
spanned.setSpan(
new RubySpan("ruby-text", RubySpan.POSITION_OVER),
"String with ".length(),
"String with over-annotated".length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
String html = SpannedToHtmlConverter.convert(spanned);
assertThat(html)
.isEqualTo(
"String with <ruby style='ruby-position:over;'>over-annotated<rt>ruby-text</rt></ruby>"
+ " section");
}
@Test
public void convert_supportsRubySpan_under() {
SpannableString spanned = new SpannableString("String with under-annotated section");
spanned.setSpan(
new RubySpan("ruby-text", RubySpan.POSITION_UNDER),
"String with ".length(),
"String with under-annotated".length(),
new RubySpan("non-àscìì-text", RubySpan.POSITION_UNDER),
"String with over-annotated and ".length(),
"String with over-annotated and under-annotated".length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
String html = SpannedToHtmlConverter.convert(spanned);
assertThat(html)
.isEqualTo(
"String with"
+ " <ruby style='ruby-position:under;'>under-annotated<rt>ruby-text</rt></ruby>"
+ " section");
"String with "
+ "<ruby style='ruby-position:over;'>"
+ "over-annotated"
+ "<rt>ruby-text</rt>"
+ "</ruby> "
+ "and "
+ "<ruby style='ruby-position:under;'>"
+ "under-annotated"
+ "<rt>non-&#224;sc&#236;&#236;-text</rt>"
+ "</ruby> "
+ "section");
}
@Test