Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firefox createLink bug workaround #1364

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions spec/anchor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ describe('Anchor Button TestCase', function () {
expect(this.el.innerHTML.indexOf('<a href="http://test.com">lorem ipsum</a>')).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 <br> may be added when insertHTML is used to add the link internally.
expect(this.el.innerHTML.indexOf('<a href="http://www.st\u00E4dtlifest.ch/">lorem ipsum</a>')).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'),
Expand Down
1 change: 1 addition & 0 deletions src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
19 changes: 19 additions & 0 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down