fix(compiler-dom): fix in-browser attribute value decoding w/ html tags
fix #3001
This commit is contained in:
parent
a641eb201f
commit
669037277b
41
packages/compiler-dom/__tests__/decoderHtmlBrowser.spec.ts
Normal file
41
packages/compiler-dom/__tests__/decoderHtmlBrowser.spec.ts
Normal file
@ -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('&lt;')).toBe('<')
|
||||
|
||||
expect(decodeHtmlBrowser('>')).toBe('>')
|
||||
expect(decodeHtmlBrowser('>')).toBe('>')
|
||||
expect(decodeHtmlBrowser('&gt;')).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('<strong>Text</strong>', true)).toBe(
|
||||
'<strong>Text</strong>'
|
||||
)
|
||||
expect(decodeHtmlBrowser('<strong>&</strong>', true)).toBe(
|
||||
'<strong>&</strong>'
|
||||
)
|
||||
expect(
|
||||
decodeHtmlBrowser(
|
||||
'<strong><strong>&</strong></strong>',
|
||||
true
|
||||
)
|
||||
).toBe('<strong><strong>&</strong></strong>')
|
||||
})
|
||||
})
|
@ -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 = `<div foo="${raw.replace(/"/g, '"')}">`
|
||||
return decoder.children[0].getAttribute('foo') as string
|
||||
} else {
|
||||
decoder.innerHTML = raw
|
||||
return decoder.textContent as string
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user