Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firefox: Selecting all and typing a new text removes <p> tags #207

Open
shardator opened this issue Sep 19, 2017 · 1 comment
Open

Firefox: Selecting all and typing a new text removes <p> tags #207

shardator opened this issue Sep 19, 2017 · 1 comment

Comments

@shardator
Copy link

shardator commented Sep 19, 2017

When I select all in a medium.js rich editor div, and type something (even in the demo http://jakiestfu.github.io/Medium.js/docs/) the div gets all <p> tags removed, and raw text is placed in the div. After that it is only a simple plain text editor without paragraphs and any formatting, until I clear it, unfocus it and go back.

The problem is non-existent in Chrome.

    new Medium({
        element:     document.getElementById('task-edit-1'),
        mode:        Medium.richMode,
        autoHR:      false,
        placeholder: 'What to do next...'
    });
@shardator
Copy link
Author

shardator commented Sep 19, 2017

This fixed it for me for now (external fix):

    document.getElementById('task-edit-1').oninput = function() {
        // medium.js fix #1: Full selection override removes the <p> elements
        for (var i = 0; i < this.childNodes.length; i++) {
            var node = this.childNodes[i];
            if (node.nodeType == Node.TEXT_NODE) {
                var text  = node.textContent;
                if (text.trim().length) {
                    var el    = document.createElement("p");
                    var range = document.createRange();
                    var sel   = window.getSelection();

                    var selStart = sel.getRangeAt(0).startOffset;

                    var textNode = document.createTextNode(text);
                    el.appendChild(textNode);
                    this.insertBefore(el, node);
                    this.removeChild(node);
                    
                    range.setStart(textNode, 1);
                    range.collapse(true);
                    sel.removeAllRanges();
                    sel.addRange(range);
                } else {
                    this.removeChild(node);                            
                }
            }
        }
    };

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

No branches or pull requests

1 participant