[wp-trac] [WordPress Trac] #57538: escape-html.js - escapeAmpersand(value) return value.replace is not a function when "value" is not a string

WordPress Trac noreply at wordpress.org
Tue Jan 24 07:40:55 UTC 2023


#57538: escape-html.js -  escapeAmpersand(value) return value.replace is not a
function when "value" is not a string
--------------------------+-----------------------------
 Reporter:  jrausell      |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  6.1.1
 Severity:  normal        |   Keywords:
  Focuses:  javascript    |
--------------------------+-----------------------------
 #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,
 '&');
 }

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57538>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list