Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom HTML with <script> incorrectly encodes & character #13186

Closed
mattbishop opened this issue Jan 4, 2019 · 4 comments
Closed

Custom HTML with <script> incorrectly encodes & character #13186

mattbishop opened this issue Jan 4, 2019 · 4 comments
Labels
[Block] HTML Affects the the HTML Block Needs Testing Needs further testing to be confirmed. [Type] WP Core Ticket Requires an upstream change from WordPress. Core Trac ticket should be linked.

Comments

@mattbishop
Copy link

mattbishop commented Jan 4, 2019

Describe the bug
If the javascript code in a <script> block contains an & operator, it will be converted to &#038, which browser flags as a syntax error. The script will not work.

I am using WP 5.0.2, Gutenberg updated Jan 3, 2019 (don't know where to find the version), hosted on easyWP.

Example:

<script>
  const hex = [];
  for (let i = 0; i < 256; i++) {
    hex[i] = (i < 16 ? '0' : '') + (i).toString(16);
  }
  function generateUUID() {
    const r = crypto.getRandomValues(new Uint8Array(16));
    r[6] = r[6] & 0x0f | 0x40;
    r[8] = r[8] & 0x3f | 0x80;
    const h = Array.from(r).map(i => hex[i]);
    return `${h[0]}${h[1]}${h[2]}${h[3]}-${h[4]}${h[5]}-${h[6]}${h[7]}-${h[8]}${h[9]}-${h[10]}${h[11]}${h[12]}${h[13]}${h[14]}${h[15]}`;
  }
</script>

To Reproduce
Steps to reproduce the behavior:

  1. Go to a Page
  2. Click on 'add Custom HTML'
  3. Paste the above HTML <script> snippet into the block.
  4. Preview the change
  5. Open the JavaScript console. You will see a syntax error: "Invalid Character: '#'":
    r[6] = r[6] &#038; 0x0f | 0x40;
    r[8] = r[8] &#038; 0x3f | 0x80;

Expected behavior
I expect no syntax errors. The script should be rendered without any encoding of characters.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Mac OS X 10.14
  • Browser chrome and safari
  • Version chrome 71.0.3578.98
@swissspidy swissspidy added Needs Testing Needs further testing to be confirmed. [Block] HTML Affects the the HTML Block labels Jan 4, 2019
@mattbishop
Copy link
Author

An interesting workaround WRT this snippet is to define the hex const after generateUUID(). This script block works as expected:

<script>
  function generateUUID() {
    const r = crypto.getRandomValues(new Uint8Array(16));
    r[6] = r[6] & 0x0f | 0x40;
    r[8] = r[8] & 0x3f | 0x80;
    const h = Array.from(r).map(i => hex[i]);
    return `${h[0]}${h[1]}${h[2]}${h[3]}-${h[4]}${h[5]}-${h[6]}${h[7]}-${h[8]}${h[9]}-${h[10]}${h[11]}${h[12]}${h[13]}${h[14]}${h[15]}`;
  }

  const hex = [];
  for (let i = 0; i < 256; i++) {
    hex[i] = (i < 16 ? '0' : '') + (i).toString(16);
  }
</script>

@finip
Copy link

finip commented Jan 12, 2019

i think the problem we have is the same. we used wp:html before. if I want to confirm the misplacement, you should have the same reason as wp:code.
the same question:
#13218

@meceware
Copy link

Hi,

This is still open with WP 5.1.1, Gutenberg updated.

@talldan
Copy link
Contributor

talldan commented Feb 20, 2020

This was identified as a WordPress core issue in a triage session (https://wordpress.slack.com/archives/C02QB2JS7/p1579758213000600).

I've reported this upstream in trac (https://core.trac.wordpress.org/ticket/49480).

@talldan talldan closed this as completed Feb 20, 2020
@talldan talldan added the [Type] WP Core Ticket Requires an upstream change from WordPress. Core Trac ticket should be linked. label Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] HTML Affects the the HTML Block Needs Testing Needs further testing to be confirmed. [Type] WP Core Ticket Requires an upstream change from WordPress. Core Trac ticket should be linked.
Projects
None yet
Development

No branches or pull requests

5 participants