Skip to content

Fix Invalid Comment Edge Case #441

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

Merged
merged 1 commit into from
Dec 5, 2020
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
8 changes: 7 additions & 1 deletion shared/src/main/scala/scala/xml/Comment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ package xml
*
* @author Burak Emir
* @param commentText the text contained in this node, may not contain "--"
* and the final character may not be `-` to prevent a closing span of `-->`
* which is invalid. [[https://www.w3.org/TR/xml11//#IDA5CES]]
*/
case class Comment(commentText: String) extends SpecialNode {

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

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

/**
* Appends &quot;<!-- text -->&quot; to this string buffer.
Expand Down
30 changes: 30 additions & 0 deletions shared/src/test/scala/scala/xml/CommentTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package scala.xml

import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

final class CommentTest {

@Test(expected=classOf[IllegalArgumentException])
def invalidCommentWithTwoDashes: Unit = {
Comment("invalid--comment")
}

@Test(expected=classOf[IllegalArgumentException])
def invalidCommentWithFinalDash: Unit = {
Comment("invalid comment-")
}

@Test
def validCommentWithDash: Unit = {
val valid: String = "valid-comment"
assertEquals(s"<!--${valid}-->", Comment(valid).toString)
}

@Test
def validEmptyComment: Unit = {
val valid: String = ""
assertEquals(s"<!--${valid}-->", Comment(valid).toString)
}
}