1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-27 12:36:14 +01:00

Added password coloring pipe (which also sanitizes HTML) (#24)

This commit is contained in:
Andreas Schneider 2018-12-08 17:06:11 +01:00 committed by Kyle Spearrin
parent 9283a29d35
commit 37616a148a

View File

@ -0,0 +1,20 @@
import { Pipe, PipeTransform } from '@angular/core';
import { SafeHtml } from '@angular/platform-browser';
/**
* A pipe that sanitizes HTML and highlights numbers and special characters (in different colors each).
*/
@Pipe({ name: 'colorPassword' })
export class ColorPasswordPipe implements PipeTransform {
transform(password: string): SafeHtml {
return password
// Sanitize HTML first.
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
// Replace special chars (since that will exclude numbers anyway).
.replace(/((&amp;|&lt;|&gt;|[^\w ])+)/g, `<span class="passwordSpecial">$1</span>`)
// Finally replace the numbers.
.replace(/(\d+)/g, `<span class="passwordNumber">$1</span>`);
}
}