#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 | 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, '&'); }
Change History (6)
#2
@
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()
@jrausell what is the actual path this file?