Skip to content

Commit f2e05c2

Browse files
authored
Merge pull request scala#5728 from Philippus/issue/html-tag-in-hover
inlineToStr is not exhaustive and does not remove html tags inside HtmlTag [ci: last-only]
2 parents 920bc4e + e3c5df8 commit f2e05c2

File tree

6 files changed

+90
-32
lines changed

6 files changed

+90
-32
lines changed

src/scaladoc/scala/tools/nsc/doc/html/Page.scala

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package scala
77
package tools.nsc.doc.html
88

99
import scala.tools.nsc.doc.model._
10-
import scala.tools.nsc.doc.base.comment
10+
import scala.tools.nsc.doc.base.comment._
1111
import java.io.{FileOutputStream, File}
1212
import scala.reflect.NameTransformer
1313
import java.nio.channels.Channels
@@ -106,16 +106,21 @@ abstract class Page {
106106
case dtpl: DocTemplateEntity => dtpl.companion.isDefined
107107
case _ => false
108108
}
109+
}
109110

110-
protected def inlineToStr(inl: comment.Inline): String = inl match {
111-
case comment.Chain(items) => items flatMap (inlineToStr(_)) mkString ""
112-
case comment.Italic(in) => inlineToStr(in)
113-
case comment.Bold(in) => inlineToStr(in)
114-
case comment.Underline(in) => inlineToStr(in)
115-
case comment.Monospace(in) => inlineToStr(in)
116-
case comment.Text(text) => text
117-
case comment.Summary(in) => inlineToStr(in)
118-
case comment.EntityLink(comment.Text(text), _) => text
119-
case _ => inl.toString
111+
object Page {
112+
def inlineToStr(inl: Inline): String = inl match {
113+
case Chain(items) => items flatMap (inlineToStr(_)) mkString ""
114+
case Italic(in) => inlineToStr(in)
115+
case Bold(in) => inlineToStr(in)
116+
case Underline(in) => inlineToStr(in)
117+
case Superscript(in) => inlineToStr(in)
118+
case Subscript(in) => inlineToStr(in)
119+
case Link(raw, title) => inlineToStr(title)
120+
case Monospace(in) => inlineToStr(in)
121+
case Text(text) => text
122+
case Summary(in) => inlineToStr(in)
123+
case HtmlTag(tag) => "<[^>]*>".r.replaceAllIn(tag, "")
124+
case EntityLink(Text(text), _) => text
120125
}
121126
}

src/scaladoc/scala/tools/nsc/doc/html/page/Entity.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ trait EntityPage extends HtmlPage {
9090
mbr match {
9191
case dtpl: DocTemplateEntity =>
9292
dtpl.companion.fold(<span class="separator"></span>) { c: DocTemplateEntity =>
93-
<a class="object" href={relativeLinkTo(c)} title={c.comment.fold("")(com => inlineToStr(com.short))}></a>
93+
<a class="object" href={relativeLinkTo(c)} title={c.comment.fold("")(com => Page.inlineToStr(com.short))}></a>
9494
}
9595
case _ => <span class="separator"></span>
9696
}
9797
}
98-
<a class={mbr.kind} href={relativeLinkTo(mbr)} title={mbr.comment.fold("")(com => inlineToStr(com.short))}></a>
99-
<a href={relativeLinkTo(mbr)} title={mbr.comment.fold("")(com => inlineToStr(com.short))}>
98+
<a class={mbr.kind} href={relativeLinkTo(mbr)} title={mbr.comment.fold("")(com => Page.inlineToStr(com.short))}></a>
99+
<a href={relativeLinkTo(mbr)} title={mbr.comment.fold("")(com => Page.inlineToStr(com.short))}>
100100
{mbr.name}
101101
</a>
102102
</li>
@@ -897,7 +897,7 @@ trait EntityPage extends HtmlPage {
897897
}
898898
}
899899
if (!nameLink.isEmpty)
900-
<a title={mbr.comment.fold("")(c => inlineToStr(c.short))} href={nameLink}>
900+
<a title={mbr.comment.fold("")(c => Page.inlineToStr(c.short))} href={nameLink}>
901901
{nameHtml}
902902
</a>
903903
else nameHtml
@@ -1065,7 +1065,7 @@ trait EntityPage extends HtmlPage {
10651065
body.blocks flatMap (blockToStr(_)) mkString ""
10661066

10671067
private def blockToStr(block: comment.Block): String = block match {
1068-
case comment.Paragraph(in) => inlineToStr(in)
1068+
case comment.Paragraph(in) => Page.inlineToStr(in)
10691069
case _ => block.toString
10701070
}
10711071

src/scaladoc/scala/tools/nsc/doc/html/page/IndexScript.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class IndexScript(universe: doc.Universe) extends Page {
8787

8888
/** Gets the short description i.e. the first sentence of the docstring */
8989
def shortDesc(mbr: MemberEntity): String = mbr.comment.fold("") { c =>
90-
inlineToStr(c.short).replaceAll("\n", "")
90+
Page.inlineToStr(c.short).replaceAll("\n", "")
9191
}
9292

9393
/** Returns the json representation of the supplied members */
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Chain(List(Chain(List(Text(This comment contains ), Superscript(Text(superscript))))))
2+
Chain(List(Chain(List(Text(This comment contains ), Subscript(Text(subscript))))))
3+
Chain(List(Chain(List(Text(This comment contains a link ), Link(https://scala.epfl.ch/,Text(https://scala.epfl.ch/))))))
4+
Chain(List(Chain(List(Text(This comment contains an ), HtmlTag(<strong>html tag</strong>)))))
5+
Chain(List(Chain(List(Text(This comment contains a), HtmlTag(<br>), Text( single html tag)))))
6+
Chain(List(Chain(List(Text(This comment contains nested ), HtmlTag(<strong>html<br> tags</strong>)))))
7+
This comment contains superscript
8+
This comment contains subscript
9+
This comment contains a link https://scala.epfl.ch/
10+
This comment contains an html tag
11+
This comment contains a single html tag
12+
This comment contains nested html tags
13+
Done.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import scala.tools.nsc.doc.html.Page
2+
import scala.tools.nsc.doc.model._
3+
import scala.tools.partest.ScaladocModelTest
4+
5+
object Test extends ScaladocModelTest {
6+
7+
override def code = """
8+
/** This comment contains ^superscript^ */
9+
class Foo {
10+
/** This comment contains ,,subscript,, */
11+
def bar = ???
12+
13+
/** This comment contains a link [[https://scala.epfl.ch/]] */
14+
def baz = ???
15+
16+
/** This comment contains an <strong>html tag</strong> */
17+
def qux = ???
18+
19+
/** This comment contains a<br> single html tag */
20+
def quux = ???
21+
22+
/** This comment contains nested <strong>html<br> tags</strong> */
23+
def quuz = ???
24+
}
25+
"""
26+
def scaladocSettings = ""
27+
28+
def testModel(root: Package) = {
29+
import scala.tools.nsc.doc.base.comment._
30+
import access._
31+
32+
val foo = root._class("Foo")
33+
val bar = foo._method("bar")
34+
val baz = foo._method("baz")
35+
val qux = foo._method("qux")
36+
val quux = foo._method("quux")
37+
val quuz = foo._method("quuz")
38+
println(foo.comment.get.short)
39+
println(bar.comment.get.short)
40+
println(baz.comment.get.short)
41+
println(qux.comment.get.short)
42+
println(quux.comment.get.short)
43+
println(quuz.comment.get.short)
44+
println(Page.inlineToStr(foo.comment.get.short))
45+
println(Page.inlineToStr(bar.comment.get.short))
46+
println(Page.inlineToStr(baz.comment.get.short))
47+
println(Page.inlineToStr(qux.comment.get.short))
48+
println(Page.inlineToStr(quux.comment.get.short))
49+
println(Page.inlineToStr(quuz.comment.get.short))
50+
}
51+
}

test/scaladoc/run/shortDescription-annotation.scala

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import scala.tools.nsc.doc.html.Page
12
import scala.tools.nsc.doc.model._
23
import scala.tools.partest.ScaladocModelTest
34

@@ -26,30 +27,18 @@ object Test extends ScaladocModelTest {
2627
import scala.tools.nsc.doc.base.comment._
2728
import access._
2829

29-
def inlineToStr(inl: Inline): String = inl match {
30-
case Chain(items) => items flatMap (inlineToStr(_)) mkString ""
31-
case Italic(in) => inlineToStr(in)
32-
case Bold(in) => inlineToStr(in)
33-
case Underline(in) => inlineToStr(in)
34-
case Monospace(in) => inlineToStr(in)
35-
case Text(text) => text
36-
case Summary(in) => inlineToStr(in)
37-
case EntityLink(Text(text), _) => text
38-
case _ => inl.toString
39-
}
40-
4130
val foo = rootPackage._package("a")._class("Foo")
4231

4332
// Assert that the class has the correct short description
44-
val classDesc = inlineToStr(foo.comment.get.short)
33+
val classDesc = Page.inlineToStr(foo.comment.get.short)
4534
assert(classDesc == "This one should appear", classDesc)
4635

4736
// Assert that the `foo` method has the correct short description
48-
val fooDesc = inlineToStr(foo._method("foo").comment.get.short)
37+
val fooDesc = Page.inlineToStr(foo._method("foo").comment.get.short)
4938
assert(fooDesc == "This comment should appear", fooDesc)
5039

5140
// Assert that the `goo` method has the correct short description
52-
val gooDesc = inlineToStr(foo._method("goo").comment.get.short)
41+
val gooDesc = Page.inlineToStr(foo._method("goo").comment.get.short)
5342
assert(gooDesc == "This comment should appear", gooDesc)
5443
}
5544
}

0 commit comments

Comments
 (0)