File size: 2,215 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * @jest-environment jsdom
 */

import highlight from '../';

describe( 'highlight', () => {
	describe( 'unit test', () => {
		test( "should return html as is if there's no term", () => {
			expect( highlight( '', 'lorem ipsum hello world' ) ).toEqual( 'lorem ipsum hello world' );
		} );

		test( 'should wrap the term with <mark> when no custom wrapper node is defined', () => {
			expect( highlight( 'hello', 'lorem ipsum hello world' ) ).toContain(
				'lorem ipsum <mark>hello</mark> world'
			);
		} );

		test( 'should wrap the term with the custom wrapper node when provided', () => {
			const span = document.createElement( 'span' );
			span.setAttribute( 'class', 'marker' );
			expect( highlight( 'hello', 'hello world', span ) ).toContain(
				'<span class="marker">hello</span> world'
			);
		} );

		test( 'should wrap case-insensitively, within a single word, with multiple matches, over multiple lines', () => {
			expect( highlight( 'HEllO', 'merhabaHEllO\nselamHEllOdunya' ) ).toContain(
				'merhaba<mark>HEllO</mark>\nselam<mark>HEllO</mark>dunya'
			);
		} );

		test( 'should be able to handle html elements with children and text nodes', () => {
			expect( highlight( 'hello', '<div>hello world<span>hello world</span></div>' ) ).toContain(
				'<div><mark>hello</mark> world<span><mark>hello</mark> world</span></div>'
			);
		} );
	} );

	describe( 'edge cases', () => {
		describe( 'unicode', () => {
			test( 'should wrap when the text and query contains unicode chars', () => {
				expect( highlight( 'üşiöqç', 'lorem ıpsum üşiöqç' ) ).toContain(
					'lorem ıpsum <mark>üşiöqç</mark>'
				);
			} );
		} );

		describe( 'html', () => {
			test( 'should not match parts of html tags', () => {
				expect( highlight( 'img', 'hello<img src="/"> world' ) ).toContain(
					'hello<img src="/"> world'
				);
			} );

			test( 'should prevent html tag injection', () => {
				expect( highlight( 'script', '<script></script>' ) ).toContain( '<script></script>' );
			} );

			test( 'should prevent html attribute injection', () => {
				expect( highlight( 'href', '<a href="/hello">hello</a>' ) ).toContain(
					'<a href="/hello">hello</a>'
				);
			} );
		} );
	} );
} );