mirror of https://github.com/vuejs/core.git
feat(custom-element): support nonce option for injected style tags
close #6530
This commit is contained in:
parent
60a88a2b12
commit
bb4a02a70c
|
@ -718,6 +718,23 @@ describe('defineCustomElement', () => {
|
||||||
await nextTick()
|
await nextTick()
|
||||||
assertStyles(el, [`div { color: blue; }`, `div { color: red; }`])
|
assertStyles(el, [`div { color: blue; }`, `div { color: red; }`])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('with nonce', () => {
|
||||||
|
const Foo = defineCustomElement(
|
||||||
|
{
|
||||||
|
styles: [`div { color: red; }`],
|
||||||
|
render() {
|
||||||
|
return h('div', 'hello')
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ nonce: 'xxx' },
|
||||||
|
)
|
||||||
|
customElements.define('my-el-with-nonce', Foo)
|
||||||
|
container.innerHTML = `<my-el-with-nonce></my-el-with-nonce>`
|
||||||
|
const el = container.childNodes[0] as VueElement
|
||||||
|
const style = el.shadowRoot?.querySelector('style')!
|
||||||
|
expect(style.getAttribute('nonce')).toBe('xxx')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('async', () => {
|
describe('async', () => {
|
||||||
|
|
|
@ -48,6 +48,7 @@ export type VueElementConstructor<P = {}> = {
|
||||||
export interface CustomElementOptions {
|
export interface CustomElementOptions {
|
||||||
styles?: string[]
|
styles?: string[]
|
||||||
shadowRoot?: boolean
|
shadowRoot?: boolean
|
||||||
|
nonce?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// defineCustomElement provides the same type inference as defineComponent
|
// defineCustomElement provides the same type inference as defineComponent
|
||||||
|
@ -513,8 +514,10 @@ export class VueElement
|
||||||
}
|
}
|
||||||
this._styleChildren.add(owner)
|
this._styleChildren.add(owner)
|
||||||
}
|
}
|
||||||
|
const nonce = this._def.nonce
|
||||||
for (let i = styles.length - 1; i >= 0; i--) {
|
for (let i = styles.length - 1; i >= 0; i--) {
|
||||||
const s = document.createElement('style')
|
const s = document.createElement('style')
|
||||||
|
if (nonce) s.setAttribute('nonce', nonce)
|
||||||
s.textContent = styles[i]
|
s.textContent = styles[i]
|
||||||
this.shadowRoot!.prepend(s)
|
this.shadowRoot!.prepend(s)
|
||||||
// record for HMR
|
// record for HMR
|
||||||
|
|
Loading…
Reference in New Issue