0

I am supporting some existing code where the original author used StringEscapeUtils to escape Javascript for the contents of a list like so...

def myList = ["Easy Installation", "Good Price / Value", "Attractive Design"]
myList = myList.collect { StringEscapeUtils.escapeJavaScript(it) }
println("Result is ${myList.join(',')}")

Of course what is printed out is..

Result is Easy Installation,Good Price \/ Value,Attractive Design

First question is why is it trying to escape the forward slash? According to this post, the forward slash is harmless in Javascript (and I assume HTML too).

Second question, can anything be done about it (short of not escaping of course) so forward slashes don't appear as "V"s?

Community
  • 1
  • 1
AbuMariam
  • 3,282
  • 13
  • 49
  • 82

1 Answers1

0

StringEscapeUtils is escaping your forward slashes because it's designed for escaping JSON strings, and escaping forward slashes is an option in JSON.


It's not clear from your code snippet why StringEscapeUtils.escapeJavaScript() is being called. But you could shorten the code to:

def myList = ["Easy Installation", "Good Price / Value", "Attractive Design"]
myList = myList.collect StringEscapeUtils.&escapeJavaScript
println("Result is ${myList.join(',')}")
Armand
  • 23,463
  • 20
  • 90
  • 119
  • Is there any way to turn this off for StringEscapeUtils? – AbuMariam May 16 '16 at 13:11
  • Why are you "escaping" the string in the first place? And please upvote if you found my answer helpful. – Armand May 16 '16 at 13:12
  • I don't know why, as I said its someone else's code and I don't want to remove that for fear of breaking something else (maybe it was used for something else besides a forward slash). I would love to upvote and accept your answer if can help with my second question. What to do about it? How to keep that code but still prevent users from seeing a "V"? – AbuMariam May 16 '16 at 13:19