2

I have a multi-level hash in which I have a user's avatar's URL.

I am trying to make a cell with "Jane Doe image" but when the code runs the URL is getting changed. When I interpolate it into an Embperl template,

https://foo.com/useravatar?size=small&id=11111

turns into

https://foo.com/useravatar%3Fsize%3Dsmall%26id%3D11111

As you can see, the special characters become encoded and so the image is not found. How do you get around this?

use strict;
use warnings;

use Embperl qw( );

our $issue = {
   avatar => {
      url => 'https://foo.com/useravatar?size=small&id=11111',
   },
};

my $template = <<'__EOI__';
[+ $issue->{avatar}{url} +]
<img src="[+ $issue->{avatar}{url} +]">
__EOI__

Embperl::Execute({
   input   => \$template,
   package => __PACKAGE__,
});

This produces:

https://foo.com/useravatar?size=small&amp;id=11111
<img src="https://foo.com/useravatar%3Fsize%3Dsmall%26id%3D11111">

When the URL is inserted outside of the src attribute, it's correctly escaped. But when it's inserted inside of the src attribute, it gets mangled.

ikegami
  • 367,544
  • 15
  • 269
  • 518
Iluvatar14
  • 699
  • 1
  • 5
  • 18
  • I added some code, but I didn't think that would help here. – Iluvatar14 Feb 25 '15 at 18:07
  • 1
    There's a bit of an art to asking a good question. You need to supply sufficient information that someone else can figure out what you mean, look at places where it might be going wrong and suggest solutions. The less information you give, the more reliant you are on someone hitting _exactly the same_ problem, and knowing what they did to fix it. That's why is [ask] is there - to give guidance on how to get the best response. People on SO _like_ questions they can answer - they wouldn't be here otherwise. – Sobrique Feb 25 '15 at 19:34

1 Answers1

4

The escaping you noticed is designed to handle

<img src="https://foo.com/useravatar?size=small&amp;id=[+ $avatar_id +]">

You can change the escaping mode. Use

<img src="[+ do { local $escmode = 1; $issue->{avatar}{url} } +]">

to produce

<img src="https://foo.com/useravatar?size=small&amp;id=11111">

to fetch the image at URL

https://foo.com/useravatar?size=small&id=11111

Test:

use strict;
use warnings;

use Embperl qw( );

our $issue = {
   avatar => {
      url => 'https://foo.com/useravatar?size=small&id=11111',
   },
};

my $template = <<'__EOI__';
<img src="[+ $issue->{avatar}{url} +]">
<img src="[+ do { local $escmode = 1; $issue->{avatar}{url} } +]">
__EOI__

Embperl::Execute({
   input   => \$template,
   package => __PACKAGE__,
});
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Not criticizing your answer, but is there no better way to do that? That looks subpar and messy. Of course, I would not be surprised to believe that is the "Embperl authorized" way. – Dre Feb 25 '15 at 21:39
  • 1
    I intentionally picked the more complex solution just in case you asked me to simplify it! – ikegami Feb 25 '15 at 21:50
  • 1
    If you don't build any urls like ``, you can set it globally instead of locally (e.g. `[- $escmode = 1 -]`. – ikegami Feb 25 '15 at 21:51
  • Am not the original question asker, just saw this as a driveby. – Dre Feb 25 '15 at 22:02