Skip to main content

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