@@ -242,6 +242,12 @@ impl<'a> Codegen<'a> {
242
242
self . code . print_str ( s) ;
243
243
}
244
244
245
+ /// Push `char` into the buffer.
246
+ #[ inline]
247
+ pub fn print_char ( & mut self , ch : char ) {
248
+ self . code . print_char ( ch) ;
249
+ }
250
+
245
251
/// Print a single [`Expression`], adding it to the code generator's
246
252
/// internal buffer. Unlike [`Codegen::build`], this does not consume `self`.
247
253
#[ inline]
@@ -578,14 +584,7 @@ impl<'a> Codegen<'a> {
578
584
579
585
fn print_string_literal ( & mut self , s : & StringLiteral < ' _ > , allow_backtick : bool ) {
580
586
self . add_source_mapping ( s. span ) ;
581
- if s. lone_surrogates {
582
- self . print_str ( s. raw . unwrap ( ) . as_str ( ) ) ;
583
- return ;
584
- }
585
- self . print_quoted_utf16 ( s, allow_backtick) ;
586
- }
587
587
588
- fn print_quoted_utf16 ( & mut self , s : & StringLiteral < ' _ > , allow_backtick : bool ) {
589
588
let quote = if self . options . minify {
590
589
let mut single_cost: i32 = 0 ;
591
590
let mut double_cost: i32 = 0 ;
@@ -680,6 +679,26 @@ impl<'a> Codegen<'a> {
680
679
}
681
680
self . print_ascii_byte ( b'$' ) ;
682
681
}
682
+ '\u{FFFD}' if s. lone_surrogates => {
683
+ // If `lone_surrogates` is set, string contains lone surrogates which are escaped
684
+ // using the lossy replacement character (U+FFFD) as an escape marker.
685
+ // The lone surrogate is encoded as `\u{FFFD}XXXX` where `XXXX` is the code point as hex.
686
+ let hex1 = chars. next ( ) . unwrap ( ) ;
687
+ let hex2 = chars. next ( ) . unwrap ( ) ;
688
+ let hex3 = chars. next ( ) . unwrap ( ) ;
689
+ let hex4 = chars. next ( ) . unwrap ( ) ;
690
+ if [ hex1, hex2, hex3, hex4] == [ 'f' , 'f' , 'f' , 'd' ] {
691
+ // Actual lossy replacement character
692
+ self . print_char ( '\u{FFFD}' ) ;
693
+ } else {
694
+ // Lossy replacement character representing a lone surrogate
695
+ self . print_str ( "\\ u" ) ;
696
+ self . print_char ( hex1) ;
697
+ self . print_char ( hex2) ;
698
+ self . print_char ( hex3) ;
699
+ self . print_char ( hex4) ;
700
+ }
701
+ }
683
702
_ => self . print_str ( c. encode_utf8 ( [ 0 ; 4 ] . as_mut ( ) ) ) ,
684
703
}
685
704
}
0 commit comments