Skip to content

Commit bfc1838

Browse files
authored
fix(runtime-dom): set width/height with units as attribute (#8781)
Technically, width / height on `<img>`, `<video>` etc must be integers and cannot contain units. When set as a DOM property, the DOM force converts strings with units to 0. However, this is such a common mistake that most browsers nowadays supports such usage, and it makes sense for Vue to at least let it be set as an attribute.
1 parent 7411143 commit bfc1838

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

packages/runtime-dom/__tests__/patchProps.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ describe('runtime-dom: props patching', () => {
291291
expect(el.value).toBe('baz')
292292
})
293293

294+
// #8780
295+
test('embedded tag with width and height', () => {
296+
// Width and height of some embedded element such as img、video、source、canvas
297+
// must be set as attribute
298+
const el = document.createElement('img')
299+
patchProp(el, 'width', null, '24px')
300+
expect(el.getAttribute('width')).toBe('24px')
301+
})
302+
294303
test('translate attribute', () => {
295304
const el = document.createElement('div')
296305
patchProp(el, 'translate', null, 'no')

packages/runtime-dom/src/patchProp.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { RendererOptions } from '@vue/runtime-core'
88

99
const nativeOnRE = /^on[a-z]/
1010

11+
const embeddedTags = ['IMG', 'VIDEO', 'CANVAS', 'SOURCE']
12+
1113
type DOMRendererOptions = RendererOptions<Node, Element>
1214

1315
export const patchProp: DOMRendererOptions['patchProp'] = (
@@ -105,6 +107,14 @@ function shouldSetAsProp(
105107
return false
106108
}
107109

110+
// #8780 the width or heigth of embedded tags must be set as attribute
111+
if (
112+
(key === 'width' || key === 'height') &&
113+
embeddedTags.includes(el.tagName)
114+
) {
115+
return false
116+
}
117+
108118
// native onclick with string value, must be set as attribute
109119
if (nativeOnRE.test(key) && isString(value)) {
110120
return false

0 commit comments

Comments
 (0)