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