Adds rel=noreferrer and rel=noopener to autolinks (Fixes #46)

This commit is contained in:
Dan Seripap 2016-11-28 12:51:27 -05:00
parent 282f43e8f1
commit 4a5db9e2ad
5 changed files with 27 additions and 10 deletions

View File

@ -211,9 +211,19 @@ export default class App {
// Prevents input from having injected markup
cleanInput(input) {
input = input.replace(/\r?\n/g, '<br />');
let sanitized = he.encode(input);
sanitized = Autolinker.link(sanitized);
return sanitized;
return this.autolinker(he.encode(input));
}
// Adds rel=noopener noreferrer to autolinked links (Addresses #46 from @Mickael-van-der-Beek)
autolinker(sanitized) {
return Autolinker.link(sanitized, {
replaceFn: function(match) {
const tag = match.buildTag();
tag.setAttr('rel', 'noopener noreferrer');
return tag;
}
});
}
// Sets the client's username

View File

@ -358,3 +358,9 @@ html.no-touchevents .chat #input-icons {
font-style: italic;
display: block;
}
.external-link:after {
content: 'External Link';
color: #FFF;
background-color: red;
}

File diff suppressed because one or more lines are too long

View File

@ -38,10 +38,11 @@ describe('App', () => {
app = new App();
});
it('should create HTML links from URLs', () => {
it('should create HTML links from URLs with "rel=noopener noreferrer"', () => {
let input = app.cleanInput('cnn.com');
assert.equal(input, '<a href="http://cnn.com" target="_blank">cnn.com</a>');
assert.equal(input, '<a href="http://cnn.com" target="_blank" rel="noopener noreferrer">cnn.com</a>');
});
});
});