diff --git a/packages/compiler-dom/__tests__/decoderHtmlBrowser.spec.ts b/packages/compiler-dom/__tests__/decoderHtmlBrowser.spec.ts new file mode 100644 index 00000000000..9100927defd --- /dev/null +++ b/packages/compiler-dom/__tests__/decoderHtmlBrowser.spec.ts @@ -0,0 +1,41 @@ +import { decodeHtmlBrowser } from '../src/decodeHtmlBrowser' + +describe('decodeHtmlBrowser', () => { + it('should decode HTML correctly', () => { + expect(decodeHtmlBrowser(' abc 123 ')).toBe(' abc 123 ') + + expect(decodeHtmlBrowser('&')).toBe('&') + expect(decodeHtmlBrowser('&')).toBe('&') + expect(decodeHtmlBrowser('&')).toBe('&') + + expect(decodeHtmlBrowser('<')).toBe('<') + expect(decodeHtmlBrowser('<')).toBe('<') + expect(decodeHtmlBrowser('<')).toBe('<') + + expect(decodeHtmlBrowser('>')).toBe('>') + expect(decodeHtmlBrowser('>')).toBe('>') + expect(decodeHtmlBrowser('>')).toBe('>') + + expect(decodeHtmlBrowser(' ')).toBe('\u00a0') + expect(decodeHtmlBrowser('"')).toBe('"') + expect(decodeHtmlBrowser(''')).toBe("'") + + expect(decodeHtmlBrowser('É')).toBe('\u00c9') + expect(decodeHtmlBrowser('É')).toBe('\u00c9') + expect(decodeHtmlBrowser('É')).toBe('\u00c9') + + // #3001 html tags inside attribute values + expect(decodeHtmlBrowser('Text', true)).toBe( + 'Text' + ) + expect(decodeHtmlBrowser('&', true)).toBe( + '&' + ) + expect( + decodeHtmlBrowser( + '<strong>&</strong>', + true + ) + ).toBe('&') + }) +}) diff --git a/packages/compiler-dom/src/decodeHtmlBrowser.ts b/packages/compiler-dom/src/decodeHtmlBrowser.ts index 7ee93c0b8db..cca3bb12a6e 100644 --- a/packages/compiler-dom/src/decodeHtmlBrowser.ts +++ b/packages/compiler-dom/src/decodeHtmlBrowser.ts @@ -2,7 +2,15 @@ let decoder: HTMLDivElement -export function decodeHtmlBrowser(raw: string): string { - ;(decoder || (decoder = document.createElement('div'))).innerHTML = raw - return decoder.textContent as string +export function decodeHtmlBrowser(raw: string, asAttr = false): string { + if (!decoder) { + decoder = document.createElement('div') + } + if (asAttr) { + decoder.innerHTML = `