Skip to content

Commit

Permalink
Fixed the (minor) issue of collapsing XML elements with no children w…
Browse files Browse the repository at this point in the history
…hen rendering

Added hack code to SmartBuffer as that was easiest;
  • Loading branch information
rchillyard committed Dec 3, 2023
1 parent cc6f92d commit 6464c37
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
22 changes: 21 additions & 1 deletion src/main/scala/com/phasmidsoftware/core/SmartBuffer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,27 @@ case class SmartBuffer(sb: StringBuilder) {
s
}

def result: String = sb.result()
/**
* Get the resulting String.
*
* NOTE: this method contains a hack which collapses output of the form <tag ...></tag> into <tag .../>
* As such, it is useful for e.g. hotSpot which has no children.
*
* NOTE: the method assumes there is at most one newline character at the beginning of the string.
*
* @return
*/
def result: String = {
val str = sb.result()
if (str.isEmpty) str
else {
val regex = """(\s*)<([a-zA-Z0-9 ="]+)></(\w+)>""".r
str.substring(1) match {
case regex(p, q, _) => str.substring(0, 1) + p + "<" + q + "/>"
case _ => str
}
}
}

override def toString: String = sb.toString

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/phasmidsoftware/render/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ object Renderer {
*
* @param maybeName an optional String.
* @param attributes a (private) SmartBuffer: accessible via addAttribute or getAttributes.
* @param interior false if we are at the top level of an element; false if we have been invoked from above.
* @param interior false if we are at the top level of an element; true if we have been invoked from above.
*/
case class StateR(maybeName: Option[String], private val attributes: SmartBuffer, interior: Boolean) extends AutoCloseable {

Expand Down
8 changes: 4 additions & 4 deletions src/test/scala/com/phasmidsoftware/kmldoc/KmlSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,9 @@ class KmlSpec extends AnyFlatSpec with should.Matchers {
| <Icon>
| <href>https://www.gstatic.com/mapspro/images/stock/22-blue-dot.png</href>
| </Icon>
| <hotSpot x="16" xunits="pixels" y="32" yunits="insetPixels"></hotSpot>
| <hotSpot x="16" xunits="pixels" y="32" yunits="insetPixels"/>
|</IconStyle>""".stripMargin
private val iconStyleText = "<IconStyle>\n <scale>1.1</scale>\n <Icon>\n <href>https://www.gstatic.com/mapspro/images/stock/22-blue-dot.png</href>\n </Icon>\n <hotSpot x=\"16\" xunits=\"pixels\" y=\"32\" yunits=\"insetPixels\"></hotSpot>\n </IconStyle>"
private val iconStyleText = "<IconStyle>\n <scale>1.1</scale>\n <Icon>\n <href>https://www.gstatic.com/mapspro/images/stock/22-blue-dot.png</href>\n </Icon>\n <hotSpot x=\"16\" xunits=\"pixels\" y=\"32\" yunits=\"insetPixels\"/>\n </IconStyle>"
private val balloonStyleText = "<BalloonStyle>\n <text>\n<![CDATA[<h3>$[name]</h3>]]>\n</text>\n </BalloonStyle>"
private val labelStyleText = "<LabelStyle>\n <scale>0</scale>\n </LabelStyle>"
private val stylesText = s"\n $labelStyleText\n $iconStyleText\n $balloonStyleText\n"
Expand Down Expand Up @@ -744,7 +744,7 @@ class KmlSpec extends AnyFlatSpec with should.Matchers {
| <Icon>
| <href>https://www.gstatic.com/mapspro/images/stock/22-blue-dot.png</href>
| </Icon>
| <hotSpot x="16" xunits="pixels" y="32" yunits="insetPixels"></hotSpot>
| <hotSpot x="16" xunits="pixels" y="32" yunits="insetPixels"/>
|</IconStyle>""".stripMargin)
case Failure(x) => fail(x)
}
Expand Down Expand Up @@ -850,7 +850,7 @@ class KmlSpec extends AnyFlatSpec with should.Matchers {
| <Icon>
| <href>https://www.gstatic.com/mapspro/images/stock/22-blue-dot.png</href>
| </Icon>
| <hotSpot x="16" xunits="pixels" y="32" yunits="insetPixels"></hotSpot>
| <hotSpot x="16" xunits="pixels" y="32" yunits="insetPixels"/>
| </IconStyle>
| <BalloonStyle>
| <text>$cdata</text>
Expand Down

0 comments on commit 6464c37

Please sign in to comment.