Make WordPress Core

Opened 23 months ago

Closed 23 months ago

Last modified 21 months ago

#57538 closed defect (bug) (invalid)

escape-html.js - escapeAmpersand(value) return value.replace is not a function when "value" is not a string

Reported by: jrausell's profile jrausell Owned by:
Milestone: Priority: normal
Severity: normal Version: 6.1.1
Component: General Keywords:
Focuses: javascript Cc:

Description

#Method affected:

/escape-html.js

function escapeAmpersand(value) {
  return value.replace(/&(?!([a-z0-9]+|#[0-9]+|#x[a-f0-9]+);)/gi, '&');
}

#Error:
value.replace is not a function

#How to test/reproduce the error:
Developing a plugin for Gutenberg Rich-Text Package , use the method "create" to create a new RichTextValue using the html parameter instead of text and "insert" or "replace" the current value whit the new one. If the html passed contains a number the escapeAmpersan() will return the error at some point.

#example:

//My plugin test
function processPrice({
   value,
   onChange,
   onFocus,
   isActive,
   activeAttributes,
   contentRef
}) {
      const newText = '<span>USD</span><span>100</span>';

      const formatType = {
         type: myCustomType,
         attributes: {
            price: newPrice,
            currency: newCurrency
         },
      };

      const newRichText = create({ 
         html: newText
      });
      
      const toInsert = applyFormat(
         newRichText,
         formatType,
         0,
         newRichText.text.length
      );


      console.log('toInsert', toInsert)

      return insert( value, toInsert );
}

#Solution:
if value is not a string, convert to string

function escapeAmpersand(value) {
  if(typeof value.replace !== 'function'){
    value = value.toString();
  }
  return value.replace(/&(?!([a-z0-9]+|#[0-9]+|#x[a-f0-9]+);)/gi, '&amp;');
}

Change History (6)

#1 @ashrafulsarkar
23 months ago

@jrausell what is the actual path this file?

#2 @jrausell
23 months ago

/wp-includes/js/dist/escape-html.js?ver=6cf743ecc1ac531a8ee6
Line 101

github : https://github.com/WordPress/WordPress/blob/master/wp-includes/js/dist/escape-html.js

There are other 3 function that may have the same problem: escapeQuotationMark() escapeLessThan()

Version 0, edited 23 months ago by jrausell (next)

#3 @jrausell
23 months ago

Sorry,
trying to find the path found the error comes from a Gutenberg plugin file.
Will report the bug there.

#4 @ashrafulsarkar
23 months ago

ok. close this ticket and it's not WordPress issue.

#5 @ashrafulsarkar
23 months ago

  • Resolution set to invalid
  • Status changed from new to closed

#6 @SergeyBiryukov
21 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.