stop encoding of href in anchor

M

meyerto

This:

use CGI qw:)standard);
print a({href=>'cats&dogs'},pets);

produces:

<a href="cats&amp;dogs">pets</a>

How can I stop the & from being encoded to &amp; ?
 
D

David Dorward

use CGI qw:)standard);
print a({href=>'cats&dogs'},pets);
produces:
<a href="cats&amp;dogs">pets</a>
How can I stop the & from being encoded to &amp; ?

Why would you want to?

"dogs" is not a named entity in HTML, and the context suggests you want & as
in "and" not & as in "The start of a character reference", in which case it
needs to be represeted as the entity.
 
J

John Bokma

This:

use CGI qw:)standard);
print a({href=>'cats&dogs'},pets);

produces:

<a href="cats&amp;dogs">pets</a>

How can I stop the & from being encoded to &amp; ?

Can you give any /good/ reason why this shouldn't happen?
 
M

meyerto

Why would you want to?

The sample I provided is simplified demostration of the problem. Here
is a more realistic example where you don't want the encoding to
happen:

a({href=>"record?uid=4&gid=6"}, "View record")
yields:
<a href="record?uid=4&amp;gid=6">View record</a>
but I want:
<a href="record?uid=4&gid=6">View record</a>
 
R

Randal L. Schwartz

meyerto> The sample I provided is simplified demostration of the problem. Here
meyerto> is a more realistic example where you don't want the encoding to
meyerto> happen:

meyerto> a({href=>"record?uid=4&gid=6"}, "View record")
meyerto> yields:
meyerto> <a href="record?uid=4&amp;gid=6">View record</a>
meyerto> but I want:
meyerto> <a href="record?uid=4&gid=6">View record</a>

You want something that isn't legal HTML and is broken? Why?

It's doing the right thing for you. Even if you don't want it. :)

print "Just another Perl hacker,"; # the original
 
X

xhoster

This:

use CGI qw:)standard);
print a({href=>'cats&dogs'},pets);

produces:

<a href="cats&amp;dogs">pets</a>

That's funny, it shouldn't. The docs say that only form-generating
functions autoEscape, and I wouldn't think that a() is a form-generating
function.
How can I stop the & from being encoded to &amp; ?

autoEscape(0) turns it off (as well as any other autoEscaping)

Xho
 
M

meyerto

autoEscape(0) turns it off (as well as any other autoEscaping)

Ah, autoEscape(), thanks Xho.

Just for the record though, this function doesn't appear to work in
older perl versions (or maybe older CGI modules). See:

[tmeyer@aztec tmeyer]$ perl #perl v5.8.0
use CGI ':standard';
autoEscape(0);
print a({href=>'dogs&cats'},yowza)."\n";
<a href="dogs&amp;cats">yowza</a>

Yet, with a newer perl on a different machine (and proabably different
CGI):

tmeyer@olof:~$ perl #perl v5.8.7
use CGI ':standard';
autoEscape(0);
print a({href=>'dogs&cats'},yowza)."\n";
<a href="dogs&cats">yowza</a>

Bah! Time to manually create anchor tags myself.
 
M

meyerto

Randal said:
meyerto> The sample I provided is simplified demostration of the problem. Here
meyerto> is a more realistic example where you don't want the encoding to
meyerto> happen:

meyerto> a({href=>"record?uid=4&gid=6"}, "View record")
meyerto> yields:
meyerto> <a href="record?uid=4&amp;gid=6">View record</a>
meyerto> but I want:
meyerto> <a href="record?uid=4&gid=6">View record</a>

You want something that isn't legal HTML and is broken? Why?

It's doing the right thing for you. Even if you don't want it. :)

print "Just another Perl hacker,"; # the original

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

LOL. You are further proof that you have to have a good sense of humor
to be a Perl hacker. :)
 
G

Gunnar Hjalmarsson

LOL. You are further proof that you have to have a good sense of humor
to be a Perl hacker. :)

Yes, it's pretty humorous that you still don't seem to understand what
people have been trying to explain to you.

Did you possibly try the 'unwanted' encoding in your browser?
 
M

meyerto

Yes, it's pretty humorous that you still don't seem to understand what
people have been trying to explain to you.

Did you possibly try the 'unwanted' encoding in your browser?

Actually, I do understand what some of these people are explaining, and
it appears that those people don't understand the issue. I definitely
don't want &amp; separating my query parameters. It breaks the link.
For example:

good:
http://www.autotrader.com/fyc/vdp.jsp?dealer_id=94476&car_id=210332809
broken:
http://www.autotrader.com/fyc/vdp.jsp?dealer_id=94476&amp;car_id=210332809
 
M

Martijn Lievaart

T

Tad McClellan

^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Actually, I do understand what some of these people are explaining,


Actually you do NOT.

Keep trying.

it appears that those people don't understand the issue.


It appears that those people do understand the issue (better than
you do).

There is not much point in asking for advice if you are going to
refuse to follow that advice...

I definitely
don't want &amp; separating my query parameters.


You definitely DO want &amp; separating your query parameters.

That is compliant with the relevant specifications.

It breaks the link.


How did you come to that conclusion?

By inspection of the HTML, or by *trying the 'unwanted' encoding
in your browser*?


You sure are spending a lot of time fixing a problem that is
not a problem at all...
 
X

xhoster

Actually, I do understand what some of these people are explaining, and
it appears that those people don't understand the issue. I definitely
don't want &amp; separating my query parameters. It breaks the link.
For example:

good:
http://www.autotrader.com/fyc/vdp.jsp?dealer_id=94476&car_id=210332809
broken:
http://www.autotrader.com/fyc/vdp.jsp?dealer_id=94476&amp;car_id=21033280
9

The second one is broken only if you put it there not in an href. If you
put both of those into <a href...> and save them in a html file, and then
open that file in a browser, the 2nd one in fact is not broken. (The first
one also isn't broken on the browsers I've tried, but that is only because
the browsers are generous and automatically fix it up for you.)

Xho
 
I

Ian Wilson

I'm using Mozilla Thunderbird to read this newsgroup. I hovered my mouse
over the "broken" link. The correct URL appeared in the status bar (i.e.
the URL you label as "good").

I then clicked the "broken" URL and Thunderbird launched my web-browser
and successfully opened the Auto Trader web page for a 2003 Audi S8
Quattro Sedan. This suggests that the car_id was transmitted and
interpreted correctly.
Did you actually try it? I did. http://ma.rtij.nl/amptest.html.

I tried your web page in both IE and FireFox, the "broken" link works fine.
 
B

Bart Lateur

I definitely
don't want &amp; separating my query parameters. It breaks the link.
For example:

good:
http://www.autotrader.com/fyc/vdp.jsp?dealer_id=94476&car_id=210332809
broken:
http://www.autotrader.com/fyc/vdp.jsp?dealer_id=94476&amp;car_id=210332809

The links don't work in literal text, and in the location bar. They DO
work in a html attribute.

That is because in HTML, attribute values need to be HTML-escaped. The
unescaped one only works by accident, because "&car_id" is not a known
entity, and browsers tend to fall back to keep the original text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top