Added password coloring pipe (which also sanitizes HTML) (#24)
This commit is contained in:
parent
9283a29d35
commit
37616a148a
|
@ -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, '<')
|
||||
.replace(/>/g, '>')
|
||||
// Replace special chars (since that will exclude numbers anyway).
|
||||
.replace(/((&|<|>|[^\w ])+)/g, `<span class="passwordSpecial">$1</span>`)
|
||||
// Finally replace the numbers.
|
||||
.replace(/(\d+)/g, `<span class="passwordNumber">$1</span>`);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue