File tree Expand file tree Collapse file tree 2 files changed +11
-2
lines changed
Expand file tree Collapse file tree 2 files changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -11,11 +11,11 @@ export function sanitizeLinks(html: string): string {
1111 const doc = parser . parseFromString ( html , 'text/html' ) ;
1212 const elements = Array . from ( doc . body . querySelectorAll ( '*' ) ) ;
1313
14- elements . forEach ( el => {
14+ elements . reverse ( ) . forEach ( el => {
1515 const text = el . textContent || '' ;
1616
1717 if ( el . tagName !== 'A' ) {
18- el . replaceWith ( doc . createTextNode ( text ) ) ;
18+ el . replaceWith ( ... el . childNodes ) ;
1919 return ;
2020 }
2121
Original file line number Diff line number Diff line change @@ -152,5 +152,14 @@ describe('sanitize', () => {
152152
153153 expect ( result ) . toBe ( text ) ;
154154 } ) ;
155+
156+ test ( 'preserves links nested in other elements' , ( ) => {
157+ const nested = '<div>Please click <a href="https://google.com">here</a></div>' ;
158+ const result = sanitizeLinks ( nested ) ;
159+
160+ expect ( result ) . toContain ( '<a' ) ;
161+ expect ( result ) . toContain ( 'href="https://google.com"' ) ;
162+ expect ( result ) . toContain ( '>here</a>' ) ;
163+ } ) ;
155164 } ) ;
156165} ) ;
You can’t perform that action at this time.
0 commit comments