-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve html attribute escaping function
- Loading branch information
1 parent
f6e4c22
commit 10641a6
Showing
3 changed files
with
76 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { sanitizeHtmlAttribute } from '../../utils/sanitizeHtmlAttribute'; | ||
|
||
describe( 'sanitize HTML attribute', () => { | ||
it( 'should convert a number to a string', () => { | ||
const value = sanitizeHtmlAttribute( 500 ); | ||
expect( value ).toEqual( '500' ); | ||
} ); | ||
|
||
it( 'should convert an object to a string', () => { | ||
const value = sanitizeHtmlAttribute( { foo: 'bar' } ); | ||
expect( value ).toEqual( '{"foo":"bar"}' ); | ||
} ); | ||
|
||
it( 'should convert an array to a string', () => { | ||
const value = sanitizeHtmlAttribute( [ 'foo', 'bar' ] ); | ||
expect( value ).toEqual( '["foo","bar"]' ); | ||
} ); | ||
|
||
it( 'should handle undefined', () => { | ||
const value = sanitizeHtmlAttribute( undefined ); | ||
expect( value ).toEqual( '' ); | ||
} ); | ||
|
||
it( 'should handle null', () => { | ||
const value = sanitizeHtmlAttribute( null ); | ||
expect( value ).toEqual( '' ); | ||
} ); | ||
|
||
it( 'should escape HTML special characters', () => { | ||
const value = sanitizeHtmlAttribute( 'foo & <bar> "quote" \'single\'' ); | ||
expect( value ).toEqual( 'foo & <bar> "quote" 'single'' ); | ||
} ); | ||
|
||
it( 'should handle boolean values', () => { | ||
expect( sanitizeHtmlAttribute( true ) ).toEqual( 'true' ); | ||
expect( sanitizeHtmlAttribute( false ) ).toEqual( 'false' ); | ||
} ); | ||
|
||
it( 'should handle special number values', () => { | ||
expect( sanitizeHtmlAttribute( NaN ) ).toEqual( 'NaN' ); | ||
expect( sanitizeHtmlAttribute( Infinity ) ).toEqual( 'Infinity' ); | ||
expect( sanitizeHtmlAttribute( -Infinity ) ).toEqual( '-Infinity' ); | ||
} ); | ||
|
||
it( 'should handle empty string', () => { | ||
const value = sanitizeHtmlAttribute( '' ); | ||
expect( value ).toEqual( '' ); | ||
} ); | ||
} ); |
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 @@ | ||
export const sanitizeHtmlAttribute = ( value ) => { | ||
if ( null === value || undefined === value ) { | ||
return ''; | ||
} | ||
|
||
let stringValue = ''; | ||
|
||
if ( 'object' === typeof value ) { | ||
try { | ||
stringValue = JSON.stringify( value ); | ||
} catch ( e ) { | ||
return ''; | ||
} | ||
} else { | ||
stringValue = String( value ); | ||
} | ||
|
||
// Replace characters like &, <, >, " with their HTML entity equivalents | ||
return stringValue | ||
.replace( /&/g, '&' ) | ||
.replace( /</g, '<' ) | ||
.replace( />/g, '>' ) | ||
.replace( /"/g, '"' ) | ||
.replace( /'/g, ''' ); | ||
}; |