basecamp / basecamp/trix

Problems with trix sanitization

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

Description

I have a little problem using trix... I created a new feature to add a button for align-center, align-right and align-left. The problem I face is sanitization in the trix editor.

`import { Controller } from "@hotwired/stimulus";
import * as AlignmentElements from "../alignment_elements";

export default class TrixController extends Controller {
static targets = ['editor']

connect() {
this.initTrix()
}

initTrix () {
window.Trix = Trix; // Don't need to bind to the window, but useful for debugging.
Trix.config.toolbar.getDefaultHTML = toolbarDefaultHTML;

Trix.config.blockAttributes.alignLeft = {
tagName: 'align-left',
parse: false,
nestable: false,
exclusive: true,
};

Trix.config.blockAttributes.alignCenter = {
tagName: 'align-center',
parse: false,
nestable: false,
exclusive: true,
};

Trix.config.blockAttributes.alignRight = {
tagName: 'align-right',
parse: false,
nestable: false,
exclusive: true,
};

document.addEventListener('trix-initialize', updateToolbars, { once: true });

function updateToolbars(event) {
const toolbars = document.querySelectorAll('trix-toolbar');
const html = Trix.config.toolbar.getDefaultHTML();
toolbars.forEach((toolbar) => (toolbar.innerHTML = html));
}

/**
* This is the default Trix toolbar. Feel free to change / manipulate it how you would like.
* @see https://github.com/basecamp/trix/blob/main/src/trix/config/toolbar.coffee
*/
function toolbarDefaultHTML() {
const { lang } = Trix.config;
return `



Align Left

Align Center

Align Right


${lang.bold}
${lang.italic}
${lang.strike}
${lang.link}


${lang.heading1}
${lang.quote}
${lang.code}
${lang.bullets}
${lang.numbers}
${lang.outdent}
${lang.indent}


${lang.attachFiles}



${lang.undo}
${lang.redo}


`;
}
}

}`

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the sanitization behavior with the custom alignment elements from the issue and inspect the imported alignment_elements module alongside Trix's sanitization entry points. Determine which alignment markup is removed and define done as preserving the intended alignment output without weakening sanitization for other content.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.