diff options
Diffstat (limited to 'plugins/base/src')
3 files changed, 136 insertions, 7 deletions
diff --git a/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt index 2575c204..69a851ed 100644 --- a/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt +++ b/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt @@ -118,22 +118,25 @@ open class DokkaLocationProvider( "${page::class.simpleName}(${page.name}) does not belong to the current page graph so it is impossible to compute its path" ) - val contextNode = - if (context !is ClasslikePageNode && context?.children?.isEmpty() == true && context.parent() != null) context.parent() else context val nodePath = pathFor(node) - val contextPath = contextNode?.let { pathFor(it) }.orEmpty() + val contextPath = context?.let { pathFor(it) }.orEmpty() + val endedContextPath = if (context?.isIndexPage() == false) + contextPath.toMutableList().also { it.removeLastOrNull() } + else contextPath - val commonPathElements = nodePath.asSequence().zip(contextPath.asSequence()) + val commonPathElements = nodePath.asSequence().zip(endedContextPath.asSequence()) .takeWhile { (a, b) -> a == b }.count() - return (List(contextPath.size - commonPathElements) { ".." } + nodePath.drop(commonPathElements) + - if (node is ClasslikePageNode || node.children.isNotEmpty()) + return (List(endedContextPath.size - commonPathElements) { ".." } + nodePath.drop(commonPathElements) + + if (node.isIndexPage()) listOf(PAGE_WITH_CHILDREN_SUFFIX) else emptyList() ).joinToString("/") } + private fun PageNode.isIndexPage() = this is ClasslikePageNode || children.isNotEmpty() + private fun PageNode.parent() = pageGraphRoot.parentMap[this] private val PageNode.pathName: String diff --git a/plugins/base/src/test/kotlin/locationProvider/DokkaLocationProviderTest.kt b/plugins/base/src/test/kotlin/locationProvider/DokkaLocationProviderTest.kt new file mode 100644 index 00000000..8c96eab3 --- /dev/null +++ b/plugins/base/src/test/kotlin/locationProvider/DokkaLocationProviderTest.kt @@ -0,0 +1,126 @@ +package locationProvider + +import org.jetbrains.dokka.base.resolvers.local.DokkaLocationProvider +import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.pages.* +import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.kotlin.backend.common.push +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class DokkaLocationProviderTest : BaseAbstractTest() { + + private val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + sourceRoots = listOf("src/") + classpath += jvmStdlibPath!! + } + } + } + + private fun getTestLocationProvider(root: RootPageNode, context: DokkaContext? = null): DokkaLocationProvider { + val dokkaContext = context ?: DokkaContext.create(configuration, logger, emptyList()) + return DokkaLocationProvider(root, dokkaContext, ".html") + } + + @DslMarker + annotation class TestNavigationDSL + + @TestNavigationDSL + class NavigationDSL { + companion object { + private val stubDCI = DCI( + setOf( + DRI("kotlin", "Any") + ), + ContentKind.Comment + ) + val stubContentNode = ContentText("", stubDCI, emptySet()) + } + + operator fun invoke(name: String, fn: ModulesDsl.() -> Unit): RendererSpecificRootPage { + val modules = ModulesDsl().also { it.fn() } + return RendererSpecificRootPage(name = name, children = modules.pages, RenderingStrategy.DoNothing) + } + + @TestNavigationDSL + class ModulesDsl(val pages: MutableList<ModulePageNode> = mutableListOf()) { + fun modulePage(name: String, fn: PackageDsl.() -> Unit) { + val packages = PackageDsl().also { it.fn() } + pages.push( + ModulePageNode( + name = name, + children = packages.pages, + content = stubContentNode, + documentable = null + ) + ) + } + } + + @TestNavigationDSL + class PackageDsl(val pages: MutableList<PackagePageNode> = mutableListOf()) { + fun packagePage(name: String, fn: ClassDsl.() -> Unit) { + val packages = ClassDsl().also { it.fn() } + pages.push( + PackagePageNode( + name = name, + children = packages.pages, + content = stubContentNode, + documentable = null, + dri = emptySet() + ) + ) + } + } + + @TestNavigationDSL + class ClassDsl(val pages: MutableList<ClasslikePageNode> = mutableListOf()) { + fun classPage(name: String) { + pages.push( + ClasslikePageNode( + name = name, + children = emptyList(), + content = stubContentNode, + documentable = null, + dri = emptySet() + ) + ) + } + } + } + + @Test + fun `links to a package with or without a class`() { + val root = NavigationDSL()("Root") { + modulePage("Module") { + packagePage("Package") {} + } + } + val packagePage = root.children.first().children.first() as PackagePageNode + val locationProvider = getTestLocationProvider(root) + val resolvedLink = locationProvider.resolve(packagePage) + val localToRoot = locationProvider.pathToRoot(packagePage) + + val rootWithClass = NavigationDSL()("Root") { + modulePage("Module") { + packagePage("Package") { + classPage("ClassA") + } + } + } + val packagePageWithClass = rootWithClass.children.first().children.first() as PackagePageNode + + val locationProviderWithClass = getTestLocationProvider(rootWithClass) + val localToRootWithClass = locationProviderWithClass.pathToRoot(packagePageWithClass) + val resolvedLinkWithClass = locationProviderWithClass.resolve(packagePageWithClass) + + assertEquals("-module/Package.html", resolvedLink) + assertEquals("../", localToRoot) + + assertEquals("-module/Package/index.html", resolvedLinkWithClass) + assertEquals("../../", localToRootWithClass) + } +} diff --git a/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt b/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt index 709274f2..57345f5c 100644 --- a/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt +++ b/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt @@ -62,7 +62,7 @@ class ResourceLinksTest : BaseAbstractTest() { r -> assert(it.`is`("[href=$r], [src=$r]")) } relativeResources.forEach { - r -> assert(it.`is`("[href=$r] , [src=$r]")) + r -> assert(it.`is`("[href=../$r] , [src=../$r]")) } } } |