medium-editor icon indicating copy to clipboard operation
medium-editor copied to clipboard

HTML class attribute is stripped with custom button

Open chrisburton opened this issue 6 years ago • 2 comments

The way my custom button works is:

  1. Highlight text
  2. Click custom button in toolbar
  3. prompt modal appears
  4. Enter classname (e.g. typing foo into prompt should result in <code class="foo">...</code>

However, the entire class attribute is stripped and I'm not sure why. cleanPastedHtml is set to false.

Code

var CodeButton = MediumEditor.extensions.button.extend({
    // Should match what you add to the Statamic object below, and the button name.
    name: 'codeButton',

    tagNames: ['code'],
    contentDefault: '<span class="icon icon-code"></span>',
    aria: 'Code',

    handleClick: function () {
        const myClass = this.getMyClass();
        if (!myClass) return;

        const selection = this.base.options.contentWindow.getSelection().toString().trim();
        const html = `<code class="${myClass}">${selection}</code>`;
        this.base.pasteHTML(html);
        this.base.checkContentChanged();
    },

    getMyClass: function () {
        // Get the classname from a modal, etc.
        return prompt('Code type (php, file, js, terminal)?');
    }
});
// Add it to the Statamic object.
Statamic.MediumEditorExtensions.codeButton = CodeButton;

Result:

<code>Text I selected</code>

Desired Result:

<code class="foo">Text I selected</code>

chrisburton avatar Jun 22 '18 01:06 chrisburton

Did you resolve this issue? I have the exact same problem!

Vingtoft avatar Feb 12 '21 10:02 Vingtoft

@Vingtoft This medium editor was part of a CMS I was using at the time. The CMS authors ended up going with a better alternative for their WYSIWYG editor. I was unable to solve the problem. Sorry!

chrisburton avatar Feb 13 '21 04:02 chrisburton