Skip to content

Commit

Permalink
Merge pull request #1258 from kevindew/single-slash-link
Browse files Browse the repository at this point in the history
Only add schemes to URLs with hostnames
  • Loading branch information
nmielnik authored Feb 9, 2017
2 parents ba69260 + 6d67581 commit 05a0037
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
57 changes: 57 additions & 0 deletions spec/anchor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,63 @@ describe('Anchor Button TestCase', function () {
expect(link.getAttribute('href')).toBe(validHashLink);
});

it('should not add a scheme to an absolute path', function () {
var editor = this.newMediumEditor('.editor', {
anchor: {
linkValidation: true
}
}),
absolutePath = '/test',
link,
anchorExtension = editor.getExtensionByName('anchor');

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

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

it('should not add a scheme to an obviously relative path', function () {
var editor = this.newMediumEditor('.editor', {
anchor: {
linkValidation: true
}
}),
relativePath = 'test/file.html',
link,
anchorExtension = editor.getExtensionByName('anchor');

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

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

it('should add a scheme to a localhost url', function () {
var editor = this.newMediumEditor('.editor', {
anchor: {
linkValidation: true
}
}),
localhostUrl = 'http://localhost',
link,
anchorExtension = editor.getExtensionByName('anchor');

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

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

it('should change spaces to %20 for a valid url if linkValidation option is set to true', function () {
var editor = this.newMediumEditor('.editor', {
anchor: {
Expand Down
29 changes: 19 additions & 10 deletions src/js/extensions/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@
// Matches common external protocols "mailto:" "tel:" "maps:"
// Matches relative hash link, begins with "#"
var urlSchemeRegex = /^([a-z]+:)?\/\/|^(mailto|tel|maps):|^\#/i,
hasScheme = urlSchemeRegex.test(value),
scheme = '',
// telRegex is a regex for checking if the string is a telephone number
telRegex = /^\+?\s?\(?(?:\d\s?\-?\)?){3,20}$/,
urlParts = value.match(/^(.*?)(?:\?(.*?))?(?:#(.*))?$/),
Expand All @@ -266,17 +268,24 @@

if (telRegex.test(value)) {
return 'tel:' + value;
} else {
// Check for URL scheme and default to http:// if none found
return (urlSchemeRegex.test(value) ? '' : 'http://') +
// Ensure path is encoded
this.ensureEncodedUri(path) +
// Ensure query is encoded
(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);
}

if (!hasScheme) {
var host = path.split('/')[0];
// if the host part of the path looks like a hostname
if (host.match(/.+(\.|:).+/) || host === 'localhost') {
scheme = 'http://';
}
}

return scheme +
// Ensure path is encoded
this.ensureEncodedUri(path) +
// Ensure query is encoded
(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);
},

doFormCancel: function () {
Expand Down

0 comments on commit 05a0037

Please sign in to comment.