Skip to content

Commit

Permalink
feat: child can indicate its content should be ignored (resolves: #204)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie authored and manniL committed Mar 12, 2019
1 parent 4dafffe commit 22e456c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/shared/merge.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import deepmerge from 'deepmerge'
import applyTemplate from './applyTemplate'
import { metaInfoAttributeKeys } from './constants'

export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, contentKeyName }, target, source) {
// we concat the arrays without merging objects contained in,
Expand All @@ -15,9 +16,11 @@ export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, conte
}

const sourceIndex = source.findIndex(item => item[tagIDKeyName] === targetItem[tagIDKeyName])
const sourceItem = source[sourceIndex]

// source doesnt contain any duplicate id's
if (sourceIndex === -1) {
// or the source item should be ignored
if (sourceIndex === -1 || sourceItem[contentKeyName] === false || sourceItem.innerHTML === false) {
destination.push(targetItem)
return
}
Expand All @@ -29,7 +32,6 @@ export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, conte
return
}

const sourceItem = source[sourceIndex]
const sourceTemplate = sourceItem[metaTemplateKeyName]

if (!sourceTemplate) {
Expand All @@ -45,6 +47,25 @@ export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, conte
}

export function merge(target, source, options = {}) {
// remove properties explicitly set to false so child components can
// optionally _not_ overwrite the parents content
// (for array properties this is checked in arrayMerge)
if (source.title === false) {
delete source.title
}

for (const attrKey in metaInfoAttributeKeys) {
if (!source[attrKey]) {
continue
}

for (const key in source[attrKey]) {
if (source[attrKey][key] === false) {
delete source[attrKey][key]
}
}
}

return deepmerge(target, source, {
arrayMerge: (t, s) => arrayMerge(options, t, s)
})
Expand Down
54 changes: 54 additions & 0 deletions test/getMetaInfo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,4 +617,58 @@ describe('getMetaInfo', () => {
__dangerouslyDisableSanitizersByTagID: {}
})
})

test('child can indicate its content should be ignored', () => {
Vue.component('merge-child', {
render: h => h('div'),
metaInfo: {
title: false,
meta: [
{
vmid: 'og:title',
content: false
}
]
}
})

const component = new Vue({
metaInfo: {
title: 'Hello',
meta: [
{
vmid: 'og:title',
property: 'og:title',
content: 'Test title',
template: chunk => `${chunk} - My page`
}
]
},
el: document.createElement('div'),
render: h => h('div', null, [h('merge-child')])
})

expect(getMetaInfo(component)).toEqual({
title: 'Hello',
titleChunk: 'Hello',
titleTemplate: '%s',
htmlAttrs: {},
headAttrs: {},
bodyAttrs: {},
meta: [
{
vmid: 'og:title',
property: 'og:title',
content: 'Test title - My page'
}
],
base: [],
link: [],
style: [],
script: [],
noscript: [],
__dangerouslyDisableSanitizers: [],
__dangerouslyDisableSanitizersByTagID: {}
})
})
})

0 comments on commit 22e456c

Please sign in to comment.