1 | use core::str::from_utf8_unchecked; |
2 | |
3 | use alloc::borrow::Cow; |
4 | use alloc::string::String; |
5 | use alloc::vec::Vec; |
6 | |
7 | #[cfg (feature = "std" )] |
8 | use std::io::{self, Write}; |
9 | |
10 | macro_rules! parse_style_comment { |
11 | ($e:expr, $step:ident, $b:block, $bq:block, $bc:block $(, $($addi:expr),+)?) => { |
12 | match $step { |
13 | 0 => { |
14 | match $e { |
15 | b'<' => $step = 1, |
16 | $(b' \\' => $step = 100, |
17 | $(| $addi)+ => $bq,)? |
18 | _ => (), |
19 | } |
20 | } |
21 | 1 => { |
22 | match $e { |
23 | b'/' => $step = 2, |
24 | b'!' => $step = 10, |
25 | $(b' \\' => $step = 100, |
26 | $(| $addi)+ => { |
27 | $step = 0; |
28 | $bq |
29 | },)? |
30 | _ => $step = 0, |
31 | } |
32 | } |
33 | 2 => { |
34 | match $e { |
35 | b's' | b'S' => $step = 3, |
36 | $(b' \\' => $step = 100, |
37 | $(| $addi)+ => { |
38 | $step = 0; |
39 | $bq |
40 | },)? |
41 | _ => $step = 0, |
42 | } |
43 | } |
44 | 3 => { |
45 | match $e { |
46 | b't' | b'T' => $step = 4, |
47 | $(b' \\' => $step = 100, |
48 | $(| $addi)+ => { |
49 | $step = 0; |
50 | $bq |
51 | },)? |
52 | _ => $step = 0, |
53 | } |
54 | } |
55 | 4 => { |
56 | match $e { |
57 | b'y' | b'Y' => $step = 5, |
58 | $(b' \\' => $step = 100, |
59 | $(| $addi)+ => { |
60 | $step = 0; |
61 | $bq |
62 | },)? |
63 | _ => $step = 0, |
64 | } |
65 | } |
66 | 5 => { |
67 | match $e { |
68 | b'l' | b'L' => $step = 6, |
69 | $(b' \\' => $step = 100, |
70 | $(| $addi)+ => { |
71 | $step = 0; |
72 | $bq |
73 | },)? |
74 | _ => $step = 0, |
75 | } |
76 | } |
77 | 6 => { |
78 | match $e { |
79 | b'e' | b'E' => $step = 7, |
80 | $(b' \\' => $step = 100, |
81 | $(| $addi)+ => { |
82 | $step = 0; |
83 | $bq |
84 | },)? |
85 | _ => $step = 0, |
86 | } |
87 | } |
88 | 7 => { |
89 | match $e { |
90 | b'>' | 9..=13 | 28..=32 => { |
91 | $step = 0; |
92 | $b |
93 | }, |
94 | $(b' \\' => $step = 100, |
95 | $(| $addi)+ => { |
96 | $step = 0; |
97 | $bq |
98 | },)? |
99 | _ => $step = 0, |
100 | } |
101 | } |
102 | 10 => { |
103 | match $e { |
104 | b'-' => $step = 11, |
105 | $(b' \\' => $step = 100, |
106 | $(| $addi)+ => { |
107 | $step = 0; |
108 | $bq |
109 | },)? |
110 | _ => $step = 0, |
111 | } |
112 | } |
113 | 11 => { |
114 | match $e { |
115 | b'-' => { |
116 | $step = 0; |
117 | $bc |
118 | }, |
119 | $(b' \\' => $step = 100, |
120 | $(| $addi)+ => { |
121 | $step = 0; |
122 | $bq |
123 | },)? |
124 | _ => $step = 0, |
125 | } |
126 | } |
127 | 100 => { |
128 | match $e { |
129 | b'<' => $step = 1, |
130 | _ => $step = 0, |
131 | } |
132 | } |
133 | _ => unreachable!(), |
134 | } |
135 | }; |
136 | } |
137 | |
138 | macro_rules! parse_style_comment_single_quoted_text { |
139 | ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => { |
140 | parse_style_comment!($e, $step, $b, $bq, $bc, b' \'' ); |
141 | }; |
142 | } |
143 | |
144 | macro_rules! parse_style_comment_double_quoted_text { |
145 | ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => { |
146 | parse_style_comment!($e, $step, $b, $bq, $bc, b'"' ); |
147 | }; |
148 | } |
149 | |
150 | macro_rules! parse_style_comment_quoted_text { |
151 | ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => { |
152 | parse_style_comment!($e, $step, $b, $bq, $bc, b' \'' , b'"' ); |
153 | }; |
154 | } |
155 | |
156 | encode_impl! { |
157 | 6; |
158 | /// The following substring is escaped: |
159 | /// |
160 | /// * `</style>` => `<\/style>` |
161 | /// * `<!--` => `<\!--` |
162 | parse_style_comment; |
163 | /// Encode text used in the `<style>` element. |
164 | encode_style; |
165 | /// Write text used in the `<style>` element to a mutable `String` reference and return the encoded string slice. |
166 | encode_style_to_string; |
167 | /// Write text used in the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice. |
168 | encode_style_to_vec; |
169 | /// Write text used in the `<style>` element to a writer. |
170 | encode_style_to_writer; |
171 | } |
172 | |
173 | encode_impl! { |
174 | 6; |
175 | /// The following substring and character are escaped: |
176 | /// |
177 | /// * `</style>` => `<\/style>` |
178 | /// * `'` => `\'` |
179 | /// * `<!--` => `<\!--` |
180 | parse_style_comment_single_quoted_text; |
181 | /// Encode text used in a single quoted text in the `<style>` element. |
182 | encode_style_single_quoted_text; |
183 | /// Write text used in a single quoted text in the `<style>` element to a mutable `String` reference and return the encoded string slice. |
184 | encode_style_single_quoted_text_to_string; |
185 | /// Write text used in a single quoted text in the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice. |
186 | encode_style_single_quoted_text_to_vec; |
187 | /// Write text used in a single quoted text in the `<style>` element to a writer. |
188 | encode_style_single_quoted_text_to_writer; |
189 | } |
190 | |
191 | encode_impl! { |
192 | 6; |
193 | /// The following substring and character are escaped: |
194 | /// |
195 | /// * `</style>` => `<\/style>` |
196 | /// * `"` => `\"` |
197 | /// * `<!--` => `<\!--` |
198 | parse_style_comment_double_quoted_text; |
199 | /// Encode text used in a double quoted text in the `<style>` element. |
200 | encode_style_double_quoted_text; |
201 | /// Write text used in a double quoted text in the `<style>` element to a mutable `String` reference and return the encoded string slice. |
202 | encode_style_double_quoted_text_to_string; |
203 | /// Write text used in a double quoted text in the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice. |
204 | encode_style_double_quoted_text_to_vec; |
205 | /// Write text used in a double quoted text in the `<style>` element to a writer. |
206 | encode_style_double_quoted_text_to_writer; |
207 | } |
208 | |
209 | encode_impl! { |
210 | 6; |
211 | /// The following substring and characters are escaped: |
212 | /// |
213 | /// * `</style>` => `<\/style>` |
214 | /// * `"` => `\"` |
215 | /// * `'` => `\'` |
216 | /// * `<!--` => `<\!--` |
217 | parse_style_comment_quoted_text; |
218 | /// Encode text used in a quoted text in the `<style>` element. |
219 | encode_style_quoted_text; |
220 | /// Write text used in a quoted text in the `<style>` element to a mutable `String` reference and return the encoded string slice. |
221 | encode_style_quoted_text_to_string; |
222 | /// Write text used in a quoted text in the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice. |
223 | encode_style_quoted_text_to_vec; |
224 | /// Write text used in a quoted text in the `<style>` element to a writer. |
225 | encode_style_quoted_text_to_writer; |
226 | } |
227 | |