File tree Expand file tree Collapse file tree 2 files changed +50
-18
lines changed
babel-plugin-jsx-dom-expressions/src/shared Expand file tree Collapse file tree 2 files changed +50
-18
lines changed Original file line number Diff line number Diff line change @@ -212,14 +212,30 @@ const ATTR_REGEX = /[&<"]/g,
212212
213213export function escapeHTML ( html , attr ) {
214214 if ( typeof html !== "string" ) return html ;
215- return html . replace ( attr ? ATTR_REGEX : CONTENT_REGEX , m => {
216- switch ( m ) {
217- case "&" :
218- return "&" ;
219- case "<" :
220- return "<" ;
221- case '"' :
222- return """ ;
215+ const match = ( attr ? ATTR_REGEX : CONTENT_REGEX ) . exec ( html ) ;
216+ if ( ! match ) return html ;
217+ let index = 0 ;
218+ let lastIndex = 0 ;
219+ let out = "" ;
220+ let escape = "" ;
221+ for ( index = match . index ; index < html . length ; index ++ ) {
222+ switch ( html . charCodeAt ( index ) ) {
223+ case 34 : // "
224+ if ( ! attr ) continue ;
225+ escape = """ ;
226+ break ;
227+ case 38 : // &
228+ escape = "&" ;
229+ break ;
230+ case 60 : // <
231+ escape = "<" ;
232+ break ;
233+ default :
234+ continue ;
223235 }
224- } ) ;
236+ if ( lastIndex !== index ) out += html . substring ( lastIndex , index ) ;
237+ lastIndex = index + 1 ;
238+ out += escape ;
239+ }
240+ return lastIndex !== index ? out + html . substring ( lastIndex , index ) : out ;
225241}
Original file line number Diff line number Diff line change @@ -240,16 +240,32 @@ const ATTR_REGEX = /[&<"]/g,
240240
241241export function escape ( html , attr ) {
242242 if ( typeof html !== "string" ) return html ;
243- return html . replace ( attr ? ATTR_REGEX : CONTENT_REGEX , m => {
244- switch ( m ) {
245- case "&" :
246- return "&" ;
247- case "<" :
248- return "<" ;
249- case '"' :
250- return """ ;
243+ const match = ( attr ? ATTR_REGEX : CONTENT_REGEX ) . exec ( html ) ;
244+ if ( ! match ) return html ;
245+ let index = 0 ;
246+ let lastIndex = 0 ;
247+ let out = "" ;
248+ let escape = "" ;
249+ for ( index = match . index ; index < html . length ; index ++ ) {
250+ switch ( html . charCodeAt ( index ) ) {
251+ case 34 : // "
252+ if ( ! attr ) continue ;
253+ escape = """ ;
254+ break ;
255+ case 38 : // &
256+ escape = "&" ;
257+ break ;
258+ case 60 : // <
259+ escape = "<" ;
260+ break ;
261+ default :
262+ continue ;
251263 }
252- } ) ;
264+ if ( lastIndex !== index ) out += html . substring ( lastIndex , index ) ;
265+ lastIndex = index + 1 ;
266+ out += escape ;
267+ }
268+ return lastIndex !== index ? out + html . substring ( lastIndex , index ) : out ;
253269}
254270
255271// Hydrate
You can’t perform that action at this time.
0 commit comments