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