@@ -621,7 +621,7 @@ function replaceEntitiesValue(val, tagName, jPath) {
621621 }
622622
623623 // Replace DOCTYPE entities
624- for ( let entityName in this . docTypeEntities ) {
624+ for ( const entityName of Object . keys ( this . docTypeEntities ) ) {
625625 const entity = this . docTypeEntities [ entityName ] ;
626626 const matches = val . match ( entity . regx ) ;
627627
@@ -653,19 +653,38 @@ function replaceEntitiesValue(val, tagName, jPath) {
653653 }
654654 }
655655 }
656- if ( val . indexOf ( '&' ) === - 1 ) return val ; // Early exit
657-
658656 // Replace standard entities
659- for ( let entityName in this . lastEntities ) {
657+ for ( const entityName of Object . keys ( this . lastEntities ) ) {
660658 const entity = this . lastEntities [ entityName ] ;
659+ const matches = val . match ( entity . regex ) ;
660+ if ( matches ) {
661+ this . entityExpansionCount += matches . length ;
662+ if ( entityConfig . maxTotalExpansions &&
663+ this . entityExpansionCount > entityConfig . maxTotalExpansions ) {
664+ throw new Error (
665+ `Entity expansion limit exceeded: ${ this . entityExpansionCount } > ${ entityConfig . maxTotalExpansions } `
666+ ) ;
667+ }
668+ }
661669 val = val . replace ( entity . regex , entity . val ) ;
662670 }
663- if ( val . indexOf ( '&' ) === - 1 ) return val ; // Early exit
671+ if ( val . indexOf ( '&' ) === - 1 ) return val ;
664672
665673 // Replace HTML entities if enabled
666674 if ( this . options . htmlEntities ) {
667- for ( let entityName in this . htmlEntities ) {
675+ for ( const entityName of Object . keys ( this . htmlEntities ) ) {
668676 const entity = this . htmlEntities [ entityName ] ;
677+ const matches = val . match ( entity . regex ) ;
678+ if ( matches ) {
679+ //console.log(matches);
680+ this . entityExpansionCount += matches . length ;
681+ if ( entityConfig . maxTotalExpansions &&
682+ this . entityExpansionCount > entityConfig . maxTotalExpansions ) {
683+ throw new Error (
684+ `Entity expansion limit exceeded: ${ this . entityExpansionCount } > ${ entityConfig . maxTotalExpansions } `
685+ ) ;
686+ }
687+ }
669688 val = val . replace ( entity . regex , entity . val ) ;
670689 }
671690 }
0 commit comments