1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
use core::str::from_utf8_unchecked;
use alloc::borrow::Cow;
use alloc::string::String;
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::io::{self, Write};
macro_rules! parse_style_comment {
($e:expr, $step:ident, $b:block, $bq:block, $bc:block $(, $($addi:expr),+)?) => {
match $step {
0 => {
match $e {
b'<' => $step = 1,
$(b'\\' => $step = 100,
$(| $addi)+ => $bq,)?
_ => (),
}
}
1 => {
match $e {
b'/' => $step = 2,
b'!' => $step = 10,
$(b'\\' => $step = 100,
$(| $addi)+ => {
$step = 0;
$bq
},)?
_ => $step = 0,
}
}
2 => {
match $e {
b's' | b'S' => $step = 3,
$(b'\\' => $step = 100,
$(| $addi)+ => {
$step = 0;
$bq
},)?
_ => $step = 0,
}
}
3 => {
match $e {
b't' | b'T' => $step = 4,
$(b'\\' => $step = 100,
$(| $addi)+ => {
$step = 0;
$bq
},)?
_ => $step = 0,
}
}
4 => {
match $e {
b'y' | b'Y' => $step = 5,
$(b'\\' => $step = 100,
$(| $addi)+ => {
$step = 0;
$bq
},)?
_ => $step = 0,
}
}
5 => {
match $e {
b'l' | b'L' => $step = 6,
$(b'\\' => $step = 100,
$(| $addi)+ => {
$step = 0;
$bq
},)?
_ => $step = 0,
}
}
6 => {
match $e {
b'e' | b'E' => $step = 7,
$(b'\\' => $step = 100,
$(| $addi)+ => {
$step = 0;
$bq
},)?
_ => $step = 0,
}
}
7 => {
match $e {
b'>' | 9..=13 | 28..=32 => {
$step = 0;
$b
},
$(b'\\' => $step = 100,
$(| $addi)+ => {
$step = 0;
$bq
},)?
_ => $step = 0,
}
}
10 => {
match $e {
b'-' => $step = 11,
$(b'\\' => $step = 100,
$(| $addi)+ => {
$step = 0;
$bq
},)?
_ => $step = 0,
}
}
11 => {
match $e {
b'-' => {
$step = 0;
$bc
},
$(b'\\' => $step = 100,
$(| $addi)+ => {
$step = 0;
$bq
},)?
_ => $step = 0,
}
}
100 => {
match $e {
b'<' => $step = 1,
_ => $step = 0,
}
}
_ => unreachable!(),
}
};
}
macro_rules! parse_style_comment_single_quoted_text {
($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
parse_style_comment!($e, $step, $b, $bq, $bc, b'\'');
};
}
macro_rules! parse_style_comment_double_quoted_text {
($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
parse_style_comment!($e, $step, $b, $bq, $bc, b'"');
};
}
macro_rules! parse_style_comment_quoted_text {
($e:expr, $step:ident, $b:block, $bq:block, $bc:block) => {
parse_style_comment!($e, $step, $b, $bq, $bc, b'\'', b'"');
};
}
encode_impl! {
6;
/// The following substring is escaped:
///
/// * `</style>` => `<\/style>`
/// * `<!--` => `<\!--`
parse_style_comment;
/// Encode text used in the `<style>` element.
encode_style;
/// Write text used in the `<style>` element to a mutable `String` reference and return the encoded string slice.
encode_style_to_string;
/// Write text used in the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
encode_style_to_vec;
/// Write text used in the `<style>` element to a writer.
encode_style_to_writer;
}
encode_impl! {
6;
/// The following substring and character are escaped:
///
/// * `</style>` => `<\/style>`
/// * `'` => `\'`
/// * `<!--` => `<\!--`
parse_style_comment_single_quoted_text;
/// Encode text used in a single quoted text in the `<style>` element.
encode_style_single_quoted_text;
/// Write text used in a single quoted text in the `<style>` element to a mutable `String` reference and return the encoded string slice.
encode_style_single_quoted_text_to_string;
/// Write text used in a single quoted text in the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
encode_style_single_quoted_text_to_vec;
/// Write text used in a single quoted text in the `<style>` element to a writer.
encode_style_single_quoted_text_to_writer;
}
encode_impl! {
6;
/// The following substring and character are escaped:
///
/// * `</style>` => `<\/style>`
/// * `"` => `\"`
/// * `<!--` => `<\!--`
parse_style_comment_double_quoted_text;
/// Encode text used in a double quoted text in the `<style>` element.
encode_style_double_quoted_text;
/// Write text used in a double quoted text in the `<style>` element to a mutable `String` reference and return the encoded string slice.
encode_style_double_quoted_text_to_string;
/// Write text used in a double quoted text in the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
encode_style_double_quoted_text_to_vec;
/// Write text used in a double quoted text in the `<style>` element to a writer.
encode_style_double_quoted_text_to_writer;
}
encode_impl! {
6;
/// The following substring and characters are escaped:
///
/// * `</style>` => `<\/style>`
/// * `"` => `\"`
/// * `'` => `\'`
/// * `<!--` => `<\!--`
parse_style_comment_quoted_text;
/// Encode text used in a quoted text in the `<style>` element.
encode_style_quoted_text;
/// Write text used in a quoted text in the `<style>` element to a mutable `String` reference and return the encoded string slice.
encode_style_quoted_text_to_string;
/// Write text used in a quoted text in the `<style>` element to a mutable `Vec<u8>` reference and return the encoded data slice.
encode_style_quoted_text_to_vec;
/// Write text used in a quoted text in the `<style>` element to a writer.
encode_style_quoted_text_to_writer;
}