]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-dom): patch xlink attribute (#842)
authorlikui <2218301630@qq.com>
Mon, 16 Mar 2020 22:46:01 +0000 (06:46 +0800)
committerGitHub <noreply@github.com>
Mon, 16 Mar 2020 22:46:01 +0000 (18:46 -0400)
packages/runtime-dom/__tests__/modules/attrs.spec.ts [new file with mode: 0644]
packages/runtime-dom/src/modules/attrs.ts

diff --git a/packages/runtime-dom/__tests__/modules/attrs.spec.ts b/packages/runtime-dom/__tests__/modules/attrs.spec.ts
new file mode 100644 (file)
index 0000000..91d5988
--- /dev/null
@@ -0,0 +1,27 @@
+import { patchAttr, xlinkNS } from '../../src/modules/attrs'
+
+describe('attrs', () => {
+  test('xlink attributes', () => {
+    const el = document.createElementNS('http://www.w3.org/2000/svg', 'use')
+    patchAttr(el, 'xlink:href', 'a', true)
+    expect(el.getAttributeNS(xlinkNS, 'href')).toBe('a')
+    patchAttr(el, 'xlink:href', null, true)
+    expect(el.getAttributeNS(xlinkNS, 'href')).toBe(null)
+  })
+
+  test('boolean attributes', () => {
+    const el = document.createElement('input')
+    patchAttr(el, 'readonly', true, false)
+    expect(el.getAttribute('readonly')).toBe('')
+    patchAttr(el, 'readonly', false, false)
+    expect(el.getAttribute('readonly')).toBe(null)
+  })
+
+  test('attributes', () => {
+    const el = document.createElement('div')
+    patchAttr(el, 'id', 'a', false)
+    expect(el.getAttribute('id')).toBe('a')
+    patchAttr(el, 'id', null, false)
+    expect(el.getAttribute('id')).toBe(null)
+  })
+})
index 2a6c5cc769a87956de63e3103f6dc8a4d31dcd5f..10fe06afb7a5a43232a38d237f43d0f98fe806f7 100644 (file)
@@ -1,6 +1,6 @@
 import { isSpecialBooleanAttr } from '@vue/shared'
 
-const xlinkNS = 'http://www.w3.org/1999/xlink'
+export const xlinkNS = 'http://www.w3.org/1999/xlink'
 
 export function patchAttr(
   el: Element,
@@ -10,7 +10,7 @@ export function patchAttr(
 ) {
   if (isSVG && key.indexOf('xlink:') === 0) {
     if (value == null) {
-      el.removeAttributeNS(xlinkNS, key)
+      el.removeAttributeNS(xlinkNS, key.slice(6, key.length))
     } else {
       el.setAttributeNS(xlinkNS, key, value)
     }