How can I include an ampersand (&) in the SendGrid code editor?

0

I'm adding a custom link to a SendGrid template.

Part of the link is some url query parameters. They're separated by an ampersand (&), like so:

<a href="www.example.com?arg1=foo&arg2=bar">My Link</a>

Sendgrid keeps auto-updating the string above, escaping the character and creating this:

<a href="www.example.com?arg1=foo&amp;arg2=bar">My Link</a>

This is not the same link at all, and I would like it to not force character escaping in my custom html code.

And no, these two links are not the same

const t1 = new URLSearchParams("a=1&amp;b=2");
console.log(t1.get("a")); // 1
console.log(t1.get("b")); // null
console.log(t1.get("amp;b")); // 2

const t2 = new URLSearchParams("a=1&b=2");
console.log(t2.get("a")); // 1
console.log(t2.get("b")); // 2

How can I make SendGrid allow me to use a ampersand?

Seph Reed

Posted 2019-05-15T18:59:11.057

Reputation: 225

Does it end up in the raw generated email as &, &amp;, or &amp;amp;? – user1686 – 2019-05-15T19:36:32.310

&amp;. Exactly as shown above – Seph Reed – 2019-05-15T19:44:12.530

1Then there is no problem. That is what an HTML file is supposed to have. – user1686 – 2019-05-15T19:49:36.537

I'm working with a url, not an html file. Escaping characters is something you do for text in an html file, and certain characters in url sometimes. This is not one of those cases, I'd like the characters to not be escaped. See my edit for details on why having &amp; does not work. – Seph Reed – 2019-05-15T20:25:36.377

Might not be an issue but are your actual links like <a href="www…? Where is the HTTP or HTTPS? Should it not be <a href="http://www… or <a href="https://www…? – JakeGould – 2019-05-15T20:38:05.960

@Seph: If it has <a href around it, it is HTML. Your JS console example doesn't seem relevant, because it does not go through the HTML parser which is what the entity ref is for. – user1686 – 2019-05-15T20:40:15.983

The page loads just fine, it's just the search params which are flawed.

@grawity Check out this link https://www.google.com/?a=hat&q=test. Then try relpacing &amp; with &.

– Seph Reed – 2019-05-16T14:55:06.880

You keep missing the point... the &amp; is not part of the URL, it's part of the HTML. Just like the HTML generator converts & to &amp;, the HTML parser converts it back to a lone & when showing the link. If you deliberately add it to the raw URL, bypassing the HTML parser, of course it won't work because that's not what it's for. – user1686 – 2019-05-16T15:15:55.607

If it was acting as just html, yes (https://jsfiddle.net/d6Lb9an3/). But I=it ends up becoming part of the url though. I'm not lying about this. If I click the link, the url has &amp; in it, and is a broken link.

In any case, I've switched over to Postmark, so it's not really something I need anymore, but without the ability to add a non-exited ampersand, my links were broken.

– Seph Reed – 2019-05-16T15:37:28.810

Answers

-1

This is normal and does not change the meaning of the URL, as it will be automatically decoded back to & when the HTML document is parsed.

Generally, in HTML and XHTML, a lone ampersand must be written as a 'character entity reference' in order to avoid possible confusion with actual HTML entity references (such as "&lt;" or "&quot;"). All parsers recognize the named reference &amp; although variants such as &#38; are also legal.

HTML 4:

Authors should use "&amp;" (ASCII decimal 38) instead of "&" to avoid confusion with the beginning of a character reference (entity reference open delimiter). Authors should also use "&amp;" in attribute values since character references are allowed within CDATA attribute values.

HTML 5:

Attribute values are a mixture of text and character references, except with the additional restriction that the text cannot contain an ambiguous ampersand. [...]

8.2.4.72. Character reference state

If no match can be made and the temporary buffer consists of a U+0026 AMPERSAND character (&) followed by a sequence of one or more alphanumeric ASCII characters and a U+003B SEMICOLON character (;), then this is a parse error. [...] If the last character matched is not a U+003B SEMICOLON character (;), this is a parse error.

XHTML 1:

In order to ensure that documents are compatible with historical HTML user agents and XML-based user agents, ampersands used in a document that are to be treated as literal characters must be expressed themselves as an entity reference (e.g. "&amp;"). For example, when the href attribute of the a element refers to a CGI script that takes parameters, it must be expressed as http://my.site.dom/cgi-bin/myscript.pl?class=guest&amp;name=user rather than as http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user.

user1686

Posted 2019-05-15T18:59:11.057

Reputation: 283 655