Skip to content

Commit

Permalink
Update Jsoup to 1.14.3 (#2448)
Browse files Browse the repository at this point in the history
* Update Jsoup to 1.14.3

* Fix Jsoup API breaking changes after the update

* Fix new Qodana inspections

* Replace IllegalStateException with more appropriate NoSuchElementException
  • Loading branch information
IgnatBeresnev committed Apr 19, 2022
1 parent 2a0ed52 commit 3d57382
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 44 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -6,7 +6,7 @@ kotlin_version=1.6.20
coroutines_version=1.6.0
kotlinx_html_version=0.7.3
kotlin_plugin_version=211-1.6.20-release-275-IJ7442.40
jsoup_version=1.13.1
jsoup_version=1.14.3
idea_version=211.7442.40
language_version=1.4
jackson_version=2.12.4
Expand Down
Expand Up @@ -61,7 +61,7 @@ abstract class AbstractIntegrationTest {
val fileText = file.readText()
val html = Jsoup.parse(fileText)
html.allElements.toList().forEach { element ->
val href = (element.attr("href") ?: return@forEach)
val href = element.attr("href")
if (href.startsWith("https")) return@forEach
if (href.startsWith("http")) return@forEach

Expand Down
1 change: 1 addition & 0 deletions plugins/base/base-test-utils/api/base-test-utils.api
Expand Up @@ -95,6 +95,7 @@ public final class signatures/Parameters : utils/Tag {

public final class signatures/SignatureUtilsKt {
public static final fun firstSignature (Lorg/jsoup/nodes/Element;)Lorg/jsoup/nodes/Element;
public static final fun lastSignature (Lorg/jsoup/nodes/Element;)Lorg/jsoup/nodes/Element;
public static final fun renderedContent (Lutils/TestOutputWriter;Ljava/lang/String;)Lorg/jsoup/nodes/Element;
public static synthetic fun renderedContent$default (Lutils/TestOutputWriter;Ljava/lang/String;ILjava/lang/Object;)Lorg/jsoup/nodes/Element;
public static final fun signature (Lorg/jsoup/nodes/Element;)Lorg/jsoup/select/Elements;
Expand Down
Expand Up @@ -11,7 +11,8 @@ fun TestOutputWriter.renderedContent(path: String = "root/example.html"): Elemen
.single()

fun Element.signature(): Elements = select("div.symbol.monospace")
fun Element.firstSignature(): Element = signature().first()
fun Element.firstSignature(): Element = signature().first() ?: throw NoSuchElementException("No signature found")
fun Element.lastSignature(): Element = signature().last() ?: throw NoSuchElementException("No signature found")

class Parameters(vararg matchers: Any) : Tag("span", *matchers, expectedClasses = listOf("parameters"))
class Parameter(vararg matchers: Any) : Tag("span", *matchers, expectedClasses = listOf("parameter"))
32 changes: 26 additions & 6 deletions plugins/base/src/test/kotlin/linking/EnumValuesLinkingTest.kt
Expand Up @@ -13,7 +13,7 @@ import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.nio.file.Paths
import utils.TestOutputWriterPlugin
import java.lang.AssertionError
import kotlin.AssertionError

class EnumValuesLinkingTest : BaseAbstractTest() {

Expand Down Expand Up @@ -106,12 +106,32 @@ class EnumValuesLinkingTest : BaseAbstractTest() {
assertNotNull(content.dfs { it is ContentDRILink && it.address.classNames == "JavaEnum.ON_DECEIT" })
}

// single method will throw an exception if there is no single element (0 or 2+)
Jsoup.parse(writerPlugin.writer.contents["root/linking.source/-java-linker/index.html"]).select("a[href=\"../-kotlin-enum/-o-n_-c-r-e-a-t-e/index.html\"]").single()
Jsoup.parse(writerPlugin.writer.contents["root/linking.source/-java-linker/index.html"]).select("a[href=\"../-java-enum/-o-n_-d-e-c-e-i-t/index.html\"]").single()
Jsoup.parse(writerPlugin.writer.contents["root/linking.source/-kotlin-linker/index.html"]).select("a[href=\"../-kotlin-enum/-o-n_-c-r-e-a-t-e/index.html\"]").single()
Jsoup.parse(writerPlugin.writer.contents["root/linking.source/-kotlin-linker/index.html"]).select("a[href=\"../-java-enum/-o-n_-d-e-c-e-i-t/index.html\"]").single()
Jsoup
.parse(writerPlugin.writer.contents.getValue("root/linking.source/-java-linker/index.html"))
.select("a[href=\"../-kotlin-enum/-o-n_-c-r-e-a-t-e/index.html\"]")
.assertOnlyOneElement()

Jsoup
.parse(writerPlugin.writer.contents.getValue("root/linking.source/-java-linker/index.html"))
.select("a[href=\"../-java-enum/-o-n_-d-e-c-e-i-t/index.html\"]")
.assertOnlyOneElement()

Jsoup
.parse(writerPlugin.writer.contents.getValue("root/linking.source/-kotlin-linker/index.html"))
.select("a[href=\"../-kotlin-enum/-o-n_-c-r-e-a-t-e/index.html\"]")
.assertOnlyOneElement()

Jsoup
.parse(writerPlugin.writer.contents.getValue("root/linking.source/-kotlin-linker/index.html"))
.select("a[href=\"../-java-enum/-o-n_-d-e-c-e-i-t/index.html\"]")
.assertOnlyOneElement()
}
}
}

private fun <T> List<T>.assertOnlyOneElement() {
if (isEmpty() || size > 1) {
throw AssertionError("Single element expected in list: $this")
}
}
}
Expand Up @@ -45,7 +45,8 @@ class TextStylesTest : HtmlRenderingOnlyTestBase() {
}
HtmlRenderer(context).render(page)
renderedContent.match(Span("keyword"))
assertEquals(renderedContent.children().last().attr("class"), "token keyword")
val lastChild = renderedContent.children().last() ?: throw IllegalStateException("No element found")
assertEquals(lastChild.attr("class"), "token keyword")
}

@Test
Expand Down
Expand Up @@ -65,7 +65,7 @@ class ResourceLinksTest : BaseAbstractTest() {
) {
renderingStage = { root, context ->
Jsoup
.parse(writerPlugin.writer.contents["root/example.html"])
.parse(writerPlugin.writer.contents.getValue("root/example.html"))
.head()
.select("link, script")
.let {
Expand Down Expand Up @@ -125,7 +125,7 @@ class ResourceLinksTest : BaseAbstractTest() {
}
if (isMultiModule) {
Jsoup
.parse(writerPlugin.writer.contents["example.html"])
.parse(writerPlugin.writer.contents.getValue("example.html"))
.head()
.select("link, script")
.let {
Expand All @@ -135,7 +135,7 @@ class ResourceLinksTest : BaseAbstractTest() {
}
} else {
Jsoup
.parse(writerPlugin.writer.contents["root/example.html"])
.parse(writerPlugin.writer.contents.getValue("root/example.html"))
.head()
.select("link, script")
.let {
Expand Down Expand Up @@ -184,7 +184,7 @@ class ResourceLinksTest : BaseAbstractTest() {
assertNull(writerPlugin.writer.contents["scripts/relativePath.js"])
assertNull(writerPlugin.writer.contents["styles/relativePath.js"])
Jsoup
.parse(writerPlugin.writer.contents["root/example.html"])
.parse(writerPlugin.writer.contents.getValue("root/example.html"))
.head()
.select("link, script")
.let {
Expand Down
Expand Up @@ -235,7 +235,7 @@ class FunctionalTypeConstructorsSignatureTest : BaseAbstractTest() {
pluginOverrides = listOf(writerPlugin)
) {
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/example/-java-class/index.html").signature().last().match(
writerPlugin.writer.renderedContent("root/example/-java-class/index.html").lastSignature().match(
"open val ", A("javaFunction"), ": (", A("Integer"), ") -> ", A("String"), Span(),
ignoreSpanWithTokenStyle = true
)
Expand All @@ -261,7 +261,7 @@ class FunctionalTypeConstructorsSignatureTest : BaseAbstractTest() {
pluginOverrides = listOf(writerPlugin)
) {
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/example/-java-class/index.html").signature().last().match(
writerPlugin.writer.renderedContent("root/example/-java-class/index.html").lastSignature().match(
"open val ", A("kotlinFunction"), ": (", A("Integer"), ") -> ", A("String"), Span(),
ignoreSpanWithTokenStyle = true
)
Expand Down
10 changes: 5 additions & 5 deletions plugins/base/src/test/kotlin/signatures/SignatureTest.kt
Expand Up @@ -547,7 +547,7 @@ class SignatureTest : BaseAbstractTest() {
pluginOverrides = listOf(writerPlugin)
) {
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/example.html").signature().first().match(
writerPlugin.writer.renderedContent("root/example.html").firstSignature().match(
"typealias ", A("PlainTypealias"), " = ", A("Int"), Span(),
ignoreSpanWithTokenStyle = true
)
Expand Down Expand Up @@ -577,7 +577,7 @@ class SignatureTest : BaseAbstractTest() {
pluginOverrides = listOf(writerPlugin)
) {
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/example/index.html").signature().first().match(
writerPlugin.writer.renderedContent("root/example/index.html").firstSignature().match(
Div(
Div(
"@", A("SomeAnnotation")
Expand Down Expand Up @@ -607,7 +607,7 @@ class SignatureTest : BaseAbstractTest() {
pluginOverrides = listOf(writerPlugin)
) {
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/example.html").signature().first().match(
writerPlugin.writer.renderedContent("root/example.html").firstSignature().match(
"typealias ", A("PlainTypealias"), " = ", A("Comparable"),
"<", A("Int"), ">", Span(),
ignoreSpanWithTokenStyle = true
Expand All @@ -634,7 +634,7 @@ class SignatureTest : BaseAbstractTest() {
pluginOverrides = listOf(writerPlugin)
) {
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/example.html").signature().first().match(
writerPlugin.writer.renderedContent("root/example.html").firstSignature().match(
"typealias ", A("GenericTypealias"), "<", A("T"), "> = ", A("Comparable"),
"<", A("T"), ">", Span(),
ignoreSpanWithTokenStyle = true
Expand Down Expand Up @@ -663,7 +663,7 @@ class SignatureTest : BaseAbstractTest() {
pluginOverrides = listOf(writerPlugin)
) {
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-a-b-c/some-fun.html").signature().first()
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-a-b-c/some-fun.html").firstSignature()
.match(
"fun ", A("someFun"), "(", Parameters(
Parameter("xd: ", A("XD"), "<", A("Int"), ", ", A("String"), ">"),
Expand Down
Expand Up @@ -43,7 +43,7 @@ internal class JavadocAccessorNamingTest : AbstractJavadocTemplateMapTest() {
pluginOverrides = listOf(writerPlugin, JavadocPlugin())
) {
renderingStage = { _, _ ->
val html = writerPlugin.writer.contents["sample/TestCase.html"].let { Jsoup.parse(it) }
val html = writerPlugin.writer.contents.getValue("sample/TestCase.html").let { Jsoup.parse(it) }
val props = html
.select("#memberSummary_tabpanel")
.select("th[scope=row].colSecond")
Expand Down
15 changes: 6 additions & 9 deletions plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt
Expand Up @@ -13,10 +13,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.junit.Assert
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import signatures.Parameter
import signatures.Parameters
import signatures.renderedContent
import signatures.signature
import signatures.*
import utils.*
import kotlin.test.assertEquals

Expand Down Expand Up @@ -333,7 +330,7 @@ class KotlinAsJavaPluginTest : BaseAbstractTest() {
cleanupOutput = true
) {
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-a-b-c/some-fun.html").signature().first().match(
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-a-b-c/some-fun.html").firstSignature().match(
"final ", A("Integer"), A("someFun"), "(", Parameters(
Parameter(A("Integer"), "xd")
), ")", Span(), ignoreSpanWithTokenStyle = true
Expand Down Expand Up @@ -372,7 +369,7 @@ class KotlinAsJavaPluginTest : BaseAbstractTest() {
cleanupOutput = true
) {
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-a-b-c/some-fun.html").signature().first().match(
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-a-b-c/some-fun.html").firstSignature().match(
"final ", A("Integer"), A("someFun"), "(", Parameters(
Parameter(A("Map"), "<", A("String"), ", ", A("Integer"), "> xd"),
), ")", Span(), ignoreSpanWithTokenStyle = true
Expand Down Expand Up @@ -438,7 +435,7 @@ class KotlinAsJavaPluginTest : BaseAbstractTest() {
cleanupOutput = true
) {
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-test-kt/sample.html").signature().first().match(
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-test-kt/sample.html").firstSignature().match(
"final static ", A("String"), A("sample"), "(", Parameters(
Parameter(A("Integer"), "a"),
), ")", Span(), ignoreSpanWithTokenStyle = true
Expand Down Expand Up @@ -483,7 +480,7 @@ class KotlinAsJavaPluginTest : BaseAbstractTest() {
assertEquals("Constructors", text.text)
}
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-test/-test.html").signature().first().match(
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-test/-test.html").firstSignature().match(
A("Test"), A("Test"), "(", Parameters(
Parameter(A("Integer"), "xd")
), ")", Span(), ignoreSpanWithTokenStyle = true
Expand Down Expand Up @@ -528,7 +525,7 @@ class KotlinAsJavaPluginTest : BaseAbstractTest() {
assertEquals("Constructors", text.text)
}
renderingStage = { _, _ ->
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-wrapped/-wrapped.html").signature().first().match(
writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-wrapped/-wrapped.html").firstSignature().match(
A("Wrapped"), A("Wrapped"), "(", Parameters(
Parameter(A("Integer"), "xd,").withClasses("indented"),
Parameter(A("Long"), "l,").withClasses("indented"),
Expand Down
4 changes: 2 additions & 2 deletions plugins/mathjax/src/test/kotlin/MathjaxPluginTest.kt
Expand Up @@ -34,7 +34,7 @@ class MathjaxPluginTest : BaseAbstractTest() {
) {
renderingStage = {
_, _ -> Jsoup
.parse(writerPlugin.writer.contents["root/example/test.html"])
.parse(writerPlugin.writer.contents.getValue("root/example/test.html"))
.head()
.select("link, script")
.let {
Expand Down Expand Up @@ -72,7 +72,7 @@ class MathjaxPluginTest : BaseAbstractTest() {
pluginOverrides = listOf(writerPlugin, MathjaxPlugin())
) {
renderingStage = { _, _ ->
val parsed = Jsoup.parse(writerPlugin.writer.contents["root/example/test.html"])
val parsed = Jsoup.parse(writerPlugin.writer.contents.getValue("root/example/test.html"))

// Ensure the MathJax CDN is loaded
assert(parsed.select("link, script").`is`("[href=$LIB_PATH], [src=$LIB_PATH]"))
Expand Down
Expand Up @@ -31,7 +31,7 @@ class AddToNavigationCommandHandler(val context: DokkaContext) : CommandHandler
val node = Element(Tag.valueOf("div"), "", attributes)
navigationFragments.entries.sortedBy { it.key }.forEach { (moduleName, command) ->
command.select("a").forEach { a ->
a.attr("href")?.also { a.attr("href", "${moduleName}/${it}") }
a.attr("href").also { a.attr("href", "${moduleName}/${it}") }
}
command.childNodes().toList().forEachIndexed { index, child ->
if (index == 0) {
Expand All @@ -43,7 +43,7 @@ class AddToNavigationCommandHandler(val context: DokkaContext) : CommandHandler

Files.write(output.resolve("navigation.html").toPath(), listOf(node.outerHtml()))
node.select("a").forEach { a ->
a.attr("href")?.also { a.attr("href", "../${it}") }
a.attr("href").also { a.attr("href", "../${it}") }
}
navigationFragments.keys.forEach {
Files.write(
Expand Down
Expand Up @@ -67,15 +67,17 @@ class DirectiveBasedHtmlTemplateProcessingStrategy(private val context: DokkaCon
var firstStartBorder: Comment? = null
for (index in startFrom until node.childNodeSize()) {
when (val currentChild = node.childNode(index)) {
is Comment -> if (currentChild.data?.startsWith(TEMPLATE_COMMAND_BEGIN_BORDER) == true) {
is Comment -> if (currentChild.data.startsWith(TEMPLATE_COMMAND_BEGIN_BORDER)) {
lastStartBorder = currentChild
firstStartBorder = firstStartBorder ?: currentChild
nodes.clear()
} else if (lastStartBorder != null && currentChild.data?.startsWith(TEMPLATE_COMMAND_END_BORDER) == true) {
} else if (lastStartBorder != null && currentChild.data.startsWith(TEMPLATE_COMMAND_END_BORDER)) {
lastStartBorder.remove()
val cmd: Command? =
lastStartBorder.data?.removePrefix("$TEMPLATE_COMMAND_BEGIN_BORDER$TEMPLATE_COMMAND_SEPARATOR")?.let { parseJson(it) }
cmd?.let { handler(it, nodes) }
val cmd = lastStartBorder.data
.removePrefix("$TEMPLATE_COMMAND_BEGIN_BORDER$TEMPLATE_COMMAND_SEPARATOR")
.let { parseJson<Command>(it) }

handler(cmd, nodes)
currentChild.remove()
extractCommandsFromComments(node, firstStartBorder?.siblingIndex() ?: 0, handler)
return
Expand Down
Expand Up @@ -13,10 +13,13 @@ class ReplaceVersionCommandHandler(private val context: DokkaContext) : CommandH
override fun canHandle(command: Command): Boolean = command is ReplaceVersionsCommand

override fun handleCommandAsTag(command: Command, body: Element, input: File, output: File) {
val position = body.elementSiblingIndex()
val parent = body.parent()
body.remove()
context.configuration.moduleVersion?.takeIf { it.isNotEmpty() }
?.let { parent.insertChildren(position, TextNode(it)) }
if (parent != null) {
val position = body.elementSiblingIndex()
body.remove()

context.configuration.moduleVersion?.takeIf { it.isNotEmpty() }
?.let { parent.insertChildren(position, TextNode(it)) }
}
}
}

0 comments on commit 3d57382

Please sign in to comment.