diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index 48cd3b222..8c71ad9de 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -299,14 +299,20 @@ export default Ember.Component.extend({ if (!this.get('ready')) { return; } const textarea = this.$('textarea.d-editor-input')[0]; + const value = textarea.value; const start = textarea.selectionStart; - const end = textarea.selectionEnd; + let end = textarea.selectionEnd; - const value = textarea.value.substring(start, end); - const pre = textarea.value.slice(0, start); - const post = textarea.value.slice(end); + // Windows selects the space after a word when you double click + while (end > start && /\s/.test(value.charAt(end-1))) { + end--; + } - return { start, end, value, pre, post }; + const selVal = value.substring(start, end); + const pre = value.slice(0, start); + const post = value.slice(end); + + return { start, end, value: selVal, pre, post }; }, _selectText(from, length) { diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6 index a6454da7e..8b7f23350 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 after a word`, function(assert, textarea) { + textarea.selectionStart = 0; + textarea.selectionEnd = 6; + + click(`button.bold`); + andThen(() => { + assert.equal(this.get('value'), `**hello** world.`); + assert.equal(textarea.selectionStart, 2); + assert.equal(textarea.selectionEnd, 7); + }); +}); + testCase(`bold button with no selection`, function(assert, textarea) { click(`button.bold`); andThen(() => { @@ -231,10 +243,8 @@ componentTest('advanced code', { test(assert) { const textarea = this.$('textarea.d-editor-input')[0]; - andThen(() => { - textarea.selectionStart = 0; - textarea.selectionEnd = textarea.value.length; - }); + textarea.selectionStart = 0; + textarea.selectionEnd = textarea.value.length; click('button.code'); andThen(() => {