-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathupdateTagAttributes.js
35 lines (34 loc) · 1.1 KB
/
updateTagAttributes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { VUE_META_ATTRIBUTE } from '../../shared/constants'
/**
* updates the document's html tag attributes
*
* @param {Object} attrs - the new document html attributes
* @param {HTMLElement} tag - the HTMLElment tag to update with new attrs
*/
export default function updateTagAttributes (attrs, tag) {
const vueMetaAttrString = tag.getAttribute(VUE_META_ATTRIBUTE)
const vueMetaAttrs = vueMetaAttrString ? vueMetaAttrString.split(',') : []
const toRemove = [].concat(vueMetaAttrs)
for (let attr in attrs) {
if (attrs.hasOwnProperty(attr)) {
const val = attrs[attr] || ''
tag.setAttribute(attr, val)
if (vueMetaAttrs.indexOf(attr) === -1) {
vueMetaAttrs.push(attr)
}
const saveIndex = toRemove.indexOf(attr)
if (saveIndex !== -1) {
toRemove.splice(saveIndex, 1)
}
}
}
let i = toRemove.length - 1
for (; i >= 0; i--) {
tag.removeAttribute(toRemove[i])
}
if (vueMetaAttrs.length === toRemove.length) {
tag.removeAttribute(VUE_META_ATTRIBUTE)
} else {
tag.setAttribute(VUE_META_ATTRIBUTE, vueMetaAttrs.join(','))
}
}