Skip to content

Commit a976bd9

Browse files
stepankuzmingithub-actions[bot]
authored andcommitted
Fix sanitizeLinks to preserve nested elements (internal-8812)
GitOrigin-RevId: 2a466ce26aaa289522a0031eba01ebac83c70087
1 parent 111e29e commit a976bd9

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/util/sanitize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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

test/unit/util/sanitize.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

0 commit comments

Comments
 (0)