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