diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/DefaultPostContentFormatter.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/DefaultPostContentFormatter.kt index 30fbb9e2..c6c02dc5 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/DefaultPostContentFormatter.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/DefaultPostContentFormatter.kt @@ -43,6 +43,8 @@ class DefaultPostContentFormatter(private val policyFactory: PolicyFactory) : Po // 文字だけのHTMLなどはここでpタグで囲む val flattenHtml = unsafeElement.childNodes().mapNotNull { + println(it.toString()) + println(it.javaClass) if (it is Element) { it } else if (it is TextNode) { @@ -57,6 +59,8 @@ class DefaultPostContentFormatter(private val policyFactory: PolicyFactory) : Po val safeHtml = policyFactory.sanitize(unsafeHtml) + println(safeHtml) + val safeDocument = 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") { brCount++ } 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 } } - 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) diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/other/DefaultPostContentFormatterTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/other/DefaultPostContentFormatterTest.kt new file mode 100644 index 00000000..05ee9f62 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/other/DefaultPostContentFormatterTest.kt @@ -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("
a
") + } + + @Test + fun コメントは無視() { + formatter.format("") + } + + @Test + fun brタグを改行に() { + formatter.format("a
a
a