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 Block: Not handling JavaScript properly, escaping unexpectedly #25928

Closed
scosgro opened this issue Oct 7, 2020 · 1 comment
Closed
Labels
[Type] WP Core Ticket Requires an upstream change from WordPress. Core Trac ticket should be linked.

Comments

@scosgro
Copy link

scosgro commented Oct 7, 2020

Describe the bug
Using a very popular example, create a PayPal Smart Button.

https://www.paypal.com/buttons/smart

The code will look something like this:

<div id="smart-button-container">
    <div style="text-align: center"><label for="description">Test </label><input type="text" name="descriptionInput" id="description" maxlength="127" value=""></div>
      <p id="descriptionError" style="visibility: hidden; color:red; text-align: center;">Please enter a description</p>
    <div style="text-align: center"><label for="amount">test amount </label><input name="amountInput" type="number" id="amount" value="" ><span> USD</span></div>
      <p id="priceLabelError" style="visibility: hidden; color:red; text-align: center;">Please enter a price</p>
    <div id="invoiceidDiv" style="text-align: center; display: none;"><label for="invoiceid">testinvoice </label><input name="invoiceid" maxlength="127" type="text" id="invoiceid" value="" ></div>
      <p id="invoiceidError" style="visibility: hidden; color:red; text-align: center;">Please enter an Invoice ID</p>
    <div style="text-align: center; margin-top: 0.625rem;" id="paypal-button-container"></div>
  </div>
  <script src="https://www.paypal.com/sdk/js?client-id=sb&currency=USD" data-sdk-integration-source="button-factory"></script>
  <script>
  function initPayPalButton() {
    var description = document.querySelector('#smart-button-container #description');
    var amount = document.querySelector('#smart-button-container #amount');
    var descriptionError = document.querySelector('#smart-button-container #descriptionError');
    var priceError = document.querySelector('#smart-button-container #priceLabelError');
    var invoiceid = document.querySelector('#smart-button-container #invoiceid');
    var invoiceidError = document.querySelector('#smart-button-container #invoiceidError');
    var invoiceidDiv = document.querySelector('#smart-button-container #invoiceidDiv');

    var elArr = [description, amount];

    if (invoiceidDiv.firstChild.innerHTML.length > 1) {
      invoiceidDiv.style.display = "block";
    }

    var purchase_units = [];
    purchase_units[0] = {};
    purchase_units[0].amount = {};

    function validate(event) {
      return event.value.length > 0;
    }

    paypal.Buttons({
      style: {
        color: 'gold',
        shape: 'rect',
        label: 'paypal',
        layout: 'vertical',
        
      },

      onInit: function (data, actions) {
        actions.disable();

        if(invoiceidDiv.style.display === "block") {
          elArr.push(invoiceid);
        }

        elArr.forEach(function (item) {
          item.addEventListener('keyup', function (event) {
            var result = elArr.every(validate);
            if (result) {
              actions.enable();
            } else {
              actions.disable();
            }
          });
        });
      },

      onClick: function () {
        if (description.value.length < 1) {
          descriptionError.style.visibility = "visible";
        } else {
          descriptionError.style.visibility = "hidden";
        }

        if (amount.value.length < 1) {
          priceError.style.visibility = "visible";
        } else {
          priceError.style.visibility = "hidden";
        }

        if (invoiceid.value.length < 1 && invoiceidDiv.style.display === "block") {
          invoiceidError.style.visibility = "visible";
        } else {
          invoiceidError.style.visibility = "hidden";
        }

        purchase_units[0].description = description.value;
        purchase_units[0].amount.value = amount.value;

        if(invoiceid.value !== '') {
          purchase_units[0].invoice_id = invoiceid.value;
        }
      },

      createOrder: function (data, actions) {
        return actions.order.create({
          purchase_units: purchase_units,
        });
      },

      onApprove: function (data, actions) {
        return actions.order.capture().then(function (details) {
          alert('Transaction completed by ' + details.payer.name.given_name + '!');
        });
      },

      onError: function (err) {
        console.log(err);
      }
    }).render('#paypal-button-container');
  }
  initPayPalButton();
  </script>

Add that to a Custom HTML Block, publish the page, then try viewing the page on a Chromium based browser.

The line:

if (invoiceid.value.length < 1 && invoiceidDiv.style.display === "block") {

Will get rendered in the browser (Chromium based) as:

if (invoiceid.value.length < 1 &#038;&#038; invoiceidDiv.style.display === "block") {

And in most cases, will not be interpreted on the page properly (so far, it seems hit or miss as to whether a Chromium based browser will show the PayPal buttons). Firefox seems to work without any issues.

Expected behavior
The code added in the Custom HTML Block to be rendered properly

Screenshots
Chrome console errors:
image
image

How the page shows in Chrome:
image

vs how it shows in Firefox:
image

Editor version (please complete the following information):

  • WordPress version: 5.5.1
  • Does the website has Gutenberg plugin installed, or is it using the block editor that comes by default? Both/either.
  • If the Gutenberg plugin is installed, which version is it? Version 9.0.0

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser: Chrome Version 86.0.4240.75 (Official Build) (64-bit) and Edge Version 85.0.564.70 (Official build) (64-bit)
@talldan
Copy link
Contributor

talldan commented Oct 9, 2020

This was previously reported in #13186, but it's considered a bug in WordPress core (something that can't be fixed in this codebase), so it's being tracked in core trac here - https://core.trac.wordpress.org/ticket/49480.

@talldan talldan closed this as completed Oct 9, 2020
@talldan talldan added the [Type] WP Core Ticket Requires an upstream change from WordPress. Core Trac ticket should be linked. label Oct 9, 2020
@scosgro scosgro changed the title Custom HTML Block: Not handling JavaScript properly, escapting unexpectedly Custom HTML Block: Not handling JavaScript properly, escaping unexpectedly Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[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

2 participants