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
227
228
229
230
231
232
233
234
235
236
237
//! # Functions to unescape HTML into raw text
//!
//! ```rust
//! use htmlize::{unescape, unescape_in, Context};
//! # use assert2::check as assert;
//!
//! assert!(unescape("1×2<3") == "1×2<3");
//! assert!(unescape_in("1×2<3", Context::Attribute) == "1×2<3");
//! assert!(unescape_in("3 × 5 < 16", Context::Attribute) == "3 × 5 < 16");
//! ```
//!
//! See the normative reference for HTML5 entities:
//! <https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references>
//!
//! Entities do not always require a trailing semicolon, though the exact rules
//! depend on whether the entity appears in an attribute value or somewhere else.
//! See [`unescape_in()`] for more information.
//!
//! Some entities are prefixes for multiple other entities. For example:
//! × × ⊠ ⨱ ⨰
use Cow;
/// The context for an input string.
///
/// See [`unescape_in()`] for usage.
/// Expand all valid entities.
///
/// ```rust
/// assert!(htmlize::unescape("1×2<3") == "1×2<3");
/// ```
///
/// This is appropriate to use on any text outside of an attribute. See
/// [`unescape_in()`] for more information.
///
/// To work with bytes (`[u8]`) instead of strings, see [`unescape_bytes_in()`].
/// Expand all valid entities in an attribute.
///
/// ```rust
/// use htmlize::unescape_attribute;
/// # use assert2::check as assert;
///
/// assert!(unescape_attribute("1×2<3") == "1×2<3");
/// assert!(unescape_attribute("1 × 2 < 3") == "1 × 2 < 3");
/// ```
///
/// This is only appropriate for the value of an attribute. See
/// [`unescape_in()`] for more information.
///
/// To work with bytes (`[u8]`) instead of strings, see [`unescape_bytes_in()`].
/// Expand all valid entities in a given context.
///
/// `context` may be:
///
/// * `Context::General`: use the rules for text outside of an attribute.
/// This is usually what you want.
/// * `Context::Attribute`: use the rules for attribute values.
///
/// This uses the [algorithm described] in the WHATWG spec. In attributes,
/// [named entities] without trailing semicolons are not expanded when followed
/// by an alphanumeric character or `=`.
///
/// For example:
///
/// ```rust
/// use htmlize::{unescape_in, Context};
/// # use assert2::check as assert;
///
/// assert!(unescape_in("×", Context::General) == "×");
/// assert!(unescape_in("×", Context::Attribute) == "×");
/// assert!(unescape_in("×X", Context::General) == "×X");
/// assert!(unescape_in("×X", Context::Attribute) == "×X");
/// assert!(unescape_in("×X", Context::General) == "×X");
/// assert!(unescape_in("×X", Context::Attribute) == "×X");
/// assert!(unescape_in("×=", Context::General) == "×=");
/// assert!(unescape_in("×=", Context::Attribute) == "×=");
/// assert!(unescape_in("×#", Context::General) == "×#");
/// assert!(unescape_in("×#", Context::Attribute) == "×#");
/// ```
///
/// To work with bytes (`[u8]`) instead of strings, see [`unescape_bytes_in()`].
///
/// [algorithm described]: https://html.spec.whatwg.org/multipage/parsing.html#character-reference-state
/// [named entities]: https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state
/// Expand all valid entities in a given context.
///
/// `context` may be:
///
/// * `Context::General`: use the rules for text outside of an attribute.
/// This is usually what you want.
/// * `Context::Attribute`: use the rules for attribute values.
///
/// This uses the [algorithm described] in the WHATWG spec. In attributes,
/// [named entities] without trailing semicolons are treated differently. They
/// not expanded if they are followed by an alphanumeric character or or `=`.
///
/// For example:
///
/// ```rust
/// use htmlize::*;
/// # use assert2::check as assert;
///
/// assert!(unescape_bytes_in(&b"×"[..], Context::General) == "×".as_bytes());
/// assert!(unescape_bytes_in(&b"×"[..], Context::Attribute) == "×".as_bytes());
/// assert!(unescape_bytes_in(&b"×X"[..], Context::General) == "×X".as_bytes());
/// assert!(unescape_bytes_in(&b"×X"[..], Context::Attribute) == "×X".as_bytes());
/// assert!(unescape_bytes_in(&b"×X"[..], Context::General) == "×X".as_bytes());
/// assert!(unescape_bytes_in(&b"×X"[..], Context::Attribute) == "×X".as_bytes());
/// assert!(unescape_bytes_in(&b"×="[..], Context::General) == "×=".as_bytes());
/// assert!(unescape_bytes_in(&b"×="[..], Context::Attribute) == "×=".as_bytes());
/// assert!(unescape_bytes_in(&b"×#"[..], Context::General) == "×#".as_bytes());
/// assert!(unescape_bytes_in(&b"×#"[..], Context::Attribute) == "×#".as_bytes());
/// ```
///
/// To work with `String` instead of bytes, see [`unescape_in()`].
///
/// [algorithm described]: https://html.spec.whatwg.org/multipage/parsing.html#character-reference-state
/// [named entities]: https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state
// Need these to be public for benchmarks
pub use REPLACEMENT_CHAR_BYTES;