Skip to content
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

fix: Angle brackets no longer cause malformed html reports #815

Merged
merged 1 commit into from
Feb 22, 2024
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
10 changes: 5 additions & 5 deletions src/main/scala/com/sksamuel/scapegoat/io/HtmlReportWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.sksamuel.scapegoat.io

import scala.xml.{Elem, Unparsed, XML}

import com.sksamuel.scapegoat.{Feedback, Levels}
import com.sksamuel.scapegoat.{Feedback, Levels, Warning}

/**
* @author
Expand Down Expand Up @@ -100,11 +100,11 @@ object HtmlReportWriter extends ReportWriter {
{reporter.warnings(Levels.Warning).size.toString}
Infos
{reporter.warnings(Levels.Info).size.toString}
</h3>{warnings(reporter)}
</h3>{warnings(reporter.warningsWithMinimalLevel)}
</body>

private def warnings(reporter: Feedback): Seq[Elem] = {
reporter.warningsWithMinimalLevel.map { warning =>
private[io] def warnings(warnings: Seq[Warning]): Seq[Elem] = {
warnings.map { warning =>
val source = warning.sourceFileNormalized + ":" + warning.line
<div class="warning">
<div class="source">
Expand All @@ -123,7 +123,7 @@ object HtmlReportWriter extends ReportWriter {
</span>
</div>
<div>
{decorateCode(warning.explanation)}
{decorateCode(warning.explanation.replace("<", "&lt;").replace(">", "&gt;"))}
</div>{
warning.snippet match {
case None =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.sksamuel.scapegoat.io

import com.sksamuel.scapegoat.{Levels, Warning}
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers

class HtmlReportWriterTest extends AnyFreeSpec with Matchers {

"HtmlReportWriter" - {
"should escape angle brackets" in {
val warnings = Seq(
Warning(
"Pointless type bounds",
13,
Levels.Error,
"/home/johnnei/git/scapegoat/src/main/scala/com/sksamuel/File.scala",
"com.sksamuel.File.scala",
None,
explanation = "Finds type bounds of the form `A <: Any` or `A >: Nothing`.",
inspection = "com.sksamuel.scapegoat.inspections.inference.PointlessTypeBounds"
)
)

val escapedReport = Seq(
<div class="warning">
<div class="source">
com.sksamuel.File.scala:13
</div>
<div class="title">
<span class="label label-danger">Error</span>&nbsp;<span>Pointless type bounds</span>&nbsp; <span class="inspection">
com.sksamuel.scapegoat.inspections.inference.PointlessTypeBounds
</span>
</div>
<div>
<span>Finds type bounds of the form <code>A &lt;: Any</code> or <code>A &gt;: Nothing</code>.</span>
</div>
</div>
)

val report = HtmlReportWriter.warnings(warnings)
report.toString() shouldEqual escapedReport.toString()
}
}
}
Loading