|
7 | 7 | export default function updateAttribute({ attribute } = {}, attrs, tag) {
|
8 | 8 | const vueMetaAttrString = tag.getAttribute(attribute)
|
9 | 9 | const vueMetaAttrs = vueMetaAttrString ? vueMetaAttrString.split(',') : []
|
10 |
| - const toRemove = [].concat(vueMetaAttrs) |
| 10 | + const toRemove = Array.from(vueMetaAttrs) |
11 | 11 |
|
| 12 | + const keepIndexes = [] |
12 | 13 | for (const attr in attrs) {
|
13 | 14 | if (attrs.hasOwnProperty(attr)) {
|
14 |
| - const val = attrs[attr] || '' |
15 |
| - tag.setAttribute(attr, val) |
| 15 | + const value = attrs[attr] || '' |
| 16 | + tag.setAttribute(attr, value) |
16 | 17 |
|
17 | 18 | if (!vueMetaAttrs.includes(attr)) {
|
18 | 19 | vueMetaAttrs.push(attr)
|
19 | 20 | }
|
20 | 21 |
|
21 |
| - const keepIndex = toRemove.indexOf(attr) |
22 |
| - if (keepIndex !== -1) { |
23 |
| - toRemove.splice(keepIndex, 1) |
24 |
| - } |
| 22 | + // filter below wont ever check -1 |
| 23 | + keepIndexes.push(toRemove.indexOf(attr)) |
25 | 24 | }
|
26 | 25 | }
|
27 | 26 |
|
28 |
| - toRemove.forEach(attr => tag.removeAttribute(attr)) |
| 27 | + const removedAttributesCount = toRemove |
| 28 | + .filter((el, index) => !keepIndexes.includes(index)) |
| 29 | + .reduce((acc, attr) => { |
| 30 | + tag.removeAttribute(attr) |
| 31 | + return acc + 1 |
| 32 | + }, 0) |
29 | 33 |
|
30 |
| - if (vueMetaAttrs.length === toRemove.length) { |
| 34 | + if (vueMetaAttrs.length === removedAttributesCount) { |
31 | 35 | tag.removeAttribute(attribute)
|
32 | 36 | } else {
|
33 | 37 | tag.setAttribute(attribute, (vueMetaAttrs.sort()).join(','))
|
|
0 commit comments