question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ampersands get escaped in URLS

See original GitHub issue
  renderHeadToString({
    headTags: [
      {
        tag: 'script',
        props: {
          async: true,
          hid: 'maps-googleapis',
          src: 'https://maps.googleapis.com/maps/api/js?key=example&libraries=places',
        },
      },
    ],
  }).headTags

results in:

<script async hid="maps-googleapis" src="https://maps.googleapis.com/maps/api/js?key=example&amp;libraries=places"></script><meta name="head:count" content="1">

https://github.com/vueuse/head/blob/main/src/stringify-attrs.ts#L2-L32

https://github.com/vueuse/head/blob/main/src/index.ts#L347-L362

Here’s a reproduction.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

3reactions
ffxsamcommented, Aug 29, 2022

@stafyniaksacha This is my local patch (using yarn’s patch/patch-commit feature). You should be using the latest yarn to do this (yarn 3.2.3).

Add this to your package.json:

  "resolutions": {
    "@vueuse/head@^0.7.9": "patch:@vueuse/head@npm%3A0.7.9#./.yarn/patches/@vueuse-head-npm-0.7.9-ad2f8578c9.patch"
  },

Then create this file:

.yarn/patches/@vueuse-head-npm-0.7.9-ad2f8578c9.patch

diff --git a/dist/index.js b/dist/index.js
index bf5e5100a01ae054ccea4da4a12b0784fe0b5840..0722bb95bbce708354d8f3ea8b42ade4966b7ad2 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -74,7 +74,7 @@ var createElement = (tag, attrs, document) => {
 };
 
 // src/stringify-attrs.ts
-var htmlEscape = (str) => str.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/'/g, "&#39;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
+var htmlEscape = (str) => str.replace(/"/g, "&quot;").replace(/'/g, "&#39;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
 var stringifyAttrs = (attributes) => {
   const handledAttributes = [];
   for (let [key, value] of Object.entries(attributes)) {
diff --git a/dist/index.mjs b/dist/index.mjs
index 481b7343c20357bc198ebec977c5daaff0c25e24..ab8fbbf57c0c12d7482e498a468a11a4b564a869 100644
--- a/dist/index.mjs
+++ b/dist/index.mjs
@@ -58,7 +58,7 @@ var createElement = (tag, attrs, document) => {
 };
 
 // src/stringify-attrs.ts
-var htmlEscape = (str) => str.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/'/g, "&#39;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
+var htmlEscape = (str) => str.replace(/"/g, "&quot;").replace(/'/g, "&#39;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
 var stringifyAttrs = (attributes) => {
   const handledAttributes = [];
   for (let [key, value] of Object.entries(attributes)) {

Run yarn, and you should be good to go!

1reaction
acidjazzcommented, Sep 19, 2022

Does the escaping actually cause issues with loading the URL though?

My understanding and testing it seems like it works fine escaped

It breaks any functionality of needing to pass parameters to JS libraries, which is how all of googles API’s work

Read more comments on GitHub >

github_iconTop Results From Across the Web

Escaping ampersand in URL - Stack Overflow
The digits, preceded by a percent sign ("%") which is used as an escape character, are then used in the URI in place...
Read more >
Escape Ampersand in URL in JavaScript - Java2Blog
In this post, we will see how to escape ampersand in JavaScript. To escape Ampersand in JavaScript, use encodeURIComponent() in JavaScript. ... As...
Read more >
URL escape codes
Character URL Escape Codes String Literal Escape Code SPACE %20 $20 < %3C $3C > %3E $3E
Read more >
Brief Introduction to URL Encoding - ThoughtCo
These encoded examples are different from what you find with HTML special characters. For example, to encode a URL with an ampersand ......
Read more >
URL ampersand escape xdg-open command
I am trying to open a URL using the xdg-open command, but I have tried escaping the ampersand character to no avail; all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found