html_escape/decode/element/
style.rs

1use core::str::from_utf8_unchecked;
2
3use alloc::borrow::Cow;
4use alloc::string::String;
5use alloc::vec::Vec;
6
7#[cfg(feature = "std")]
8use std::io::{self, Write};
9
10macro_rules! parse_style {
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                    _ => (),
18                }
19            }
20            1 => {
21                match $e {
22                    b'\\' => $step = 2,
23                    _ => (),
24                }
25            }
26            2 => {
27                match $e {
28                    b'/' => $step = 3,
29                    b'!' => $step = 10,
30                    $($(| $addi)+ => {
31                        $step = 0;
32                        $bq
33                    },)?
34                    _ => $step = 0,
35                }
36            }
37            3 => {
38                match $e {
39                    b's' | b'S' => $step = 4,
40                    b'\\' => $step = 100,
41                    _ => $step = 0,
42                }
43            }
44            4 => {
45                match $e {
46                    b't' | b'T' => $step = 5,
47                    b'\\' => $step = 100,
48                    _ => $step = 0,
49                }
50            }
51            5 => {
52                match $e {
53                    b'y' | b'Y' => $step = 6,
54                    b'\\' => $step = 100,
55                    _ => $step = 0,
56                }
57            }
58            6 => {
59                match $e {
60                    b'l' | b'L' => $step = 7,
61                    b'\\' => $step = 100,
62                    _ => $step = 0,
63                }
64            }
65            7 => {
66                match $e {
67                    b'e' | b'E' => $step = 8,
68                    b'\\' => $step = 100,
69                    _ => $step = 0,
70                }
71            }
72            8 => {
73                match $e {
74                    b'>' | 9..=13 | 28..=32 => {
75                        $step = 0;
76                        $b
77                    },
78                    b'\\' => $step = 100,
79                    _ => $step = 0,
80                }
81            }
82            10 => {
83                match $e {
84                    b'-' => $step = 11,
85                    b'\\' => $step = 100,
86                    _ => $step = 0,
87                }
88            }
89            11 => {
90                match $e {
91                    b'-' => {
92                        $step = 0;
93                        $bc
94                    },
95                    b'\\' => $step = 100,
96                    _ => $step = 0,
97                }
98            }
99            100 => {
100                match $e {
101                    b'<' => $step = 1,
102                     $($(| $addi)+ => {
103                        $step = 0;
104                        $bq
105                    },)?
106                    _ => $step = 0,
107                }
108            }
109            _ => unreachable!(),
110        }
111    };
112}
113
114macro_rules! parse_style_single_quoted_text {
115    ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
116        parse_style!($e, $step, $b, $bq, $bc, b'\'');
117    };
118}
119
120macro_rules! parse_style_double_quoted_text {
121    ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
122        parse_style!($e, $step, $b, $bq, $bc, b'"');
123    };
124}
125
126macro_rules! parse_style_quoted_text {
127    ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
128        parse_style!($e, $step, $b, $bq, $bc, b'\'', b'"');
129    };
130}
131
132decode_impl! {
133    6;
134    /// The following substring is unescaped:
135    ///
136    /// * `<\/style>` => `</style>`
137    parse_style;
138    /// Decode text from the `<style>` element.
139    decode_style;
140    /// Write text from the `<style>` element to a mutable `String` reference and return the encoded string slice.
141    decode_style_to_string;
142    /// Write text from the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
143    decode_style_to_vec;
144    /// Write text from the `<style>` element to a writer.
145    decode_style_to_writer;
146}
147
148decode_impl! {
149    6;
150    /// The following substring and character is unescaped:
151    ///
152    /// * `<\/style>` => `</style>`
153    /// * `\'` => `'`
154    parse_style_single_quoted_text;
155    /// Decode text from a single quoted text in the `<style>` element.
156    decode_style_single_quoted_text;
157    /// Write text from a single quoted text in the `<style>` element to a mutable `String` reference and return the encoded string slice.
158    decode_style_single_quoted_text_to_string;
159    /// Write text from a single quoted text in the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
160    decode_style_single_quoted_text_to_vec;
161    /// Write text from a single quoted text in the `<style>` element to a writer.
162    decode_style_single_quoted_text_to_writer;
163}
164
165decode_impl! {
166    6;
167    /// The following substring and character are unescaped:
168    ///
169    /// * `<\/style>` => `</style>`
170    /// * `\"` => `"`
171    parse_style_double_quoted_text;
172    /// Decode text from a double quoted text in the `<style>` element.
173    decode_style_double_quoted_text;
174    /// Write text from a double quoted text in the `<style>` element to a mutable `String` reference and return the encoded string slice.
175    decode_style_double_quoted_text_to_string;
176    /// Write text from a double quoted text in the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
177    decode_style_double_quoted_text_to_vec;
178    /// Write text from a double quoted text in the `<style>` element to a writer.
179    decode_style_double_quoted_text_to_writer;
180}
181
182decode_impl! {
183    6;
184    /// The following substring and characters are unescaped:
185    ///
186    /// * `<\/style>` => `</style>`
187    /// * `\"` => `"`
188    /// * `\'` => `'`
189    parse_style_quoted_text;
190    /// Decode text from a quoted text in the `<style>` element.
191    decode_style_quoted_text;
192    /// Write text from a quoted text in the `<style>` element to a mutable `String` reference and return the encoded string slice.
193    decode_style_quoted_text_to_string;
194    /// Write text from a quoted text in the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
195    decode_style_quoted_text_to_vec;
196    /// Write text from a quoted text in the `<style>` element to a writer.
197    decode_style_quoted_text_to_writer;
198}