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

32 lines
695 B
TypeScript
Raw Normal View History

const xlinkNS = 'http://www.w3.org/1999/xlink'
function isXlink(name: string): boolean {
return name.charAt(5) === ':' && name.slice(0, 5) === 'xlink'
}
function getXlinkProp(name: string): string {
return isXlink(name) ? name.slice(6, name.length) : ''
}
2018-09-19 23:35:38 +08:00
export function patchAttr(
el: Element,
key: string,
value: any,
isSVG: boolean
) {
// isSVG short-circuits isXlink check
if (isSVG && isXlink(key)) {
if (value == null) {
el.removeAttributeNS(xlinkNS, getXlinkProp(key))
} else {
el.setAttributeNS(xlinkNS, key, value)
}
} else {
if (value == null) {
el.removeAttribute(key)
} else {
el.setAttribute(key, value)
}
}
}