basecamp / basecamp/trix

Add documentation to help debugging of custom attachments not working when editing content?

Open
#1,229 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
20k
Forks
1.1k
Avg merge
3d 12h
Merged PRs (30d)
13

Description

Like many people I've been following guidance to support customising Trix to embed YouTube videos.

I found that while my embeds were being saved and displayed, on editing text the attachments were being stripped out. Other people have reported problems with HTML sanitisation as being the cause of the issue, e.g. with embedded iframes being removed.

However my issue was slightly different. My Rails model which produces the `sgid` for the embed implements `to_trix_content_attachment_partial_path` to provide a different in-editor view of the video. This does not use an iframe and instead just inserts an img with a link to video thumbnail. Images aren't sanitised by default.

It turned out that the issue was that we had `annotate_rendered_view_with_filenames` enabled in Rails in our development config. This causes Rails to insert an HTML comment containing the path to the partial that produced each block of HTML. These comments were then included in the `data-trix-attachment` attribute as part of the content to be rendered in the editor.

DOMPurify forcibly [removes all attributes that contain HTML comments](https://github.com/cure53/DOMPurify/blob/2e5fd642ba473242220b98581dfe19049c3ca6de/src/purify.ts#L1320). This isn't configurable behaviour.

This means that any `data-trix-attachment` attribute containing a comment will be removed so Trix doesn't see the attachment as being present. This problem isn't limited to the `annotate_rendered_view_with_filenames` setting as you might just have an HTML comment in your partial.

Am aware this isn't a Trix bug, but thought I'd file this here as it seems like a number of people are discussing how to get custom attachments to work correctly. And this issue hasn't been noted.

It might be useful to highlight potential issues with `annotate_rendered_view_with_filenames` or HTML comments in attachment content in the documentation?

Happy to take suggestions for alternative places to report/suggest this.

Contributor guide

No contributing guide indexed for this repository

Research direction

Use the issue's reproduction as the starting point: custom Trix attachments are stripped when HTML comments enter data-trix-attachment, particularly with annotate_rendered_view_with_filenames enabled. Locate the repository's attachment or customization documentation, then document this setting and the HTML-comment sanitization behavior so users can diagnose the problem. No file or test is named in the payload.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.