Prechádzať zdrojové kódy

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.
zhoulixiang 2 rokov pred
rodič
commit
bfc1838f31

+ 9 - 0
packages/runtime-dom/__tests__/patchProps.spec.ts

@@ -291,6 +291,15 @@ describe('runtime-dom: props patching', () => {
     expect(el.value).toBe('baz')
   })
 
+  // #8780
+  test('embedded tag with width and height', () => {
+    // Width and height of some embedded element such as img、video、source、canvas
+    // must be set as attribute
+    const el = document.createElement('img')
+    patchProp(el, 'width', null, '24px')
+    expect(el.getAttribute('width')).toBe('24px')
+  })
+
   test('translate attribute', () => {
     const el = document.createElement('div')
     patchProp(el, 'translate', null, 'no')

+ 10 - 0
packages/runtime-dom/src/patchProp.ts

@@ -8,6 +8,8 @@ import { RendererOptions } from '@vue/runtime-core'
 
 const nativeOnRE = /^on[a-z]/
 
+const embeddedTags = ['IMG', 'VIDEO', 'CANVAS', 'SOURCE']
+
 type DOMRendererOptions = RendererOptions<Node, Element>
 
 export const patchProp: DOMRendererOptions['patchProp'] = (
@@ -105,6 +107,14 @@ function shouldSetAsProp(
     return false
   }
 
+  // #8780 the width or heigth of embedded tags must be set as attribute
+  if (
+    (key === 'width' || key === 'height') &&
+    embeddedTags.includes(el.tagName)
+  ) {
+    return false
+  }
+
   // native onclick with string value, must be set as attribute
   if (nativeOnRE.test(key) && isString(value)) {
     return false