Skip to content

Conversation

Betree
Copy link

@Betree Betree commented Sep 2, 2024

Resolve #1178 by making the sanitizer options configurable.

Comment on lines -16 to -21
static sanitize(html, options) {
const sanitizer = new this(html, options)
sanitizer.sanitize()
return sanitizer
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this function since it wasn't used, and to prevent any confusion with the non-static sanitize method

@OtherCroissant
Copy link

This looks great!

@Betree
Copy link
Author

Betree commented Jan 23, 2025

@jorgemanrubia, do you have any feedback on the approach implemented here? Some of us are still struggling with custom tags and attributes (e.g., iframe for those allowing the embedding of YouTube videos); see #1178 for more details.

#1208 was a first step in the right direction, but it was apparently not enough to support the use case mentioned above.

I'd gladly rebase & adjust the changes here according to your feedback!

@oktaal
Copy link

oktaal commented Aug 20, 2025

I've made an updated fork with your modifications and the current version of main: https://github.com/oktaal/trix/tree/fix/sanitizer-config

I've ran into this issue where I needed to add a custom attribute (in my case data-keyword) to a tags. This would only work with your @Betree's fix and adding it to the textAttributes in the config. For example:

document.addEventListener("trix-before-initialize", () => {
    Trix.config.textAttributes['data-keyword'] = {
        groupTagName: "a",
        parser(element) {
            const matchingSelector = `a[data-keyword]`
            const keyword = element.closest(matchingSelector)
            if (keyword) {
                return keyword.getAttribute("data-keyword")
            }
        }
    };
    Trix.config.parser.allowedAttributes = "style href src width height language class data-keyword".split(" ")
});

Ideally just having the attribute in the allowedAttributes (or having it in textAttributes) should be enough to get it to work, but at least this improves on not working at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trix no longer adds dataset attributes on inserted attachments in 2.1.4

3 participants