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

Don't encode fragment as part of linkValidation #1257

Merged
merged 1 commit into from
Feb 5, 2017
Merged
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
26 changes: 26 additions & 0 deletions spec/anchor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,32 @@ describe('Anchor Button TestCase', function () {
expect(link.href).toBe(expectedOpts.value);
});

it('should not change # to %23 for a valid url if linkValidation option is set to true', function () {
var editor = this.newMediumEditor('.editor', {
anchor: {
linkValidation: true
}
}),
link,
anchorExtension = editor.getExtensionByName('anchor'),
expectedOpts = {
value: 'http://example.com/?query=value#anchor',
target: '_self'
};

spyOn(editor, 'execAction').and.callThrough();

selectElementContentsAndFire(editor.elements[0]);
anchorExtension.showForm(expectedOpts.value);
fireEvent(anchorExtension.getForm().querySelector('a.medium-editor-toolbar-save'), 'click');

expect(editor.execAction).toHaveBeenCalledWith('createLink', expectedOpts);

link = editor.elements[0].querySelector('a');
expect(link).not.toBeNull();
expect(link.href).toBe(expectedOpts.value);
});

it('should not encode an encoded URL if linkValidation option is set to true', function () {
var editor = this.newMediumEditor('.editor', {
anchor: {
Expand Down
12 changes: 8 additions & 4 deletions src/js/extensions/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@
var urlSchemeRegex = /^([a-z]+:)?\/\/|^(mailto|tel|maps):|^\#/i,
// telRegex is a regex for checking if the string is a telephone number
telRegex = /^\+?\s?\(?(?:\d\s?\-?\)?){3,20}$/,
split = value.split('?'),
path = split[0],
query = split[1];
urlParts = value.match(/^(.*?)(?:\?(.*?))?(?:#(.*))?$/),
path = urlParts[1],
query = urlParts[2],
fragment = urlParts[3];

if (telRegex.test(value)) {
return 'tel:' + value;
Expand All @@ -271,7 +272,10 @@
// Ensure path is encoded
this.ensureEncodedUri(path) +
// Ensure query is encoded
(query === undefined ? '' : '?' + this.ensureEncodedQuery(query));
(query === undefined ? '' : '?' + this.ensureEncodedQuery(query)) +
// Include fragment unencoded as encodeUriComponent is too
// heavy handed for the many characters allowed in a fragment
(fragment === undefined ? '' : '#' + fragment);
}
},

Expand Down