File tree Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -75,4 +75,37 @@ describe('runtime-dom: props patching', () => {
75
75
expect ( root . innerHTML ) . toBe ( `<div>bar</div>` )
76
76
expect ( fn ) . toHaveBeenCalled ( )
77
77
} )
78
+
79
+ // #1049
80
+ test ( 'set domProps where string is not accepted' , ( ) => {
81
+ const realCreateElement = document . createElement . bind ( document )
82
+ const spyCreateElement = jest
83
+ . spyOn ( document , 'createElement' )
84
+ . mockImplementation ( tagName => {
85
+ const el = realCreateElement ( tagName )
86
+ let srcObject : any = undefined
87
+ Object . defineProperty ( el , 'srcObject' , {
88
+ enumerable : true ,
89
+ set ( v ) {
90
+ if ( typeof v === 'string' ) {
91
+ throw new TypeError (
92
+ `Failed to set the 'srcObject' property on 'HTMLMediaElement'`
93
+ )
94
+ }
95
+ srcObject = v
96
+ } ,
97
+ get ( ) {
98
+ return srcObject
99
+ }
100
+ } )
101
+ return el
102
+ } )
103
+
104
+ const el = document . createElement ( 'video' )
105
+
106
+ patchProp ( el , 'srcObject' , undefined , null )
107
+
108
+ expect ( el . srcObject ) . toBeNull ( )
109
+ spyCreateElement . mockRestore ( )
110
+ } )
78
111
} )
Original file line number Diff line number Diff line change @@ -31,7 +31,21 @@ export function patchDOMProp(
31
31
if ( value === '' && typeof el [ key ] === 'boolean' ) {
32
32
// e.g. <select multiple> compiles to { multiple: '' }
33
33
el [ key ] = true
34
- } else {
34
+ } else if ( isStringAttribute ( el . tagName , key ) ) {
35
35
el [ key ] = value == null ? '' : value
36
+ } else {
37
+ el [ key ] = value
38
+ }
39
+ }
40
+
41
+ function isStringAttribute ( tagName : any , key : string ) {
42
+ try {
43
+ // some dom properties accept '' but not other strings, e.g. <video>.volume
44
+ document . createElement ( tagName ) [ key ] = 'test string'
45
+ return true
46
+ } catch ( e ) {
47
+ if ( e . name !== 'TypeError' ) throw e
48
+
49
+ return false
36
50
}
37
51
}
You can’t perform that action at this time.
0 commit comments