File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff 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 */
1820case 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 "<!-- text -->" to this string buffer.
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments