@@ -132,6 +132,14 @@ describe('utils', () => {
132132 assert ( result . includes ( 'link text' ) ) ;
133133 assert ( result . includes ( 'data-local-file="src/file.ts"' ) ) ;
134134 } ) ;
135+
136+ it ( 'should escape HTML special characters in file paths' , async ( ) => {
137+ const html = makePermalink ( 'src/file&name"test.ts' , 10 ) ;
138+ const result = await utils . processPermalinks ( html , repoName , authority , async ( ) => true ) ;
139+
140+ assert ( result . includes ( 'data-local-file="src/file&name"test.ts"' ) ) ;
141+ assert ( ! result . includes ( 'data-local-file="src/file&name"test.ts"' ) ) ;
142+ } ) ;
135143 } ) ;
136144
137145 describe ( 'processDiffLinks' , ( ) => {
@@ -233,5 +241,40 @@ describe('utils', () => {
233241 assert ( result . includes ( 'data-local-file="src/found.ts"' ) ) ;
234242 assert ( ! result . includes ( 'data-local-file="src/other.ts"' ) ) ;
235243 } ) ;
244+
245+ it ( 'should escape HTML special characters in file names' , async ( ) => {
246+ const hashMap : Record < string , string > = { [ diffHash ] : 'src/file&name"test.ts' } ;
247+ const html = makeDiffLink ( diffHash , 10 ) ;
248+ const result = await utils . processDiffLinks ( html , repoOwner , repoName , authority , hashMap , prNumber ) ;
249+
250+ assert ( result . includes ( 'data-local-file="src/file&name"test.ts"' ) ) ;
251+ assert ( ! result . includes ( 'data-local-file="src/file&name"test.ts"' ) ) ;
252+ } ) ;
253+ } ) ;
254+
255+ describe ( 'escapeHtmlAttr' , ( ) => {
256+ it ( 'should escape ampersands' , ( ) => {
257+ assert . strictEqual ( utils . escapeHtmlAttr ( 'foo&bar' ) , 'foo&bar' ) ;
258+ } ) ;
259+
260+ it ( 'should escape double quotes' , ( ) => {
261+ assert . strictEqual ( utils . escapeHtmlAttr ( 'foo"bar' ) , 'foo"bar' ) ;
262+ } ) ;
263+
264+ it ( 'should escape single quotes' , ( ) => {
265+ assert . strictEqual ( utils . escapeHtmlAttr ( 'foo\'bar' ) , 'foo'bar' ) ;
266+ } ) ;
267+
268+ it ( 'should escape angle brackets' , ( ) => {
269+ assert . strictEqual ( utils . escapeHtmlAttr ( '<script>' ) , '<script>' ) ;
270+ } ) ;
271+
272+ it ( 'should escape all special characters together' , ( ) => {
273+ assert . strictEqual ( utils . escapeHtmlAttr ( 'a&b"c\'d<e>f' ) , 'a&b"c'd<e>f' ) ;
274+ } ) ;
275+
276+ it ( 'should return the same string when no special characters' , ( ) => {
277+ assert . strictEqual ( utils . escapeHtmlAttr ( 'normal-file.ts' ) , 'normal-file.ts' ) ;
278+ } ) ;
236279 } ) ;
237280} ) ;
0 commit comments