This commit is contained in:
usbharu 2024-08-07 12:13:52 +09:00
parent 4fd97b6182
commit 046350ef84
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
2 changed files with 76 additions and 2 deletions

View File

@ -43,6 +43,8 @@ class DefaultPostContentFormatter(private val policyFactory: PolicyFactory) : Po
// 文字だけのHTMLなどはここでpタグで囲む // 文字だけのHTMLなどはここでpタグで囲む
val flattenHtml = unsafeElement.childNodes().mapNotNull { val flattenHtml = unsafeElement.childNodes().mapNotNull {
println(it.toString())
println(it.javaClass)
if (it is Element) { if (it is Element) {
it it
} else if (it is TextNode) { } else if (it is TextNode) {
@ -57,6 +59,8 @@ class DefaultPostContentFormatter(private val policyFactory: PolicyFactory) : Po
val safeHtml = policyFactory.sanitize(unsafeHtml) val safeHtml = policyFactory.sanitize(unsafeHtml)
println(safeHtml)
val safeDocument = val safeDocument =
Jsoup.parseBodyFragment(safeHtml).getElementsByTag("body").first() ?: return FormattedPostContent("", "") Jsoup.parseBodyFragment(safeHtml).getElementsByTag("body").first() ?: return FormattedPostContent("", "")
@ -71,11 +75,25 @@ class DefaultPostContentFormatter(private val policyFactory: PolicyFactory) : Po
if (childNode is Element && childNode.tagName() == "br") { if (childNode is Element && childNode.tagName() == "br") {
brCount++ brCount++
} else if (brCount >= 2) { } else if (brCount >= 2) {
formattedHtml.add(Element("p").appendChildren(childNodes.subList(prevIndex, index - brCount))) formattedHtml.add(
Element(element.tag(), element.baseUri(), element.attributes()).appendChildren(
childNodes.subList(
prevIndex,
index - brCount
)
)
)
prevIndex = index prevIndex = index
} }
} }
formattedHtml.add(Element("p").appendChildren(childNodes.subList(prevIndex, childNodes.size))) formattedHtml.add(
Element(element.tag(), element.baseUri(), element.attributes()).appendChildren(
childNodes.subList(
prevIndex,
childNodes.size
)
)
)
} }
val elements = Elements(formattedHtml) val elements = Elements(formattedHtml)

View File

@ -0,0 +1,56 @@
package dev.usbharu.hideout.core.infrastructure.other
import dev.usbharu.hideout.core.config.HtmlSanitizeConfig
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
import org.mockito.Spy
import org.mockito.junit.jupiter.MockitoExtension
@ExtendWith(MockitoExtension::class)
class DefaultPostContentFormatterTest {
@InjectMocks
lateinit var formatter: DefaultPostContentFormatter
@Spy
val policyFactory = HtmlSanitizeConfig().policy()
@Test
fun 文字だけのHTMLをPで囲む() {
formatter.format("a")
}
@Test
fun エレメントはそのまま() {
formatter.format("<p>a</p>")
}
@Test
fun コメントは無視() {
formatter.format("<!-- aa -->")
}
@Test
fun brタグを改行に() {
formatter.format("<p>a<br></p>")
}
@Test
fun brタグ2連続を段落に() {
val format = formatter.format("<p>a<br><br>a</p>")
println(format)
}
@Test
fun aタグは許可される() {
val format = formatter.format("<a href=\"https://example.com\">p</a>")
println(format)
}
@Test
fun pの中のaタグも許可される() {
formatter.format("<p><a href=\"https://example.com\">a</a></p>")
}
}