Skip to content

Commit 13c3e29

Browse files
authored
Merge pull request #441 from isomarcte/fix-comment-edge-case
Fix Invalid Comment Edge Case
2 parents a4cafbe + 74778d6 commit 13c3e29

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

shared/src/main/scala/scala/xml/Comment.scala

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ package xml
1414
*
1515
* @author Burak Emir
1616
* @param commentText the text contained in this node, may not contain "--"
17+
* and the final character may not be `-` to prevent a closing span of `-->`
18+
* which is invalid. [[https://www.w3.org/TR/xml11//#IDA5CES]]
1719
*/
1820
case class Comment(commentText: String) extends SpecialNode {
1921

@@ -22,8 +24,12 @@ case class Comment(commentText: String) extends SpecialNode {
2224
final override def doCollectNamespaces = false
2325
final override def doTransform = false
2426

25-
if (commentText contains "--")
27+
if (commentText.contains("--")) {
2628
throw new IllegalArgumentException("text contains \"--\"")
29+
}
30+
if (commentText.length > 0 && commentText.charAt(commentText.length - 1) == '-') {
31+
throw new IllegalArgumentException("The final character of a XML comment may not be '-'. See https://www.w3.org/TR/xml11//#IDA5CES")
32+
}
2733

2834
/**
2935
* Appends &quot;<!-- text -->&quot; to this string buffer.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package scala.xml
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Assert.assertTrue
5+
import org.junit.Test
6+
7+
final class CommentTest {
8+
9+
@Test(expected=classOf[IllegalArgumentException])
10+
def invalidCommentWithTwoDashes: Unit = {
11+
Comment("invalid--comment")
12+
}
13+
14+
@Test(expected=classOf[IllegalArgumentException])
15+
def invalidCommentWithFinalDash: Unit = {
16+
Comment("invalid comment-")
17+
}
18+
19+
@Test
20+
def validCommentWithDash: Unit = {
21+
val valid: String = "valid-comment"
22+
assertEquals(s"<!--${valid}-->", Comment(valid).toString)
23+
}
24+
25+
@Test
26+
def validEmptyComment: Unit = {
27+
val valid: String = ""
28+
assertEquals(s"<!--${valid}-->", Comment(valid).toString)
29+
}
30+
}

0 commit comments

Comments
 (0)