2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-10 22:42:24 +03:00

feat: attr keys can have array values (resolves #231)

This commit is contained in:
pimlie
2019-03-08 16:40:43 +01:00
committed by Alexander Lichter
parent 6e18a7d54a
commit 01edc8c242
4 changed files with 52 additions and 4 deletions
+5 -2
View File
@@ -1,3 +1,5 @@
import isArray from '../../shared/isArray'
/**
* Updates the document's html tag attributes
*
@@ -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)
+2 -1
View File
@@ -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.
@@ -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 += ' '
}
+5 -1
View File
@@ -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 {
+40
View File
@@ -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: {}
})
})
})