11import jsdom from 'jsdom' ;
2-
3- import { nodeListToArray } from '../utils' ;
2+ import { nodeListToArray , sanitizeString } from '../utils' ;
43
54describe ( 'MessageInputUtils/nodeListToArray' , ( ) => {
65 it ( 'should convert node list to array' , ( ) => {
@@ -19,7 +18,75 @@ describe('MessageInputUtils/nodeListToArray', () => {
1918 } ) ;
2019
2120 it ( 'should return empty array if nodelist is undefined' , ( ) => {
22- const nodes = nodeListToArray ( null ) ;
21+ const nodes = nodeListToArray ( undefined ) ;
2322 expect ( nodes . length ) . toBe ( 0 ) ;
2423 } ) ;
2524} ) ;
25+
26+ describe ( 'Utils/sanitizeString' , ( ) => {
27+ it ( 'should encode special HTML characters' , ( ) => {
28+ const input = '<div>Hello & "world"!</div>' ;
29+ const expectedOutput = '<div>Hello & \"world\"!</div>' ;
30+ expect ( sanitizeString ( input ) ) . toBe ( expectedOutput ) ;
31+ } ) ;
32+
33+ it ( 'should encode non-English characters correctly' , ( ) => {
34+ const input = '안녕하세요' ;
35+ const expectedOutput = '안녕하세요' ;
36+ expect ( sanitizeString ( input ) ) . toBe ( expectedOutput ) ;
37+ } ) ;
38+
39+ it ( 'should encode emojis as HTML entities' , ( ) => {
40+ const input = '🙂' ;
41+ const expectedOutput = '🙂' ;
42+ expect ( sanitizeString ( input ) ) . toBe ( expectedOutput ) ;
43+ } ) ;
44+
45+ it ( 'should handle mixed content with HTML tags and non-English characters' , ( ) => {
46+ const input = '<p>안녕 & Hello 🙂</p>' ;
47+ const expectedOutput = '<p>안녕 & Hello 🙂</p>' ;
48+ expect ( sanitizeString ( input ) ) . toBe ( expectedOutput ) ;
49+ } ) ;
50+
51+ it ( 'should return an empty string if input is undefined' , ( ) => {
52+ expect ( sanitizeString ( undefined ) ) . toBe ( '' ) ;
53+ } ) ;
54+
55+ it ( 'should return an empty string if input is null' , ( ) => {
56+ expect ( sanitizeString ( null as any ) ) . toBe ( '' ) ;
57+ } ) ;
58+
59+ it ( 'should return an empty string if input is empty' , ( ) => {
60+ expect ( sanitizeString ( '' ) ) . toBe ( '' ) ;
61+ } ) ;
62+
63+ it ( 'should encode spaces as non-breaking spaces' , ( ) => {
64+ const input = 'Hello world!' ; // Note: The space here is a non-breaking space (U+00A0)
65+ const expectedOutput = 'Hello world!' ;
66+ expect ( sanitizeString ( input ) ) . toBe ( expectedOutput ) ;
67+ } ) ;
68+
69+ it ( 'should not double encode already encoded HTML entities' , ( ) => {
70+ const input = '<div>Hello</div>' ;
71+ const expectedOutput = '<div>Hello</div>' ;
72+ expect ( sanitizeString ( input ) ) . toBe ( expectedOutput ) ;
73+ } ) ;
74+
75+ it ( 'should handle long strings without performance issues' , ( ) => {
76+ const input = '<div>' . repeat ( 1000 ) ;
77+ const expectedOutput = '<div>' . repeat ( 1000 ) ;
78+ expect ( sanitizeString ( input ) ) . toBe ( expectedOutput ) ;
79+ } ) ;
80+
81+ it ( 'should handle mixed types of spaces' , ( ) => {
82+ const input = 'Hello\u0020world\u00A0!' ;
83+ const expectedOutput = 'Hello world !' ;
84+ expect ( sanitizeString ( input ) ) . toBe ( expectedOutput ) ;
85+ } ) ;
86+
87+ it ( 'should handle special Unicode control characters' , ( ) => {
88+ const input = 'Hello\u200BWorld' ; // Zero-width space (U+200B)
89+ const expectedOutput = 'Hello\u200BWorld' ;
90+ expect ( sanitizeString ( input ) ) . toBe ( expectedOutput ) ;
91+ } ) ;
92+ } ) ;
0 commit comments