Skip to content

Commit 22e456c

Browse files
pimlieTheAlexLichter
authored andcommitted
feat: child can indicate its content should be ignored (resolves: #204)
1 parent 4dafffe commit 22e456c

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/shared/merge.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import deepmerge from 'deepmerge'
22
import applyTemplate from './applyTemplate'
3+
import { metaInfoAttributeKeys } from './constants'
34

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

1718
const sourceIndex = source.findIndex(item => item[tagIDKeyName] === targetItem[tagIDKeyName])
19+
const sourceItem = source[sourceIndex]
1820

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

32-
const sourceItem = source[sourceIndex]
3335
const sourceTemplate = sourceItem[metaTemplateKeyName]
3436

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

4749
export function merge(target, source, options = {}) {
50+
// remove properties explicitly set to false so child components can
51+
// optionally _not_ overwrite the parents content
52+
// (for array properties this is checked in arrayMerge)
53+
if (source.title === false) {
54+
delete source.title
55+
}
56+
57+
for (const attrKey in metaInfoAttributeKeys) {
58+
if (!source[attrKey]) {
59+
continue
60+
}
61+
62+
for (const key in source[attrKey]) {
63+
if (source[attrKey][key] === false) {
64+
delete source[attrKey][key]
65+
}
66+
}
67+
}
68+
4869
return deepmerge(target, source, {
4970
arrayMerge: (t, s) => arrayMerge(options, t, s)
5071
})

test/getMetaInfo.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,58 @@ describe('getMetaInfo', () => {
617617
__dangerouslyDisableSanitizersByTagID: {}
618618
})
619619
})
620+
621+
test('child can indicate its content should be ignored', () => {
622+
Vue.component('merge-child', {
623+
render: h => h('div'),
624+
metaInfo: {
625+
title: false,
626+
meta: [
627+
{
628+
vmid: 'og:title',
629+
content: false
630+
}
631+
]
632+
}
633+
})
634+
635+
const component = new Vue({
636+
metaInfo: {
637+
title: 'Hello',
638+
meta: [
639+
{
640+
vmid: 'og:title',
641+
property: 'og:title',
642+
content: 'Test title',
643+
template: chunk => `${chunk} - My page`
644+
}
645+
]
646+
},
647+
el: document.createElement('div'),
648+
render: h => h('div', null, [h('merge-child')])
649+
})
650+
651+
expect(getMetaInfo(component)).toEqual({
652+
title: 'Hello',
653+
titleChunk: 'Hello',
654+
titleTemplate: '%s',
655+
htmlAttrs: {},
656+
headAttrs: {},
657+
bodyAttrs: {},
658+
meta: [
659+
{
660+
vmid: 'og:title',
661+
property: 'og:title',
662+
content: 'Test title - My page'
663+
}
664+
],
665+
base: [],
666+
link: [],
667+
style: [],
668+
script: [],
669+
noscript: [],
670+
__dangerouslyDisableSanitizers: [],
671+
__dangerouslyDisableSanitizersByTagID: {}
672+
})
673+
})
620674
})

0 commit comments

Comments
 (0)