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