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