Skip to content

Commit 01edc8c

Browse files
pimlieTheAlexLichter
authored andcommitted
feat: attr keys can have array values (resolves #231)
1 parent 6e18a7d commit 01edc8c

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

src/client/updaters/attribute.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import isArray from '../../shared/isArray'
2+
13
/**
24
* Updates the document's html tag attributes
35
*
@@ -12,8 +14,9 @@ export default function updateAttribute({ attribute } = {}, attrs, tag) {
1214
const keepIndexes = []
1315
for (const attr in attrs) {
1416
if (attrs.hasOwnProperty(attr)) {
15-
const value = attrs[attr] || ''
16-
tag.setAttribute(attr, value)
17+
const value = isArray(attrs[attr]) ? attrs[attr].join(' ') : attrs[attr]
18+
19+
tag.setAttribute(attr, value || '')
1720

1821
if (!vueMetaAttrs.includes(attr)) {
1922
vueMetaAttrs.push(attr)

src/server/generators/attribute.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { booleanHtmlAttributes } from '../../shared/constants'
22
import { isUndefined } from '../../shared/typeof'
3+
import isArray from '../../shared/isArray'
34

45
/**
56
* Generates tag attributes for use on the server.
@@ -20,7 +21,7 @@ export default function attributeGenerator({ attribute } = {}, type, data) {
2021

2122
attributeStr += isUndefined(data[attr]) || booleanHtmlAttributes.includes(attr)
2223
? attr
23-
: `${attr}="${data[attr]}"`
24+
: `${attr}="${isArray(data[attr]) ? data[attr].join(' ') : data[attr]}"`
2425

2526
attributeStr += ' '
2627
}

src/shared/escape.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ export default function escape(info, { tagIDKeyName }, escapeSequences = []) {
3535
if (isString(value)) {
3636
escaped[key] = escapeSequences.reduce((val, [v, r]) => val.replace(v, r), value)
3737
} else if (isArray(value)) {
38-
escaped[key] = value.map(v => escape(v, { tagIDKeyName }, escapeSequences))
38+
escaped[key] = value.map((v) => {
39+
return isObject(v)
40+
? escape(v, { tagIDKeyName }, escapeSequences)
41+
: escapeSequences.reduce((val, [v, r]) => val.replace(v, r), v)
42+
})
3943
} else if (isObject(value)) {
4044
escaped[key] = escape(value, { tagIDKeyName }, escapeSequences)
4145
} else {

test/getMetaInfo.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,4 +719,44 @@ describe('getMetaInfo', () => {
719719
__dangerouslyDisableSanitizersByTagID: {}
720720
})
721721
})
722+
723+
test('attribute values can be an array', () => {
724+
Vue.component('merge-child', {
725+
render: h => h('div'),
726+
metaInfo: {
727+
bodyAttrs: {
728+
class: ['foo']
729+
}
730+
}
731+
})
732+
733+
const component = new Vue({
734+
metaInfo: {
735+
bodyAttrs: {
736+
class: ['bar']
737+
}
738+
},
739+
el: document.createElement('div'),
740+
render: h => h('div', null, [h('merge-child')])
741+
})
742+
743+
expect(getMetaInfo(component)).toEqual({
744+
title: '',
745+
titleChunk: '',
746+
titleTemplate: '%s',
747+
htmlAttrs: {},
748+
headAttrs: {},
749+
bodyAttrs: {
750+
class: ['bar', 'foo']
751+
},
752+
meta: [],
753+
base: [],
754+
link: [],
755+
style: [],
756+
script: [],
757+
noscript: [],
758+
__dangerouslyDisableSanitizers: [],
759+
__dangerouslyDisableSanitizersByTagID: {}
760+
})
761+
})
722762
})

0 commit comments

Comments
 (0)