From fbee5a49ee4c21bf0f31362bf1f9af46adba7a44 Mon Sep 17 00:00:00 2001 From: Marcin Aman Date: Thu, 1 Oct 2020 09:59:52 +0200 Subject: Escape urls in anchors --- plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt | 8 ++++++-- .../main/kotlin/resolvers/local/DokkaLocationProvider.kt | 3 ++- .../main/resources/dokka/scripts/platformContentHandler.js | 13 ++++++------- 3 files changed, 14 insertions(+), 10 deletions(-) (limited to 'plugins') diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 01f4ed6d..3eb1c030 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -21,6 +21,7 @@ import org.jetbrains.dokka.plugability.plugin import org.jetbrains.dokka.plugability.query import org.jetbrains.dokka.plugability.querySingle import org.jetbrains.dokka.utilities.htmlEscape +import org.jetbrains.dokka.utilities.urlEncoded import java.io.File import java.net.URI @@ -365,7 +366,7 @@ open class HtmlRenderer( .filter { sourceSetRestriction == null || it.sourceSets.any { s -> s in sourceSetRestriction } } .takeIf { it.isNotEmpty() } ?.let { - val anchorName = node.dci.dri.first().toString() + val anchorName = node.dci.dri.first().anchor withAnchor(anchorName) { div(classes = "table-row") { if (!style.contains(MultimoduleTable)) { @@ -498,7 +499,7 @@ open class HtmlRenderer( override fun FlowContent.buildHeader(level: Int, node: ContentHeader, content: FlowContent.() -> Unit) { - val anchor = node.extra[SimpleAttr.SimpleAttrKey("anchor")]?.extraValue + val anchor = node.extra[SimpleAttr.SimpleAttrKey("anchor")]?.extraValue?.urlEncoded() val classes = node.style.joinToString { it.toString() }.toLowerCase() when (level) { 1 -> h1(classes = classes) { withAnchor(anchor, content) } @@ -759,3 +760,6 @@ private val PageNode.isNavigable: Boolean get() = this !is RendererSpecificPage || strategy != RenderingStrategy.DoNothing fun PropertyContainer.extraHtmlAttributes() = allOfType() + +private val DRI.anchor: String + get() = toString().urlEncoded() diff --git a/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt index 48e44f5f..fe8c8f84 100644 --- a/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt +++ b/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt @@ -8,6 +8,7 @@ import org.jetbrains.dokka.model.DisplaySourceSet import org.jetbrains.dokka.model.withDescendants import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.utilities.urlEncoded import java.util.* open class DokkaLocationProvider( @@ -68,7 +69,7 @@ open class DokkaLocationProvider( private fun getLocalLocation(dri: Pair, context: PageNode?): String? = pagesIndex[dri]?.let { resolve(it, context) } - ?: anchorsIndex[dri]?.let { resolve(it, context) + "#${dri.first}" } + ?: anchorsIndex[dri]?.let { resolve(it, context) + "#${dri.first.toString().urlEncoded()}" } override fun pathToRoot(from: PageNode): String = diff --git a/plugins/base/src/main/resources/dokka/scripts/platformContentHandler.js b/plugins/base/src/main/resources/dokka/scripts/platformContentHandler.js index bc34d6a6..022aca4f 100644 --- a/plugins/base/src/main/resources/dokka/scripts/platformContentHandler.js +++ b/plugins/base/src/main/resources/dokka/scripts/platformContentHandler.js @@ -57,15 +57,14 @@ function handleAnchor() { if (element) { let tab = searchForTab(element) if (tab) { - let found = document.querySelector('.tabs-section > .section-tab[data-togglable="' + tab.getAttribute("data-togglable") + '"]') toggleSections(tab) - const content = element.nextElementSibling - if(content){ - content.classList.add('anchor-highlight') - highlightedAnchor = content - } - element.scrollIntoView({behavior: "smooth"}) } + const content = element.nextElementSibling + if(content){ + content.classList.add('anchor-highlight') + highlightedAnchor = content + } + element.scrollIntoView({behavior: "smooth"}) } } } -- cgit