• Andrew Nicols's avatar
    MDL-52136 core: Add support for quoting variables in mustache helpers · 0b4bff8c
    Andrew Nicols authored
    This is required for when helpers include json-encoded variables as arguments.
    As an example, imagine a template with content:
    {{# str }} somekey, someidentifier, { "fullname": "{{ fullname }}" } {{/ str }}
    If the fullname variable were to include the double-quote character (e.g.
    John "Trevor" Doe) because of the way in which mustache renders content, it
    would become:
    {{# str }} somekey, someidentifier, { "fullname": "John "Trevor" Doe" } {{/ str }}
    This results in an invalid JSON structure.
    To work around this issue, the quote characters in the passed variable
    must be escaped:
    {{# str }} somekey, someidentifier, { "fullname": "John \"Trevor\" Doe" } {{/ str }}
    Unfortunately, Mustache provides no way of doing so natively.
    With this function, we can quote the text as appropriate:
    {{# str }} somekey, someidentifier, { "fullname": {{# quote }}{{ fullname }}{{/ quote }} } {{/ str }}
    This also handles the case where the quoted content includes the Mustache
    delimeter ({{ or }}).
    For example:
    fullname = 'John "}}Trevor{{" Doe'
    Ordinarily this would be rendered as:
    {{# str }} somekey, someidentifier, { "fullname": "John "}}Trevor{{" Doe" } {{/ str }}
    This rendering is both a JSON error, and also a mustache syntax error because of the mustache delimeters.
    The quote helper also escapes these by wrapping them in change delimeter
    {{# str }} somekey, someidentifier, { "fullname": "John "{{=<% %>=}}}}<%={{ }}=%>Trevor{{=<% %>=}}{{{{=<% %>=}}" Doe" } {{/ str }}