From 22e456cbe27abb1430c2ca6ed980d10c1cef1280 Mon Sep 17 00:00:00 2001 From: pimlie Date: Fri, 8 Mar 2019 14:41:11 +0100 Subject: [PATCH] feat: child can indicate its content should be ignored (resolves: #204) --- src/shared/merge.js | 25 +++++++++++++++++-- test/getMetaInfo.test.js | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/shared/merge.js b/src/shared/merge.js index 966182e..fcbbe22 100644 --- a/src/shared/merge.js +++ b/src/shared/merge.js @@ -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, @@ -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 } @@ -29,7 +32,6 @@ export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, conte return } - const sourceItem = source[sourceIndex] const sourceTemplate = sourceItem[metaTemplateKeyName] if (!sourceTemplate) { @@ -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) }) diff --git a/test/getMetaInfo.test.js b/test/getMetaInfo.test.js index a6159fe..d6ab3ec 100644 --- a/test/getMetaInfo.test.js +++ b/test/getMetaInfo.test.js @@ -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: {} + }) + }) })