@@ -221,7 +221,7 @@ where
221221 if let Some ( entity) = pat. strip_prefix ( '#' ) {
222222 let codepoint = parse_number ( entity, start..end) ?;
223223 unescaped. push_str ( codepoint. encode_utf8 ( & mut [ 0u8 ; 4 ] ) ) ;
224- } else if let Some ( value) = named_entity ( pat) {
224+ } else if let Some ( value) = resolve_predefined_entity ( pat) {
225225 unescaped. push_str ( value) ;
226226 } else if let Some ( value) = resolve_entity ( pat) {
227227 unescaped. push_str ( value) ;
@@ -248,10 +248,45 @@ where
248248 }
249249}
250250
251- #[ cfg( not( feature = "escape-html" ) ) ]
252- fn named_entity ( name : & str ) -> Option < & str > {
251+ /// Resolves predefined XML entities or all HTML5 entities depending on the feature
252+ /// [`escape-html`](https://docs.rs/quick-xml/latest/quick_xml/#escape-html).
253+ ///
254+ /// Behaves like [`resolve_xml_entity`] if feature is not enabled and as
255+ /// [`resolve_html5_entity`] if enabled.
256+ #[ inline]
257+ pub fn resolve_predefined_entity ( entity : & str ) -> Option < & ' static str > {
258+ #[ cfg( not( feature = "escape-html" ) ) ]
259+ {
260+ resolve_xml_entity ( entity)
261+ }
262+
263+ #[ cfg( feature = "escape-html" ) ]
264+ {
265+ resolve_html5_entity ( entity)
266+ }
267+ }
268+
269+ /// Resolves predefined XML entities. If specified entity is not a predefined XML
270+ /// entity, `None` is returned.
271+ ///
272+ /// The complete list of predefined entities are defined in the [specification].
273+ ///
274+ /// ```
275+ /// # use quick_xml::escape::resolve_xml_entity;
276+ /// # use pretty_assertions::assert_eq;
277+ /// assert_eq!(resolve_xml_entity("lt"), Some("<"));
278+ /// assert_eq!(resolve_xml_entity("gt"), Some(">"));
279+ /// assert_eq!(resolve_xml_entity("amp"), Some("&"));
280+ /// assert_eq!(resolve_xml_entity("apos"), Some("'"));
281+ /// assert_eq!(resolve_xml_entity("quot"), Some("\""));
282+ ///
283+ /// assert_eq!(resolve_xml_entity("foo"), None);
284+ /// ```
285+ ///
286+ /// [specification]: https://www.w3.org/TR/xml11/#sec-predefined-ent
287+ pub fn resolve_xml_entity ( entity : & str ) -> Option < & ' static str > {
253288 // match over strings are not allowed in const functions
254- let s = match name . as_bytes ( ) {
289+ let s = match entity . as_bytes ( ) {
255290 b"lt" => "<" ,
256291 b"gt" => ">" ,
257292 b"amp" => "&" ,
@@ -261,12 +296,13 @@ fn named_entity(name: &str) -> Option<&str> {
261296 } ;
262297 Some ( s)
263298}
264- #[ cfg( feature = "escape-html" ) ]
265- fn named_entity ( name : & str ) -> Option < & str > {
299+
300+ /// Resolves all HTML5 entities. For complete list see <https://dev.w3.org/html5/html-author/charref>.
301+ pub fn resolve_html5_entity ( entity : & str ) -> Option < & ' static str > {
266302 // imported from https://dev.w3.org/html5/html-author/charref
267303 // match over strings are not allowed in const functions
268304 //TODO: automate up-to-dating using https://html.spec.whatwg.org/entities.json
269- let s = match name . as_bytes ( ) {
305+ let s = match entity . as_bytes ( ) {
270306 b"Tab" => "\u{09} " ,
271307 b"NewLine" => "\u{0A} " ,
272308 b"excl" => "\u{21} " ,
0 commit comments