html_escape/decode/element/
script.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_script {
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'c' | b'C' => $step = 5,
47                    b'\\' => $step = 100,
48                    _ => $step = 0,
49                }
50            }
51            5 => {
52                match $e {
53                    b'r' | b'R' => $step = 6,
54                    b'\\' => $step = 100,
55                    _ => $step = 0,
56                }
57            }
58            6 => {
59                match $e {
60                    b'i' | b'I' => $step = 7,
61                    b'\\' => $step = 100,
62                    _ => $step = 0,
63                }
64            }
65            7 => {
66                match $e {
67                    b'p' | b'P' => $step = 8,
68                    b'\\' => $step = 100,
69                    _ => $step = 0,
70                }
71            }
72            8 => {
73                match $e {
74                    b't' | b'T' => $step = 9,
75                    b'\\' => $step = 100,
76                    _ => $step = 0,
77                }
78            }
79            9 => {
80                match $e {
81                    b'>' | 9..=13 | 28..=32 => {
82                        $step = 0;
83                        $b
84                    },
85                    b'\\' => $step = 100,
86                    _ => $step = 0,
87                }
88            }
89            10 => {
90                match $e {
91                    b'-' => $step = 11,
92                    b'\\' => $step = 100,
93                    _ => $step = 0,
94                }
95            }
96            11 => {
97                match $e {
98                    b'-' => {
99                        $step = 0;
100                        $bc
101                    },
102                    b'\\' => $step = 100,
103                    _ => $step = 0,
104                }
105            }
106            100 => {
107                match $e {
108                    b'<' => $step = 1,
109                     $($(| $addi)+ => {
110                        $step = 0;
111                        $bq
112                    },)?
113                    _ => $step = 0,
114                }
115            }
116            _ => unreachable!(),
117        }
118    };
119}
120
121macro_rules! parse_script_single_quoted_text {
122    ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
123        parse_script!($e, $step, $b, $bq, $bc, b'\'');
124    };
125}
126
127macro_rules! parse_script_double_quoted_text {
128    ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
129        parse_script!($e, $step, $b, $bq, $bc, b'"');
130    };
131}
132
133macro_rules! parse_script_quoted_text {
134    ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
135        parse_script!($e, $step, $b, $bq, $bc, b'\'', b'"');
136    };
137}
138
139decode_impl! {
140    7;
141    /// The following substring is unescaped:
142    ///
143    /// * `<\/script>` => `</script>`
144    parse_script;
145    /// Decode text from the `<script>` element.
146    decode_script;
147    /// Write text from the `<script>` element to a mutable `String` reference and return the encoded string slice.
148    decode_script_to_string;
149    /// Write text from the `<script>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
150    decode_script_to_vec;
151    /// Write text from the `<script>` element to a writer.
152    decode_script_to_writer;
153}
154
155decode_impl! {
156    7;
157    /// The following substring and character is unescaped:
158    ///
159    /// * `<\/script>` => `</script>`
160    /// * `\'` => `'`
161    parse_script_single_quoted_text;
162    /// Decode text from a single quoted text in the `<script>` element.
163    decode_script_single_quoted_text;
164    /// Write text from a single quoted text in the `<script>` element to a mutable `String` reference and return the encoded string slice.
165    decode_script_single_quoted_text_to_string;
166    /// Write text from a single quoted text in the `<script>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
167    decode_script_single_quoted_text_to_vec;
168    /// Write text from a single quoted text in the `<script>` element to a writer.
169    decode_script_single_quoted_text_to_writer;
170}
171
172decode_impl! {
173    7;
174    /// The following substring and character are unescaped:
175    ///
176    /// * `<\/script>` => `</script>`
177    /// * `\"` => `"`
178    parse_script_double_quoted_text;
179    /// Decode text from a double quoted text in the `<script>` element.
180    decode_script_double_quoted_text;
181    /// Write text from a double quoted text in the `<script>` element to a mutable `String` reference and return the encoded string slice.
182    decode_script_double_quoted_text_to_string;
183    /// Write text from a double quoted text in the `<script>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
184    decode_script_double_quoted_text_to_vec;
185    /// Write text from a double quoted text in the `<script>` element to a writer.
186    decode_script_double_quoted_text_to_writer;
187}
188
189decode_impl! {
190    7;
191    /// The following substring and characters are unescaped:
192    ///
193    /// * `<\/script>` => `</script>`
194    /// * `\"` => `"`
195    /// * `\'` => `'`
196    parse_script_quoted_text;
197    /// Decode text from a quoted text in the `<script>` element.
198    decode_script_quoted_text;
199    /// Write text from a quoted text in the `<script>` element to a mutable `String` reference and return the encoded string slice.
200    decode_script_quoted_text_to_string;
201    /// Write text from a quoted text in the `<script>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
202    decode_script_quoted_text_to_vec;
203    /// Write text from a quoted text in the `<script>` element to a writer.
204    decode_script_quoted_text_to_writer;
205}