Skip to content

Commit

Permalink
feat: attr keys can have array values (resolves #231)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie authored and manniL committed Mar 12, 2019
1 parent 6e18a7d commit 01edc8c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/client/updaters/attribute.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import isArray from '../../shared/isArray'

/**
* Updates the document's html tag attributes
*
Expand All @@ -12,8 +14,9 @@ export default function updateAttribute({ attribute } = {}, attrs, tag) {
const keepIndexes = []
for (const attr in attrs) {
if (attrs.hasOwnProperty(attr)) {
const value = attrs[attr] || ''
tag.setAttribute(attr, value)
const value = isArray(attrs[attr]) ? attrs[attr].join(' ') : attrs[attr]

tag.setAttribute(attr, value || '')

if (!vueMetaAttrs.includes(attr)) {
vueMetaAttrs.push(attr)
Expand Down
3 changes: 2 additions & 1 deletion src/server/generators/attribute.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { booleanHtmlAttributes } from '../../shared/constants'
import { isUndefined } from '../../shared/typeof'
import isArray from '../../shared/isArray'

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

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

attributeStr += ' '
}
Expand Down
6 changes: 5 additions & 1 deletion src/shared/escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export default function escape(info, { tagIDKeyName }, escapeSequences = []) {
if (isString(value)) {
escaped[key] = escapeSequences.reduce((val, [v, r]) => val.replace(v, r), value)
} else if (isArray(value)) {
escaped[key] = value.map(v => escape(v, { tagIDKeyName }, escapeSequences))
escaped[key] = value.map((v) => {
return isObject(v)
? escape(v, { tagIDKeyName }, escapeSequences)
: escapeSequences.reduce((val, [v, r]) => val.replace(v, r), v)
})
} else if (isObject(value)) {
escaped[key] = escape(value, { tagIDKeyName }, escapeSequences)
} else {
Expand Down
40 changes: 40 additions & 0 deletions test/getMetaInfo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,4 +719,44 @@ describe('getMetaInfo', () => {
__dangerouslyDisableSanitizersByTagID: {}
})
})

test('attribute values can be an array', () => {
Vue.component('merge-child', {
render: h => h('div'),
metaInfo: {
bodyAttrs: {
class: ['foo']
}
}
})

const component = new Vue({
metaInfo: {
bodyAttrs: {
class: ['bar']
}
},
el: document.createElement('div'),
render: h => h('div', null, [h('merge-child')])
})

expect(getMetaInfo(component)).toEqual({
title: '',
titleChunk: '',
titleTemplate: '%s',
htmlAttrs: {},
headAttrs: {},
bodyAttrs: {
class: ['bar', 'foo']
},
meta: [],
base: [],
link: [],
style: [],
script: [],
noscript: [],
__dangerouslyDisableSanitizers: [],
__dangerouslyDisableSanitizersByTagID: {}
})
})
})

0 comments on commit 01edc8c

Please sign in to comment.