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