Skip to main content

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