Skip to main content

html_escape/encode/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_comment {
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                    $(| $addi)+ => $bq,)?
14                    _ => (),
15                }
16            }
17            1 => {
18                match $e {
19                    b'/' => $step = 2,
20                    b'!' => $step = 10,
21                    b'<' => $step = 1,
22                    $(b'\\' => $step = 100,
23                    $(| $addi)+ => {
24                        $step = 0;
25                        $bq
26                    },)?
27                    _ => $step = 0,
28                }
29            }
30            2 => {
31                match $e {
32                    b's' | b'S' => $step = 3,
33                    b'<' => $step = 1,
34                    $(b'\\' => $step = 100,
35                    $(| $addi)+ => {
36                        $step = 0;
37                        $bq
38                    },)?
39                    _ => $step = 0,
40                }
41            }
42            3 => {
43                match $e {
44                    b'c' | b'C' => $step = 4,
45                    b'<' => $step = 1,
46                    $(b'\\' => $step = 100,
47                    $(| $addi)+ => {
48                        $step = 0;
49                        $bq
50                    },)?
51                    _ => $step = 0,
52                }
53            }
54            4 => {
55                match $e {
56                    b'r' | b'R' => $step = 5,
57                    b'<' => $step = 1,
58                    $(b'\\' => $step = 100,
59                    $(| $addi)+ => {
60                        $step = 0;
61                        $bq
62                    },)?
63                    _ => $step = 0,
64                }
65            }
66            5 => {
67                match $e {
68                    b'i' | b'I' => $step = 6,
69                    b'<' => $step = 1,
70                    $(b'\\' => $step = 100,
71                    $(| $addi)+ => {
72                        $step = 0;
73                        $bq
74                    },)?
75                    _ => $step = 0,
76                }
77            }
78            6 => {
79                match $e {
80                    b'p' | b'P' => $step = 7,
81                    b'<' => $step = 1,
82                    $(b'\\' => $step = 100,
83                    $(| $addi)+ => {
84                        $step = 0;
85                        $bq
86                    },)?
87                    _ => $step = 0,
88                }
89            }
90            7 => {
91                match $e {
92                    b't' | b'T' => $step = 8,
93                    b'<' => $step = 1,
94                    $(b'\\' => $step = 100,
95                    $(| $addi)+ => {
96                        $step = 0;
97                        $bq
98                    },)?
99                    _ => $step = 0,
100                }
101            }
102            8 => {
103                match $e {
104                    b'/' | b'>' | 9..=13 | 28..=32 => {
105                        $step = 0;
106                        $b
107                    },
108                    b'<' => $step = 1,
109                    $(b'\\' => $step = 100,
110                    $(| $addi)+ => {
111                        $step = 0;
112                        $bq
113                    },)?
114                    _ => $step = 0,
115                }
116            }
117            10 => {
118                match $e {
119                    b'-' => $step = 11,
120                    b'<' => $step = 1,
121                    $(b'\\' => $step = 100,
122                    $(| $addi)+ => {
123                        $step = 0;
124                        $bq
125                    },)?
126                    _ => $step = 0,
127                }
128            }
129            11 => {
130                match $e {
131                    b'-' => {
132                        $step = 0;
133                        $bc
134                    },
135                    b'<' => $step = 1,
136                    $(b'\\' => $step = 100,
137                    $(| $addi)+ => {
138                        $step = 0;
139                        $bq
140                    },)?
141                    _ => $step = 0,
142                }
143            }
144            100 => {
145                match $e {
146                    b'<' => $step = 1,
147                    _ => $step = 0,
148                }
149            }
150            _ => unreachable!(),
151        }
152    };
153}
154
155macro_rules! parse_script_comment_single_quoted_text {
156    ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
157        parse_script_comment!($e, $step, $b, $bq, $bc, b'\'');
158    };
159}
160
161macro_rules! parse_script_comment_double_quoted_text {
162    ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
163        parse_script_comment!($e, $step, $b, $bq, $bc, b'"');
164    };
165}
166
167macro_rules! parse_script_comment_quoted_text {
168    ($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
169        parse_script_comment!($e, $step, $b, $bq, $bc, b'\'', b'"');
170    };
171}
172
173encode_impl! {
174    7;
175    /// The following substring is escaped:
176    ///
177    /// * `</script>` => `<\/script>`
178    /// * `<!--` => `<\!--`
179    parse_script_comment;
180    /// Encode text used in the `<script>` element.
181    encode_script;
182    /// Write text used in the `<script>` element to a mutable `String` reference and return the encoded string slice.
183    encode_script_to_string;
184    /// Write text used in the `<script>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
185    encode_script_to_vec;
186    /// Write text used in the `<script>` element to a writer.
187    encode_script_to_writer;
188}
189
190encode_impl! {
191    7;
192    /// The following substring and character are escaped:
193    ///
194    /// * `</script>` => `<\/script>`
195    /// * `'` => `\'`
196    /// * `<!--` => `<\!--`
197    parse_script_comment_single_quoted_text;
198    /// Encode text used in a single quoted text in the `<script>` element.
199    encode_script_single_quoted_text;
200    /// Write text used in a single quoted text in the `<script>` element to a mutable `String` reference and return the encoded string slice.
201    encode_script_single_quoted_text_to_string;
202    /// Write text used in a single quoted text in the `<script>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
203    encode_script_single_quoted_text_to_vec;
204    /// Write text used in a single quoted text in the `<script>` element to a writer.
205    encode_script_single_quoted_text_to_writer;
206}
207
208encode_impl! {
209    7;
210    /// The following substring and character are escaped:
211    ///
212    /// * `</script>` => `<\/script>`
213    /// * `"` => `\"`
214    /// * `<!--` => `<\!--`
215    parse_script_comment_double_quoted_text;
216    /// Encode text used in a double quoted text in the `<script>` element.
217    encode_script_double_quoted_text;
218    /// Write text used in a double quoted text in the `<script>` element to a mutable `String` reference and return the encoded string slice.
219    encode_script_double_quoted_text_to_string;
220    /// Write text used in a double quoted text in the `<script>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
221    encode_script_double_quoted_text_to_vec;
222    /// Write text used in a double quoted text in the `<script>` element to a writer.
223    encode_script_double_quoted_text_to_writer;
224}
225
226encode_impl! {
227    7;
228    /// The following substring and characters are escaped:
229    ///
230    /// * `</script>` => `<\/script>`
231    /// * `"` => `\"`
232    /// * `'` => `\'`
233    /// * `<!--` => `<\!--`
234    parse_script_comment_quoted_text;
235    /// Encode text used in a quoted text in the `<script>` element.
236    encode_script_quoted_text;
237    /// Write text used in a quoted text in the `<script>` element to a mutable `String` reference and return the encoded string slice.
238    encode_script_quoted_text_to_string;
239    /// Write text used in a quoted text in the `<script>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
240    encode_script_quoted_text_to_vec;
241    /// Write text used in a quoted text in the `<script>` element to a writer.
242    encode_script_quoted_text_to_writer;
243}