5
5
6
6
use std:: fmt;
7
7
8
+ use pulldown_cmark_escape:: FmtWriter ;
8
9
use unicode_segmentation:: UnicodeSegmentation ;
9
10
10
11
/// Wrapper struct which will emit the HTML-escaped version of the contained
@@ -13,31 +14,7 @@ pub(crate) struct Escape<'a>(pub &'a str);
13
14
14
15
impl fmt:: Display for Escape < ' _ > {
15
16
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
16
- // Because the internet is always right, turns out there's not that many
17
- // characters to escape: http://stackoverflow.com/questions/7381974
18
- let Escape ( s) = * self ;
19
- let pile_o_bits = s;
20
- let mut last = 0 ;
21
- for ( i, ch) in s. char_indices ( ) {
22
- let s = match ch {
23
- '>' => ">" ,
24
- '<' => "<" ,
25
- '&' => "&" ,
26
- '\'' => "'" ,
27
- '"' => """ ,
28
- _ => continue ,
29
- } ;
30
- fmt. write_str ( & pile_o_bits[ last..i] ) ?;
31
- fmt. write_str ( s) ?;
32
- // NOTE: we only expect single byte characters here - which is fine as long as we
33
- // only match single byte characters
34
- last = i + 1 ;
35
- }
36
-
37
- if last < s. len ( ) {
38
- fmt. write_str ( & pile_o_bits[ last..] ) ?;
39
- }
40
- Ok ( ( ) )
17
+ pulldown_cmark_escape:: escape_html ( FmtWriter ( fmt) , self . 0 )
41
18
}
42
19
}
43
20
@@ -51,29 +28,7 @@ pub(crate) struct EscapeBodyText<'a>(pub &'a str);
51
28
52
29
impl fmt:: Display for EscapeBodyText < ' _ > {
53
30
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
54
- // Because the internet is always right, turns out there's not that many
55
- // characters to escape: http://stackoverflow.com/questions/7381974
56
- let EscapeBodyText ( s) = * self ;
57
- let pile_o_bits = s;
58
- let mut last = 0 ;
59
- for ( i, ch) in s. char_indices ( ) {
60
- let s = match ch {
61
- '>' => ">" ,
62
- '<' => "<" ,
63
- '&' => "&" ,
64
- _ => continue ,
65
- } ;
66
- fmt. write_str ( & pile_o_bits[ last..i] ) ?;
67
- fmt. write_str ( s) ?;
68
- // NOTE: we only expect single byte characters here - which is fine as long as we
69
- // only match single byte characters
70
- last = i + 1 ;
71
- }
72
-
73
- if last < s. len ( ) {
74
- fmt. write_str ( & pile_o_bits[ last..] ) ?;
75
- }
76
- Ok ( ( ) )
31
+ pulldown_cmark_escape:: escape_html_body_text ( FmtWriter ( fmt) , self . 0 )
77
32
}
78
33
}
79
34
0 commit comments