Add test for HtmlDocument.

This commit is contained in:
Benoit Marty 2023-02-15 15:18:36 +01:00 committed by Benoit Marty
parent bdae51f252
commit 2dae6b6d7b
42 changed files with 152 additions and 24 deletions

View file

@ -23,19 +23,32 @@ import org.jsoup.nodes.Document
open class DocumentProvider : PreviewParameterProvider<Document> {
override val values: Sequence<Document>
get() = sequenceOf(
"hello",
// TODO Find a way to make is work with real HTML data. For now there is an error with
// jsoup: java.lang.NoSuchMethodError: 'java.lang.String org.jsoup.nodes.Element.normalName()'
/*
"<html>\n" +
" <head></head>\n" +
" <body>\n" +
" <p><strong>Bold</strong></p>\n" +
" </body>\n" +
"</html>",
"<html><head></head><body><b>Bold</b></body></html>",
"<h1>Heading 1</h1>",
"<h2>Heading 2</h2>",
*/
"text",
"<strong>Strong</strong>",
"<b>Bold</b>",
"<i>Italic</i>",
// FIXME This does not work
"<b><i>Bold then italic</i></b>",
// FIXME This does not work
"<i><b>Italic then bold</b></i>",
"<em>em</em>",
"<unknown>unknown</unknown>",
// FIXME `br` is not rendered correctly in the Preview.
"Line 1<br/>Line 2",
"<code>code</code>",
"<del>del</del>",
"<h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3><h4>Heading 4</h4><h5>Heading 5</h5><h6>Heading 6</h6><h7>Heading 7</h7>",
"<a href=\"https://matrix.org\">link</a>",
"<p>paragraph</p>",
"<p>paragraph 1</p><p>paragraph 2</p>",
"<ol><li>ol item 1</li><li>ol item 2</li></ol>",
"<ol><li><i>ol item 1 italic</i></li><li><b>ol item 2 bold</b></li></ol>",
"<ul><li>ul item 1</li><li>ul item 2</li></ul>",
"<blockquote>blockquote</blockquote>",
// TODO Find a way to make is work with `pre`. For now there is an error with
// jsoup: java.lang.NoSuchMethodError: 'org.jsoup.nodes.Element org.jsoup.nodes.Element.firstElementChild()'
// "<pre>pre</pre>",
"<mx-reply><blockquote><a href=\\\"https://matrix.to/#/!roomId/\$eventId?via=matrix.org\\\">In reply to</a> " +
"<a href=\\\"https://matrix.to/#/@alice:matrix.org\\\">@alice:matrix.org</a><br>original message</blockquote></mx-reply>reply",
).map { Jsoup.parse(it) }
}

View file

@ -146,7 +146,7 @@ private fun HtmlBody(
}
private fun Element.isInline(): Boolean {
return when (normalName()) {
return when (tagName().lowercase()) {
"del" -> true
"mx-reply" -> false
else -> !isBlock
@ -163,7 +163,7 @@ private fun HtmlBlock(
) {
val blockModifier = modifier
.padding(top = 4.dp)
when (element.normalName()) {
when (element.tagName().lowercase()) {
"p" -> HtmlParagraph(
paragraph = element,
modifier = blockModifier,
@ -237,7 +237,7 @@ private fun HtmlPreformatted(
pre: Element,
modifier: Modifier = Modifier
) {
val isCode = pre.firstElementChild()?.normalName() == "code"
val isCode = pre.firstElementChild()?.tagName()?.lowercase() == "code"
val backgroundColor =
if (isCode) MaterialTheme.colorScheme.codeBackground() else Color.Unspecified
Box(
@ -313,7 +313,7 @@ private fun HtmlHeading(
onTextClicked: () -> Unit = {},
onTextLongClicked: () -> Unit = {},
) {
val style = when (heading.normalName()) {
val style = when (heading.tagName().lowercase()) {
"h1" -> MaterialTheme.typography.headlineLarge.copy(fontSize = 30.sp)
"h2" -> MaterialTheme.typography.headlineLarge.copy(fontSize = 26.sp)
"h3" -> MaterialTheme.typography.headlineMedium.copy(fontSize = 22.sp)
@ -369,7 +369,7 @@ private fun HtmlMxReply(
}
}
is Element -> {
when (blockquoteNode.normalName()) {
when (blockquoteNode.tagName().lowercase()) {
"br" -> {
append('\n')
}
@ -491,7 +491,7 @@ private fun AnnotatedString.Builder.appendInlineChildrenElements(
}
private fun AnnotatedString.Builder.appendInlineElement(element: Element, colors: ColorScheme) {
when (element.normalName()) {
when (element.tagName().lowercase()) {
"br" -> {
append('\n')
}
@ -510,6 +510,7 @@ private fun AnnotatedString.Builder.appendInlineElement(element: Element, colors
appendInlineChildrenElements(element.childNodes(), colors)
}
}
"i",
"em" -> {
withStyle(style = SpanStyle(fontStyle = FontStyle.Italic)) {
appendInlineChildrenElements(element.childNodes(), colors)