Skip to content

Commit

Permalink
fix(runtime-dom): patch xlink attribute (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin authored Mar 16, 2020
1 parent 3d38e6f commit d318576
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 27 additions & 0 deletions packages/runtime-dom/__tests__/modules/attrs.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
4 changes: 2 additions & 2 deletions packages/runtime-dom/src/modules/attrs.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
}
Expand Down

0 comments on commit d318576

Please sign in to comment.