From f23390ed01e14f9679f5b37bedd6edc38a9d4548 Mon Sep 17 00:00:00 2001 From: Jonas Kalderstam Date: Sun, 9 Jun 2024 19:55:50 +0200 Subject: [PATCH] Fixed nested content inside blockquotes --- .../feeder/model/html/HtmlLinearizer.kt | 58 ++++++++++++++----- .../feeder/model/html/HtmlLinearizerTest.kt | 35 +++++++++++ 2 files changed, 78 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/nononsenseapps/feeder/model/html/HtmlLinearizer.kt b/app/src/main/java/com/nononsenseapps/feeder/model/html/HtmlLinearizer.kt index d3a31e45c..baa35912d 100644 --- a/app/src/main/java/com/nononsenseapps/feeder/model/html/HtmlLinearizer.kt +++ b/app/src/main/java/com/nononsenseapps/feeder/model/html/HtmlLinearizer.kt @@ -334,21 +334,49 @@ class HtmlLinearizer { "blockquote" -> { finalizeAndAddCurrentElement(blockStyle) - add( - LinearBlockQuote( - cite = element.attr("cite").ifBlank { null }, - content = - ListBuilderScope { - asElement(blockStyle = LinearTextBlockStyle.TEXT) { - linearizeChildren( - element.childNodes(), - blockStyle = LinearTextBlockStyle.TEXT, - baseUrl = baseUrl, - ) - } - }.items, - ), - ) + + val cite = element.attr("cite").ifBlank { null } + + // Text should be collected into LinearBlockQuote + // Other types should be added separately + ListBuilderScope { + asElement(blockStyle = LinearTextBlockStyle.TEXT) { + linearizeChildren( + element.childNodes(), + blockStyle = LinearTextBlockStyle.TEXT, + baseUrl = baseUrl, + ) + } + }.items + .fold(mutableListOf()) { acc, item -> + if (item is LinearText) { + acc.add(item) + acc + } else { + if (acc.isNotEmpty()) { + add( + LinearBlockQuote( + cite = cite, + content = acc, + ), + ) + } + add( + item, + ) + mutableListOf() + } + } + .let { + if (it.isNotEmpty()) { + add( + LinearBlockQuote( + cite = cite, + content = it, + ), + ) + } + } } "a" -> { diff --git a/app/src/test/java/com/nononsenseapps/feeder/model/html/HtmlLinearizerTest.kt b/app/src/test/java/com/nononsenseapps/feeder/model/html/HtmlLinearizerTest.kt index b08153cbe..63475d793 100644 --- a/app/src/test/java/com/nononsenseapps/feeder/model/html/HtmlLinearizerTest.kt +++ b/app/src/test/java/com/nononsenseapps/feeder/model/html/HtmlLinearizerTest.kt @@ -2,6 +2,7 @@ package com.nononsenseapps.feeder.model.html import com.nononsenseapps.feeder.ui.compose.html.toTableData import org.junit.Before +import org.junit.Ignore import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -536,6 +537,40 @@ class HtmlLinearizerTest { ) } + @Test + fun `content inside blockquote`() { + val html = + """ +

everydaylouie:

+
+
+
+ +

CHALLENGE!

+ """.trimIndent() + + val baseUrl = "https://example.com" + + val result = linearizer.linearize(html, baseUrl).elements + + assertEquals(3, result.size, "Expected items: $result") + + assertTrue { result[1] is LinearImage } + } + + @Ignore + @Test + fun `audio in iframe`() { + val html = + """ + + """.trimIndent() + } + @Test fun `video with no sources is ignored`() { val html = ""