Fixed inserting block-level elements into empty <p /> in Safari
authorAlexander Ebert <ebert@woltlab.com>
Sun, 28 Dec 2014 23:31:44 +0000 (00:31 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 28 Dec 2014 23:31:44 +0000 (00:31 +0100)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js

index d5b757fc80acd918ea650369aa308b45c36dff11..4e95f3e53eb221ad6db29bb40c8f68c563e5a28c 100644 (file)
@@ -504,7 +504,7 @@ RedactorPlugins.wmonkeypatch = function() {
                                var $html = this.$editor.html();
                                if (this.utils.isEmpty($html)) {
                                        var $cleared = false;
-                                       if (html.match(/^<(blockquote|div|p)/)) {
+                                       if (html.match(/^<(blockquote|div|p)/i)) {
                                                // inserting a block-level element into a <p /> yields inconsistent behaviors in different browsers
                                                // but since the HTML to be inserted is already a block element, we can place it directly in the root
                                                this.$editor.empty();
@@ -522,6 +522,14 @@ RedactorPlugins.wmonkeypatch = function() {
                                        if (document.activeElement !== this.$editor[0]) {
                                                this.wutil.restoreSelection();
                                        }
+                                       
+                                       if (html.match(/^<(blockquote|div|p)/i) && getSelection().getRangeAt(0).collapsed) {
+                                               var $startContainer = getSelection().getRangeAt(0).startContainer;
+                                               if ($startContainer.nodeType === Node.TEXT_NODE && $startContainer.textContent === '\u200b') {
+                                                       // Safari breaks if inserting block-level elements into a <p /> w/ only a zero-width space
+                                                       this.caret.setEnd($($startContainer.parentElement).html('<br />'));
+                                               }
+                                       }
                                }
                        }).bind(this);