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

add noopener & noreferrer into targetBlank #1355

Merged
merged 1 commit into from
Jul 27, 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
7 changes: 7 additions & 0 deletions dist/js/medium-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ MediumEditor.extensions = {};
Util.moveTextRangeIntoElement(textNodes[0], textNodes[textNodes.length - 1], anchor);
anchor.setAttribute('href', href);
if (target) {
if (target === '_blank') {
anchor.setAttribute('rel', 'noopener noreferrer');
}
anchor.setAttribute('target', target);
}
return anchor;
Expand Down Expand Up @@ -989,12 +992,14 @@ MediumEditor.extensions = {};
var i, url = anchorUrl || false;
if (el.nodeName.toLowerCase() === 'a') {
el.target = '_blank';
el.rel = 'noopener noreferrer';
} else {
el = el.getElementsByTagName('a');

for (i = 0; i < el.length; i += 1) {
if (false === url || url === el[i].attributes.href.value) {
el[i].target = '_blank';
el[i].rel = 'noopener noreferrer';
}
}
}
Expand All @@ -1008,12 +1013,14 @@ MediumEditor.extensions = {};
var i;
if (el.nodeName.toLowerCase() === 'a') {
el.removeAttribute('target');
el.removeAttribute('rel');
} else {
el = el.getElementsByTagName('a');

for (i = 0; i < el.length; i += 1) {
if (anchorUrl === el[i].attributes.href.value) {
el[i].removeAttribute('target');
el[i].removeAttribute('rel');
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions spec/anchor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ describe('Anchor Button TestCase', function () {
link = editor.elements[0].querySelector('a');
expect(link).not.toBeNull();
expect(link.target).toBe('_blank');
expect(link.rel).toBe('noopener noreferrer');
});

it('should add target="_blank" when respective option is set to true', function () {
Expand All @@ -608,6 +609,7 @@ describe('Anchor Button TestCase', function () {
link = editor.elements[0].querySelector('a');
expect(link).not.toBeNull();
expect(link.target).toBe('_blank');
expect(link.rel).toBe('noopener noreferrer');
});

it('should create a button when user selects this option and presses enter', function () {
Expand Down Expand Up @@ -717,6 +719,7 @@ describe('Anchor Button TestCase', function () {
fireEvent(anchorExtension.getForm().querySelector('a.medium-editor-toolbar-save'), 'click');
link = editor.elements[0].querySelector('a');
expect(link.target).toBe('_blank');
expect(link.rel).toBe('noopener noreferrer');

selectElementContentsAndFire(editor.elements[0]);
anchorExtension.showForm('http://test.com');
Expand Down
7 changes: 7 additions & 0 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
Util.moveTextRangeIntoElement(textNodes[0], textNodes[textNodes.length - 1], anchor);
anchor.setAttribute('href', href);
if (target) {
if (target === '_blank') {
anchor.setAttribute('rel', 'noopener noreferrer');
}
anchor.setAttribute('target', target);
}
return anchor;
Expand Down Expand Up @@ -583,12 +586,14 @@
var i, url = anchorUrl || false;
if (el.nodeName.toLowerCase() === 'a') {
el.target = '_blank';
el.rel = 'noopener noreferrer';
} else {
el = el.getElementsByTagName('a');

for (i = 0; i < el.length; i += 1) {
if (false === url || url === el[i].attributes.href.value) {
el[i].target = '_blank';
el[i].rel = 'noopener noreferrer';
}
}
}
Expand All @@ -602,12 +607,14 @@
var i;
if (el.nodeName.toLowerCase() === 'a') {
el.removeAttribute('target');
el.removeAttribute('rel');
} else {
el = el.getElementsByTagName('a');

for (i = 0; i < el.length; i += 1) {
if (anchorUrl === el[i].attributes.href.value) {
el[i].removeAttribute('target');
el[i].removeAttribute('rel');
}
}
}
Expand Down