Skip to content

removeFormat button won't remove the strikethrough style #1549

@fanogueira-portoeditora

Description

The button "Remove Format" does not clear the strikethrough tags, and that's because it calls the function document.execCommand('removeFormat'), which doesn't handle the strikethrough format and is deprecated.

A possible fix would be clearing the tags manually using string.replace(), which allows even more control over the content

        execCmd: function (cmd, param, forceCss, skipTrumbowyg) {
            var t = this;
            skipTrumbowyg = !!skipTrumbowyg || '';

            if (cmd !== 'dropdown') {
                t.$ed.focus();
            }

            if (cmd === 'strikethrough' && t.o.semantic) {
                t.semanticTag('strike', t.o.semanticKeepAttributes, true); // browsers cannot undo e.g. <del> as they expect <strike>
            }

            try {
                t.doc.execCommand('styleWithCSS', false, forceCss || false);
            } catch (c) {
            }

            try {
                t[cmd + skipTrumbowyg](param);
            } catch (c) {
                try {
                    cmd(param);
                } catch (e2) {
                    if (cmd === 'insertHorizontalRule') {
                        param = undefined;
                    } else if (cmd === 'formatBlock' && t.isIE) {
                        param = '<' + param + '>';
                    }

                    t.doc.execCommand(cmd, false, param);

                    if (cmd == 'removeformat'){
                        const selection = window.getSelection();
                        if (!selection.rangeCount) return;

                        const range = selection.getRangeAt(0);
                        const contents = range.cloneContents();

                        // Create a temporary container to manipulate the contents
                        const tempDiv = document.createElement("div");
                        tempDiv.appendChild(contents);

                        // Remove <strike>, <s>, and <del> tags
                        const cleanedContent = tempDiv.innerHTML
                        .replace(/<del>|<\/del>|<s>|<\/s>|<strike>|<\/strike>|amp;/g, '')
                        .replace("&nbsp;", " ");

                        // Replace the selected content with cleaned content
                        range.deleteContents();
                        range.insertNode(document.createTextNode(cleanedContent));
                    }

                    t.syncCode();
                    t.semanticCode(false, true);
                }

                if (cmd !== 'dropdown') {
                    t.updateButtonPaneStatus();
                    t.$c.trigger('tbwchange');
                }
            }

            try {
                t.applyTagClasses();
            } catch (e) {
            }
        },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions