Skip to content

Scaladoc: fix issues with incorrect external links and special characters #14516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions scaladoc-js/main/src/searchbar/PageEntry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import scala.scalajs.js

@js.native
trait PageEntryJS extends js.Object {
val n: String = js.native
val t: String = js.native
val d: String = js.native
val l: String = js.native
val k: String = js.native
val n: String = js.native
val t: String = js.native
val d: String = js.native
val l: String = js.native
val e: Boolean = js.native
val k: String = js.native
}

case class PageEntry(
fullName: String,
description: String,
location: String,
isLocationExternal: Boolean,
shortName: String,
kind: String,
tokens: List[String]
Expand All @@ -34,6 +36,7 @@ object PageEntry {
jsObj.t,
jsObj.d,
jsObj.l,
jsObj.e,
jsObj.n.toLowerCase,
jsObj.k,
StringUtils.createCamelCaseTokens(jsObj.n)
Expand Down
16 changes: 12 additions & 4 deletions scaladoc-js/main/src/searchbar/SearchbarComponent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
icon.classList.add(p.kind.take(2))

val resultA = document.createElement("a").asInstanceOf[html.Anchor]
resultA.href = Globals.pathToRoot + p.location
resultA.href =
if (p.isLocationExternal) {
p.location
} else {
Globals.pathToRoot + p.location
}
resultA.text = s"${p.fullName}"
resultA.onclick = (event: Event) =>
if (document.body.contains(rootDiv)) {
Expand Down Expand Up @@ -57,11 +62,14 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
icon.classList.add(m.entryType.take(2))

val resultA = document.createElement("a").asInstanceOf[html.Anchor]
// Inkuire pageLocation should start with e (external)
// or i (internal). The rest of the string is an absolute
// or relative URL
resultA.href =
if(new URI(m.pageLocation).isAbsolute()) {
m.pageLocation
if (m.pageLocation(0) == 'e') {
m.pageLocation.substring(1)
} else {
Globals.pathToRoot + m.pageLocation
Globals.pathToRoot + m.pageLocation.substring(1)
}
resultA.text = m.functionName
resultA.onclick = (event: Event) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MatchersTest:
s"$kind $name",
"",
"",
false,
s"$name",
kind,
StringUtils.createCamelCaseTokens(name)
Expand Down
3 changes: 2 additions & 1 deletion scaladoc/src/dotty/tools/scaladoc/Inkuire.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ object Inkuire {
name: String,
packageName: String,
uri: String,
isLocationExternal: Boolean,
entryType: String
)

Expand Down Expand Up @@ -333,7 +334,7 @@ object Inkuire {
("signature", serialize(e.signature)),
("name", serialize(e.name)),
("packageName", serialize(e.packageName)),
("uri", serialize(e.uri)),
("uri", serialize((if e.isLocationExternal then "e" else "i") + e.uri)),
("entryType", serialize(e.entryType))
)
}
Expand Down
7 changes: 6 additions & 1 deletion scaladoc/src/dotty/tools/scaladoc/renderers/Locations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ trait Locations(using ctx: DocContext):

def resolveRoot(dri: DRI, path: String): String = resolveRoot(rawLocation(dri), path)
def absolutePath(dri: DRI, extension: String = "html"): String = rawLocation(dri).mkString("", "/", s".$extension")
def absolutePathWithAnchor(dri: DRI, extension: String = "html"): String = s"${absolutePath(dri, extension)}#${dri.anchor}"

def escapedAbsolutePathWithAnchor(dri: DRI, extension: String = "html"): String =
s"${escapeUrl(absolutePath(dri, extension))}#${dri.anchor}"

def relativeInternalOrAbsoluteExternalPath(dri: DRI): String =
dri.externalLink.getOrElse(escapedAbsolutePathWithAnchor(dri))

def resolveLink(dri: DRI, url: String): String =
if URI(url).isAbsolute then url else resolveRoot(dri, url)
Expand Down
4 changes: 3 additions & 1 deletion scaladoc/src/dotty/tools/scaladoc/renderers/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ abstract class Renderer(rootPackage: Member, val members: Map[DRI, Member], prot
val signatureRenderer = new SignatureRenderer:
def currentDri: DRI = page.link.dri
def link(dri: DRI): Option[String] =
Some(pathToPage(currentDri, dri)).filter(_ != UnresolvedLocationLink)
dri.externalLink.orElse(
Some(pathToPage(currentDri, dri)).filter(_ != UnresolvedLocationLink)
)

MemberRenderer(signatureRenderer).fullMember(m)
case t: ResolvedTemplate => siteContent(page.link.dri, t)
Expand Down
3 changes: 2 additions & 1 deletion scaladoc/src/dotty/tools/scaladoc/renderers/Resources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ trait Resources(using ctx: DocContext) extends Locations, Writer:
}.mkString

def mkEntry(dri: DRI, name: String, text: String, descr: String, kind: String) = jsonObject(
"l" -> jsonString(absolutePathWithAnchor(dri)),
"l" -> jsonString(relativeInternalOrAbsoluteExternalPath(dri)),
"e" -> (if dri.externalLink.isDefined then rawJSON("true") else rawJSON("false")),
"n" -> jsonString(name),
"t" -> jsonString(text),
"d" -> jsonString(descr),
Expand Down
6 changes: 4 additions & 2 deletions scaladoc/src/dotty/tools/scaladoc/tasty/InkuireSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ trait InkuireSupport(using DocContext) extends Resources:
),
name = name,
packageName = ownerName,
uri = methodSymbol.dri.externalLink.getOrElse(absolutePathWithAnchor(methodSymbol.dri)),
uri = methodSymbol.dri.externalLink.getOrElse(escapedAbsolutePathWithAnchor(methodSymbol.dri)),
isLocationExternal = methodSymbol.dri.externalLink.isDefined,
entryType = "def"
)
val curriedSgn = sgn.copy(signature = Inkuire.curry(sgn.signature))
Expand Down Expand Up @@ -142,7 +143,8 @@ trait InkuireSupport(using DocContext) extends Resources:
),
name = name,
packageName = ownerName,
uri = valSymbol.dri.externalLink.getOrElse(absolutePathWithAnchor(valSymbol.dri)),
uri = valSymbol.dri.externalLink.getOrElse(escapedAbsolutePathWithAnchor(valSymbol.dri)),
isLocationExternal = valSymbol.dri.externalLink.isDefined,
entryType = "val"
)
val curriedSgn = sgn.copy(signature = Inkuire.curry(sgn.signature))
Expand Down
15 changes: 15 additions & 0 deletions scaladoc/test/dotty/tools/scaladoc/renderers/LocationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ class LocationTests:
"../../annotation",
path("api/scala/annotation/meta/beanGetter", "api/scala/annotation"),
)

@Test
def testAnchorLinks() =
def pathWithAnchor(location: String, anchor: String) =
locations.escapedAbsolutePathWithAnchor(new DRI(location, anchor))

assertEquals(
"scala/%23::.html#abcde",
pathWithAnchor("scala.#::", "abcde")
)

assertEquals(
"scala/collection/immutable/LazyList$$%23$.html#abcde",
pathWithAnchor("scala.collection.immutable.LazyList$$#$", "abcde")
)