diff --git a/spec/anchor.spec.js b/spec/anchor.spec.js index f4f6b743f..b493cf3a4 100644 --- a/spec/anchor.spec.js +++ b/spec/anchor.spec.js @@ -88,6 +88,26 @@ describe('Anchor Button TestCase', function () { expect(this.el.innerHTML.indexOf('lorem ipsum')).toBe(0); }); + // https://github.com/yabwe/medium-editor/pull/1364 + it('should create a link containing utf-8 characters correctly when user presses enter', function () { + spyOn(MediumEditor.prototype, 'createLink').and.callThrough(); + var editor = this.newMediumEditor('.editor'), + toolbar = editor.getExtensionByName('toolbar'), + button, input; + + selectElementContents(editor.elements[0]); + button = toolbar.getToolbarElement().querySelector('[data-action="createLink"]'); + fireEvent(button, 'click'); + input = editor.getExtensionByName('anchor').getInput(); + input.value = 'http://www.st\u00E4dtlifest.ch/'; + fireEvent(input, 'keyup', { + keyCode: MediumEditor.util.keyCode.ENTER + }); + expect(editor.createLink).toHaveBeenCalled(); + // A trailing
may be added when insertHTML is used to add the link internally. + expect(this.el.innerHTML.indexOf('lorem ipsum')).toBe(0); + }); + it('should remove the extra white spaces in the link when user presses enter', function () { spyOn(MediumEditor.prototype, 'createLink').and.callThrough(); var editor = this.newMediumEditor('.editor'), diff --git a/src/js/core.js b/src/js/core.js index e71455b95..a7b0a3e77 100644 --- a/src/js/core.js +++ b/src/js/core.js @@ -1149,6 +1149,7 @@ this.importSelection(exportedSelection); } else { this.options.ownerDocument.execCommand('createLink', false, targetUrl); + MediumEditor.util.ensureHref(MediumEditor.selection.getSelectionStart(this.options.ownerDocument), targetUrl); } if (this.options.targetBlank || opts.target === '_blank') { diff --git a/src/js/util.js b/src/js/util.js index c2bf06c45..a94eeb4ef 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -572,6 +572,25 @@ return doc.execCommand('formatBlock', false, tagName); }, + /* + * this function is called to ensure href is set correctly as FF does "encodeURI" on href value when execCommand createLink. + * see also https://bugzilla.mozilla.org/show_bug.cgi?id=451142 + */ + ensureHref: function (el, anchorUrl) { + var i, url = anchorUrl; + if (el.nodeName.toLowerCase() === 'a') { + el.attributes.href.value = url; + } else { + el = el.getElementsByTagName('a'); + + for (i = 0; i < el.length; i += 1) { + if (encodeURI(url) === el[i].attributes.href.value) { + el[i].attributes.href.value = url; + } + } + } + }, + /** * Set target to blank on the given el element *