@@ -49,5 +49,80 @@ describe('Attributes to String', () => {
4949 expect ( resultString ) . toEqual ( ' style="text-align:left; " rows="4" cols="2" colWidths="250, 250"' )
5050 done ( )
5151 } )
52+ it ( 'Should rignore attributes with forbidden characters in keys and values' , done => {
53+ const attr = {
54+ "style" : {
55+ "text-align" : "left"
56+ } ,
57+ "rows" : 4 ,
58+ "cols" : 2 ,
59+ "colWidths" : [ 250 , 250 ] ,
60+ "<ls" : "\"></p><h1>test</h1><p class=\"" ,
61+ "\"></p><h1>test</h1><p class=\"" : 1
62+ } as Attributes ;
63+
64+ const resultString = attributeToString ( attr ) ;
65+
66+ expect ( resultString ) . toEqual ( ' style=\"text-align:left; \" rows=\"4\" cols=\"2\" colWidths=\"250, 250\" <ls=\""></p><h1>test</h1><p class="\"' )
67+ done ( ) ;
68+ } ) ;
69+ it ( 'Should handle object attribute values correctly' , done => {
70+ const attr = {
71+ "style" : {
72+ "color" : "red" ,
73+ "font-size" : "14px"
74+ }
75+ } as Attributes ;
76+
77+ const resultString = attributeToString ( attr ) ;
78+
79+ expect ( resultString ) . toEqual ( ' style="color:red; font-size:14px; "' ) ;
80+ done ( ) ;
81+ } ) ;
82+ it ( 'Should convert arrays into comma-separated values' , done => {
83+ const attr = {
84+ "data-values" : [ 10 , 20 , 30 ]
85+ } as Attributes ;
86+
87+ const resultString = attributeToString ( attr ) ;
88+
89+ expect ( resultString ) . toEqual ( ' data-values="10, 20, 30"' ) ;
90+ done ( ) ;
91+ } ) ;
92+ it ( 'Should handle special characters in values properly' , done => {
93+ const attr = {
94+ "title" : 'This & That > Those < Them "Quoted"' ,
95+ "description" : "Hello <script>alert(xss)</script>"
96+ } as Attributes ;
97+
98+ const resultString = attributeToString ( attr ) ;
99+
100+ expect ( resultString ) . toEqual ( ' title="This & That > Those < Them "Quoted"" description="Hello <script>alert(xss)</script>"' ) ;
101+ done ( ) ;
102+ } ) ;
103+
104+ it ( 'Should handle mixed types of values properly' , done => {
105+ const attr = {
106+ "rows" : 5 ,
107+ "isEnabled" : true ,
108+ "ids" : [ 101 , 102 ] ,
109+ "style" : { "margin" : "10px" , "padding" : "5px" }
110+ } as Attributes ;
111+
112+ const resultString = attributeToString ( attr ) ;
113+
114+ expect ( resultString ) . toEqual ( ' rows="5" isEnabled="true" ids="101, 102" style="margin:10px; padding:5px; "' ) ;
115+ done ( ) ;
116+ } ) ;
117+ it ( 'Should sanitize both keys and values to prevent HTML injection' , done => {
118+ const attr = {
119+ "<script>alert('key')</script>" : "test" ,
120+ "safeKey" : "<script>alert(xss)</script>"
121+ } as Attributes ;
122+
123+ const resultString = attributeToString ( attr ) ;
52124
125+ expect ( resultString ) . toEqual ( ' safeKey="<script>alert(xss)</script>"' ) ;
126+ done ( ) ;
127+ } ) ;
53128} )
0 commit comments