Skip to content

Allow customization of default escaper within an escaper group #32

@0b10011

Description

@0b10011

While the defaults will work for most people, some may want raw (or another variant) to be the default. This is currently possible by creating a new escaper group that calls the existing public functions, but it's pretty verbose, although it only has to be done once per project:

use oxiplate::escapers::html::{escape_text, escape_attribute_quoted_value, escape_comment_text};

pub enum CustomHtmlEscaper {
    /// Escaper for [text](https://html.spec.whatwg.org/#text-content) in an HTML document.
    /// See [`escape_text()`] for details.
    Text,

    /// Escaper for single- and double-quoted [attribute values](https://html.spec.whatwg.org/#syntax-attribute-value) in an HTML document.
    /// See [`escape_attribute_quoted_value()`] for details.
    Attr,

    /// Escaper for [comment text](https://html.spec.whatwg.org/#comments) in an HTML document.
    /// See [`escape_comment_text()`] for details.
    Comment,

    /// Do not escape anything using this.
    Raw,
}

impl super::Escaper for CustomHtmlEscaper {
    const DEFAULT: Self = Self::Raw;

    fn escape<'a>(&self, value: &'a str) -> Cow<'a, str> {
        match self {
            Self::Text => escape_text(value),
            Self::Attr => escape_attribute_quoted_value(value),
            Self::Comment => escape_comment_text(value),
            Self::Raw => value,
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions