From 7be90a885c34e60820748aa96da415d8daa0749e Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Thu, 10 Mar 2016 22:43:08 +1100 Subject: [PATCH] FIX: bolding text when selection has a leading space --- .../javascripts/discourse/components/d-editor.js.es6 | 9 +++++++-- test/javascripts/components/d-editor-test.js.es6 | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index 55a673d1b..d04e3f754 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -360,14 +360,19 @@ export default Ember.Component.extend({ const textarea = this.$('textarea.d-editor-input')[0]; const value = textarea.value; - const start = textarea.selectionStart; + var start = textarea.selectionStart; let end = textarea.selectionEnd; - // Windows selects the space after a word when you double click + // trim trailing spaces cause **test ** would be invalid while (end > start && /\s/.test(value.charAt(end-1))) { end--; } + // trim leading spaces cause ** test** would be invalid + while(end > start && /\s/.test(value.charAt(start))) { + start++; + } + const selVal = value.substring(start, end); const pre = value.slice(0, start); const post = value.slice(end); diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6 index 821b4d92f..ffcb67d33 100644 --- a/test/javascripts/components/d-editor-test.js.es6 +++ b/test/javascripts/components/d-editor-test.js.es6 @@ -62,6 +62,18 @@ function testCase(title, testFunc) { }); } +testCase(`selecting the space before a word`, function(assert, textarea) { + textarea.selectionStart = 5; + textarea.selectionEnd = 7; + + click(`button.bold`); + andThen(() => { + assert.equal(this.get('value'), `hello **w**orld.`); + assert.equal(textarea.selectionStart, 8); + assert.equal(textarea.selectionEnd, 9); + }); +}); + testCase(`selecting the space after a word`, function(assert, textarea) { textarea.selectionStart = 0; textarea.selectionEnd = 6;