Skip to content

Commit d318576

Browse files
authored
fix(runtime-dom): patch xlink attribute (#842)
1 parent 3d38e6f commit d318576

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { patchAttr, xlinkNS } from '../../src/modules/attrs'
2+
3+
describe('attrs', () => {
4+
test('xlink attributes', () => {
5+
const el = document.createElementNS('http://www.w3.org/2000/svg', 'use')
6+
patchAttr(el, 'xlink:href', 'a', true)
7+
expect(el.getAttributeNS(xlinkNS, 'href')).toBe('a')
8+
patchAttr(el, 'xlink:href', null, true)
9+
expect(el.getAttributeNS(xlinkNS, 'href')).toBe(null)
10+
})
11+
12+
test('boolean attributes', () => {
13+
const el = document.createElement('input')
14+
patchAttr(el, 'readonly', true, false)
15+
expect(el.getAttribute('readonly')).toBe('')
16+
patchAttr(el, 'readonly', false, false)
17+
expect(el.getAttribute('readonly')).toBe(null)
18+
})
19+
20+
test('attributes', () => {
21+
const el = document.createElement('div')
22+
patchAttr(el, 'id', 'a', false)
23+
expect(el.getAttribute('id')).toBe('a')
24+
patchAttr(el, 'id', null, false)
25+
expect(el.getAttribute('id')).toBe(null)
26+
})
27+
})

packages/runtime-dom/src/modules/attrs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isSpecialBooleanAttr } from '@vue/shared'
22

3-
const xlinkNS = 'http://www.w3.org/1999/xlink'
3+
export const xlinkNS = 'http://www.w3.org/1999/xlink'
44

55
export function patchAttr(
66
el: Element,
@@ -10,7 +10,7 @@ export function patchAttr(
1010
) {
1111
if (isSVG && key.indexOf('xlink:') === 0) {
1212
if (value == null) {
13-
el.removeAttributeNS(xlinkNS, key)
13+
el.removeAttributeNS(xlinkNS, key.slice(6, key.length))
1414
} else {
1515
el.setAttributeNS(xlinkNS, key, value)
1616
}

0 commit comments

Comments
 (0)