Просмотр исходного кода

fix: ensure element attribute names are always hyphenated

Thorsten Luenborg 4 лет назад
Родитель
Сommit
a23438ca7b

+ 8 - 0
packages/runtime-dom/__tests__/patchAttrs.spec.ts

@@ -53,4 +53,12 @@ describe('runtime-dom: attrs patching', () => {
     patchProp(el, 'onwards', 'a', null)
     expect(el.getAttribute('onwards')).toBe(null)
   })
+
+  test('camelCase attribute name properly hyphenated', () => {
+    const el = document.createElement('div')
+    patchProp(el, 'customAttribute', null, 'a')
+    expect(el.getAttribute('custom-attribute')).toBe('a')
+    patchProp(el, 'custom-attribute', 'a', null)
+    expect(el.getAttribute('custom-attribute')).toBe(null)
+  })
 })

+ 2 - 1
packages/runtime-dom/src/modules/attrs.ts

@@ -1,4 +1,5 @@
 import {
+  hyphenate,
   includeBooleanAttr,
   isSpecialBooleanAttr,
   makeMap,
@@ -36,7 +37,7 @@ export function patchAttr(
     if (value == null || (isBoolean && !includeBooleanAttr(value))) {
       el.removeAttribute(key)
     } else {
-      el.setAttribute(key, isBoolean ? '' : value)
+      el.setAttribute(hyphenate(key), isBoolean ? '' : value)
     }
   }
 }