-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
htmlEncode
pipe to encode HTML code to be rendered …
…as string (#1575) * add documentation note
- Loading branch information
Showing
4 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { HtmlEncodePipe } from './html-encode.pipe'; | ||
|
||
describe('Html Encode Pipe', () => { | ||
let htmlEncodePipe: HtmlEncodePipe; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [HtmlEncodePipe], | ||
}); | ||
htmlEncodePipe = TestBed.inject(HtmlEncodePipe); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(htmlEncodePipe).toBeTruthy(); | ||
}); | ||
|
||
it.each([ | ||
['<img src=https://test.jpg>', '<img src=https://test.jpg>'], | ||
['?hello&world', '?hello&world'], | ||
])(`should transform '%s' to '%s'`, (input, output) => { | ||
expect(htmlEncodePipe.transform(input)).toEqual(output); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
/** | ||
* The HTML encode pipe simply replaces HTML special characters like angle brackets (< and >) with HTML entities | ||
* so they can be displayed as plain text in a web page. | ||
* https://jasonwatmore.com/vanilla-js-html-encode-in-javascript | ||
*/ | ||
@Pipe({ name: 'htmlEncode', pure: true }) | ||
export class HtmlEncodePipe implements PipeTransform { | ||
transform(value: string): string { | ||
return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | ||
} | ||
} |