File tree 2 files changed +37
-1
lines changed
2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,8 @@ package xml
14
14
*
15
15
* @author Burak Emir
16
16
* @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 ]]
17
19
*/
18
20
case class Comment (commentText : String ) extends SpecialNode {
19
21
@@ -22,8 +24,12 @@ case class Comment(commentText: String) extends SpecialNode {
22
24
final override def doCollectNamespaces = false
23
25
final override def doTransform = false
24
26
25
- if (commentText contains " --" )
27
+ if (commentText. contains( " --" )) {
26
28
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
+ }
27
33
28
34
/**
29
35
* 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