Storytime
In Play's templating scheme, I have this simplified setup:
@(param:String)
<!DOCTYPE html>
<html> <head><!-- JQuery & Bootstrap css+js includes --></head> <body>
@param
<input type="text" data-provide="typeahead" data-source='@Html(param)' data-items="4">
</body> </html>
The @Html() is mentioned here and the bottom of here and basically prevents characters like <
from being replaced with <
. I'm attempting to pass in a Json.stringify
-ed and Json.toJson
-ed List[String]
that I get from my database into the HTML through Play's template engine, and then have Bootstrap pick it up automatically from the data-source
attribute.
Say @param evaluates to a JSON object that contains a string with a '
:
<input data-provide="typeahead" data-source='["can't","hi","boom"]' data-items="4" type="text" >
I realized that the single quote characters needed to be escaped in my data-source JSON object. At first I experimented with using \
and even \\\
to no avail. I even set out to write a regex replacer for the task to emulate the addSlashes() mentioned here
Then on a whim...
<input data-provide="typeahead" data-source='["can't","hi", "boom"]' data-items="4" type="text" >
Everything works normally now! (when the data-source is hardcoded. Still need to figure out how to unescape @Html() so that '
doesn't disappear.)
Question
Why does Bootstrap Typeahead need to read in the data-source with the single-quote characters unescaped?
For posterity:
val quoteRegex = """'""".r
quoteRegex.replaceAllIn(str, m => "'")