0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 12:02:16 +02:00

fix: replace cloze with blank within <tts> tag

resolves ankidroid/Anki-Android#12233
This commit is contained in:
Kendall Park 2022-08-29 17:58:19 -05:00 committed by Mike Hardy
parent 1107d61e97
commit e687746093
2 changed files with 17 additions and 4 deletions

View File

@ -26,7 +26,7 @@ object TtsParser {
fun getTextsToRead(html: String, clozeReplacement: String?): List<TTSTag> {
val textsToRead: MutableList<TTSTag> = ArrayList()
val elem = Jsoup.parseBodyFragment(html).body()
parseTtsElements(elem, textsToRead)
parseTtsElements(elem, textsToRead, clozeReplacement)
if (textsToRead.isEmpty()) {
// No <tts service="android"> elements found: return the text of the whole HTML fragment
textsToRead.add(readWholeCard(elem.text().replace(TemplateFilters.CLOZE_DELETION_REPLACEMENT, clozeReplacement!!)))
@ -34,15 +34,15 @@ object TtsParser {
return textsToRead
}
private fun parseTtsElements(element: Element, textsToRead: MutableList<TTSTag>) {
private fun parseTtsElements(element: Element, textsToRead: MutableList<TTSTag>, clozeReplacement: String?) {
if ("tts".equals(element.tagName(), ignoreCase = true) &&
"android".equals(element.attr("service"), ignoreCase = true)
) {
textsToRead.add(localisedText(element.text(), element.attr("voice")))
textsToRead.add(localisedText(element.text().replace(TemplateFilters.CLOZE_DELETION_REPLACEMENT, clozeReplacement!!), element.attr("voice")))
return // ignore any children
}
for (child in element.children()) {
parseTtsElements(child, textsToRead)
parseTtsElements(child, textsToRead, clozeReplacement)
}
}

View File

@ -36,6 +36,19 @@ class TtsParserTest {
assertThat(actual.fieldText, equalTo("This is a blank"))
}
@Test
fun clozeIsReplacedWithBlankInTTSTag() {
val content = """<style>.card {
font-family: arial;
font-size: 20px;
text-align: center;
color: black;
background-color: white;
}.cloze {font-weight: bold;color: blue;}</style><tts service="android">This is a <span class=cloze>[...]</span></tts>"""
val actual = getTtsTagFrom(content)
assertThat(actual.fieldText, equalTo("This is a blank"))
}
@Test
fun providedExampleClozeReplaces() {
val content = """<style>.card {