streamline the state caching for IE mousedown a bit; also remember the text area...
[pagedown] / Markdown.Editor.js
index ba2e1b7..19d6914 100644 (file)
     //\r
     // If remove is true, the whitespace disappears.\r
     Chunks.prototype.trimWhitespace = function (remove) {\r
-\r
-        this.selection = this.selection.replace(/^(\s*)/, "");\r
-\r
-        if (!remove) {\r
-            this.before += re.$1;\r
-        }\r
-\r
-        this.selection = this.selection.replace(/(\s*)$/, "");\r
-\r
-        if (!remove) {\r
-            this.after = re.$1 + this.after;\r
+        var beforeReplacer, afterReplacer, that = this;\r
+        if (remove) {\r
+            beforeReplacer = afterReplacer = "";\r
+        } else {\r
+            beforeReplacer = function (s) { that.before += s; return ""; }\r
+            afterReplacer = function (s) { that.after = s + that.after; return ""; }\r
         }\r
+        \r
+        this.selection = this.selection.replace(/^(\s*)/, beforeReplacer).replace(/(\s*)$/, afterReplacer);\r
     };\r
 \r
 \r
 \r
     // A collection of the important regions on the page.\r
     // Cached so we don't have to keep traversing the DOM.\r
-    // Also holds ieRetardedClick and ieCachedRange, where necessary; working around\r
+    // Also holds ieCachedRange and ieCachedScrollTop, where necessary; working around\r
     // this issue:\r
     // Internet explorer has problems with CSS sprite buttons that use HTML\r
     // lists.  When you click on the background image "button", IE will \r
 \r
         this.setInputAreaSelectionStartEnd = function () {\r
 \r
-            if (!panels.ieRetardedClick && (inputArea.selectionStart || inputArea.selectionStart === 0)) {\r
+            if (!panels.ieCachedRange && (inputArea.selectionStart || inputArea.selectionStart === 0)) {\r
 \r
                 stateObj.start = inputArea.selectionStart;\r
                 stateObj.end = inputArea.selectionEnd;\r
                 stateObj.text = util.fixEolChars(inputArea.value);\r
 \r
                 // IE loses the selection in the textarea when buttons are\r
-                // clicked.  On IE we cache the selection and set a flag\r
-                // which we check for here.\r
-                var range;\r
-                if (panels.ieRetardedClick && panels.ieCachedRange) {\r
-                    range = panels.ieCachedRange;\r
-                    panels.ieRetardedClick = false;\r
-                }\r
-                else {\r
-                    range = doc.selection.createRange();\r
-                }\r
+                // clicked.  On IE we cache the selection. Here, if something is cached,\r
+                // we take it.\r
+                var range = panels.ieCachedRange || doc.selection.createRange();\r
 \r
                 var fixedRange = util.fixEolChars(range.text);\r
                 var marker = "\x07";\r
                     range.text = fixedRange;\r
                 }\r
 \r
+                if (panels.ieCachedRange)\r
+                    stateObj.scrollTop = panels.ieCachedScrollTop; // this is set alongside with ieCachedRange\r
+                \r
+                panels.ieCachedRange = null;\r
+\r
                 this.setInputAreaSelection();\r
             }\r
         };\r
                         if (doc.activeElement && doc.activeElement !== panels.input) { // we're not even in the input box, so there's no selection\r
                             return;\r
                         }\r
-                        panels.ieRetardedClick = true;\r
                         panels.ieCachedRange = document.selection.createRange();\r
+                        panels.ieCachedScrollTop = panels.input.scrollTop;\r
                     };\r
                 }\r
 \r
         chunk.selection = chunk.selection.replace(/\n{2,}/g, "\n");\r
 \r
         // Look for stars before and after.  Is the chunk already marked up?\r
-        chunk.before.search(/(\**$)/);\r
-        var starsBefore = re.$1;\r
-\r
-        chunk.after.search(/(^\**)/);\r
-        var starsAfter = re.$1;\r
+        // note that these regex matches cannot fail\r
+        var starsBefore = /(\**$)/.exec(chunk.before)[0];\r
+        var starsAfter = /(^\**)/.exec(chunk.after)[0];\r
 \r
         var prevStars = Math.min(starsBefore.length, starsAfter.length);\r
 \r
         if (chunk.before) {\r
             var lines = chunk.before.replace(/\n$/, "").split("\n");\r
             var inChain = false;\r
-            for (var i = 0; i < lines.length; i++ ) {\r
+            for (var i = 0; i < lines.length; i++) {\r
                 var good = false;\r
                 line = lines[i];\r
                 inChain = inChain && line.length > 0; // c) any non-empty line continues the chain\r