X-Git-Url: http://git.roojs.org/?p=pagedown;a=blobdiff_plain;f=Markdown.Editor.js;h=19d6914fae7b7ac15f91fe53d87fbecad2ac13e4;hp=ba2e1b76682083d7d1d8b7cd6f736a9610d5c581;hb=HEAD;hpb=340f85793552abe1775dc4457e50cd04c63ca590 diff --git a/Markdown.Editor.js b/Markdown.Editor.js index ba2e1b7..19d6914 100644 --- a/Markdown.Editor.js +++ b/Markdown.Editor.js @@ -148,18 +148,15 @@ // // If remove is true, the whitespace disappears. Chunks.prototype.trimWhitespace = function (remove) { - - this.selection = this.selection.replace(/^(\s*)/, ""); - - if (!remove) { - this.before += re.$1; - } - - this.selection = this.selection.replace(/(\s*)$/, ""); - - if (!remove) { - this.after = re.$1 + this.after; + var beforeReplacer, afterReplacer, that = this; + if (remove) { + beforeReplacer = afterReplacer = ""; + } else { + beforeReplacer = function (s) { that.before += s; return ""; } + afterReplacer = function (s) { that.after = s + that.after; return ""; } } + + this.selection = this.selection.replace(/^(\s*)/, beforeReplacer).replace(/(\s*)$/, afterReplacer); }; @@ -230,7 +227,7 @@ // A collection of the important regions on the page. // Cached so we don't have to keep traversing the DOM. - // Also holds ieRetardedClick and ieCachedRange, where necessary; working around + // Also holds ieCachedRange and ieCachedScrollTop, where necessary; working around // this issue: // Internet explorer has problems with CSS sprite buttons that use HTML // lists. When you click on the background image "button", IE will @@ -681,7 +678,7 @@ this.setInputAreaSelectionStartEnd = function () { - if (!panels.ieRetardedClick && (inputArea.selectionStart || inputArea.selectionStart === 0)) { + if (!panels.ieCachedRange && (inputArea.selectionStart || inputArea.selectionStart === 0)) { stateObj.start = inputArea.selectionStart; stateObj.end = inputArea.selectionEnd; @@ -691,16 +688,9 @@ stateObj.text = util.fixEolChars(inputArea.value); // IE loses the selection in the textarea when buttons are - // clicked. On IE we cache the selection and set a flag - // which we check for here. - var range; - if (panels.ieRetardedClick && panels.ieCachedRange) { - range = panels.ieCachedRange; - panels.ieRetardedClick = false; - } - else { - range = doc.selection.createRange(); - } + // clicked. On IE we cache the selection. Here, if something is cached, + // we take it. + var range = panels.ieCachedRange || doc.selection.createRange(); var fixedRange = util.fixEolChars(range.text); var marker = "\x07"; @@ -725,6 +715,11 @@ range.text = fixedRange; } + if (panels.ieCachedRange) + stateObj.scrollTop = panels.ieCachedScrollTop; // this is set alongside with ieCachedRange + + panels.ieCachedRange = null; + this.setInputAreaSelection(); } }; @@ -1347,8 +1342,8 @@ if (doc.activeElement && doc.activeElement !== panels.input) { // we're not even in the input box, so there's no selection return; } - panels.ieRetardedClick = true; panels.ieCachedRange = document.selection.createRange(); + panels.ieCachedScrollTop = panels.input.scrollTop; }; } @@ -1520,11 +1515,9 @@ chunk.selection = chunk.selection.replace(/\n{2,}/g, "\n"); // Look for stars before and after. Is the chunk already marked up? - chunk.before.search(/(\**$)/); - var starsBefore = re.$1; - - chunk.after.search(/(^\**)/); - var starsAfter = re.$1; + // note that these regex matches cannot fail + var starsBefore = /(\**$)/.exec(chunk.before)[0]; + var starsAfter = /(^\**)/.exec(chunk.after)[0]; var prevStars = Math.min(starsBefore.length, starsAfter.length); @@ -1792,7 +1785,7 @@ if (chunk.before) { var lines = chunk.before.replace(/\n$/, "").split("\n"); var inChain = false; - for (var i = 0; i < lines.length; i++ ) { + for (var i = 0; i < lines.length; i++) { var good = false; line = lines[i]; inChain = inChain && line.length > 0; // c) any non-empty line continues the chain