How to percent-encode in soy

139 views
Skip to first unread message

thomasmu...@yahoo.com

unread,
Sep 6, 2017, 3:31:28 PM9/6/17
to Closure Templates Discuss
Hi, im wondering how do i do percent-encoding please?

Reason why i ask is downstrream (wmf) found that in velocity same for soy in my testing that doing + was relplaced with ' ' (not strings but a space).

I am currently trying to add support for soy here https://gerrit-review.googlesource.com/#/c/plugins/its-base/+/108215/

But i am wondering how do i do percent encoding?

the template looks like

{template .PatchSetCreated autoescape="strict" kind="text"}

  {if $uploaderName != $authorName and $uploaderUsername != $authorUsername}{\n}

    Change {$changeNumber} had a related patch set uploaded (by {$uploaderName}; owner: {$authorName}):{\n}

    [{$project}@{$branch}] {$subject}{\n}

  {else}

    Change {$changeNumber} had a related patch set uploaded (by {$uploaderName}):{\n}

    [{$project}@{$branch}] {$subject}{\n}

  {/if}

{/template}

Mike Samuel

unread,
Sep 6, 2017, 3:47:15 PM9/6/17
to closure-temp...@googlegroups.com
Responses inline.

On Wed, Sep 6, 2017 at 2:22 PM, thomasmulhall410 via Closure Templates
Discuss <closure-temp...@googlegroups.com> wrote:
> Hi, im wondering how do i do percent-encoding please?


Normally, with type="html" instead of type="text", the autoescaper
would add that for you in appropriate contexts.

You could always call out to a template of kind="uri" which would have
interpolations properly escaped.

If that doesn't work for you, then take a look at the escaping
directives, especially |escapeUri and |normalizeUri, defined in
https://github.com/google/closure-templates/blob/6a6a54ff55e1ab6e09dd24703825d5971f450eaa/java/src/com/google/template/soy/basicdirectives/BasicEscapeDirective.java#L327
So
{$foo |escapeUri}
should replace any URL meta-characters with the UTF-8 %-encoded
equivalent like the JS builtin encodeURIComponent.

We don't really advertise them since we prefer that people let the
autoescaper do the job, but the names haven't changed in years.


> Reason why i ask is downstrream (wmf) found that in velocity same for soy in
> my testing that doing + was relplaced with ' ' (not strings but a space).

I'm afraid I don't understand any of that.
Are you sure the problem isn't that something is %-decoding something
that they shouldn't be?

> I am currently trying to add support for soy here
> https://gerrit-review.googlesource.com/#/c/plugins/its-base/+/108215/
>
> But i am wondering how do i do percent encoding?
>
> the template looks like
>
> {template .PatchSetCreated autoescape="strict" kind="text"}
>
> {if $uploaderName != $authorName and $uploaderUsername !=
> $authorUsername}{\n}
>
> Change {$changeNumber} had a related patch set uploaded (by
> {$uploaderName}; owner: {$authorName}):{\n}
>
> [{$project}@{$branch}] {$subject}{\n}
>
> {else}
>
> Change {$changeNumber} had a related patch set uploaded (by
> {$uploaderName}):{\n}
>
> [{$project}@{$branch}] {$subject}{\n}
>
> {/if}
>
> {/template}

Which part of this template should be %-escaped? It looks like a text
email body and to my eyes none of the interpolations are obviously
part of a URL.


> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "Closure Templates Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to closure-templates-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

thomasmu...@yahoo.com

unread,
Sep 6, 2017, 3:51:29 PM9/6/17
to Closure Templates Discuss
Thanks. Will try that :)

thomasmu...@yahoo.com

unread,
Sep 6, 2017, 4:51:31 PM9/6/17
to Closure Templates Discuss
Thankyou doing escapeUri worked :).


On Wednesday, September 6, 2017 at 8:31:28 PM UTC+1, thomasmu...@yahoo.com wrote:
Reply all
Reply to author
Forward
0 new messages