From 7484adc70dca7e0c1a073e4cea755c2607930f15 Mon Sep 17 00:00:00 2001 From: Sean Date: Fri, 8 Sep 2017 12:54:02 -0700 Subject: [PATCH 1/4] Add tests for expected behaviour --- test/getMetaInfo.spec.js | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/test/getMetaInfo.spec.js b/test/getMetaInfo.spec.js index 35572a8..54512b3 100644 --- a/test/getMetaInfo.spec.js +++ b/test/getMetaInfo.spec.js @@ -69,4 +69,64 @@ describe('getMetaInfo', () => { __dangerouslyDisableSanitizers: [] }) }) + + it('properly uses string titleTemplates', () => { + component = new Vue({ + metaInfo: { + title: 'Hello', + titleTemplate: '%s World', + meta: [ + { charset: 'utf-8' } + ] + } + }) + expect(getMetaInfo(component)).to.eql({ + title: 'Hello World', + titleChunk: 'Hello', + titleTemplate: '%s World', + htmlAttrs: {}, + headAttrs: {}, + bodyAttrs: {}, + meta: [ + { charset: 'utf-8' } + ], + base: [], + link: [], + style: [], + script: [], + noscript: [], + __dangerouslyDisableSanitizers: [] + }) + }) + + it('properly uses function titleTemplates', () => { + const titleTemplate = chunk => `${chunk} Function World` + + component = new Vue({ + metaInfo: { + title: 'Hello', + titleTemplate: titleTemplate, + meta: [ + { charset: 'utf-8' } + ] + } + }) + expect(getMetaInfo(component)).to.eql({ + title: 'Hello Function World', + titleChunk: 'Hello', + titleTemplate: titleTemplate, + htmlAttrs: {}, + headAttrs: {}, + bodyAttrs: {}, + meta: [ + { charset: 'utf-8' } + ], + base: [], + link: [], + style: [], + script: [], + noscript: [], + __dangerouslyDisableSanitizers: [] + }) + }) }) From c3cc4bdae4132770e6bf42b730e38508a694df3e Mon Sep 17 00:00:00 2001 From: Sean Date: Fri, 8 Sep 2017 12:54:09 -0700 Subject: [PATCH 2/4] Allow titleTemplate to be a function --- src/shared/getMetaInfo.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/shared/getMetaInfo.js b/src/shared/getMetaInfo.js index 284b703..db37a76 100644 --- a/src/shared/getMetaInfo.js +++ b/src/shared/getMetaInfo.js @@ -84,7 +84,11 @@ export default function _getMetaInfo (options = {}) { // replace title with populated template if (info.titleTemplate) { - info.title = info.titleTemplate.replace(/%s/g, info.titleChunk) + if (typeof info.titleTemplate === 'function') { + info.title = info.titleTemplate(info.titleChunk) + } else { + info.title = info.titleTemplate.replace(/%s/g, info.titleChunk) + } } // convert base tag to an array so it can be handled the same way From 47fb79e0ee539f82bbc51f4c72829d30fdc980f4 Mon Sep 17 00:00:00 2001 From: Sean Date: Fri, 8 Sep 2017 12:58:59 -0700 Subject: [PATCH 3/4] Add test to check `this` binding during function call --- test/getMetaInfo.spec.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/getMetaInfo.spec.js b/test/getMetaInfo.spec.js index 54512b3..c69a644 100644 --- a/test/getMetaInfo.spec.js +++ b/test/getMetaInfo.spec.js @@ -129,4 +129,42 @@ describe('getMetaInfo', () => { __dangerouslyDisableSanitizers: [] }) }) + + it('has the proper `this` binding when using function titleTemplates', () => { + const titleTemplate = function (chunk) { + return `${chunk} ${this.helloWorldText}` + } + + component = new Vue({ + metaInfo: { + title: 'Hello', + titleTemplate: titleTemplate, + meta: [ + { charset: 'utf-8' } + ] + }, + data () { + return { + helloWorldText: 'Function World' + } + } + }) + expect(getMetaInfo(component)).to.eql({ + title: 'Hello Function World', + titleChunk: 'Hello', + titleTemplate: titleTemplate, + htmlAttrs: {}, + headAttrs: {}, + bodyAttrs: {}, + meta: [ + { charset: 'utf-8' } + ], + base: [], + link: [], + style: [], + script: [], + noscript: [], + __dangerouslyDisableSanitizers: [] + }) + }) }) From 6283e1e3fd072c80c2409c6e9049d07ae373a0fe Mon Sep 17 00:00:00 2001 From: Sean Date: Fri, 8 Sep 2017 12:59:10 -0700 Subject: [PATCH 4/4] Bind titleTemplate function call to component instance --- src/shared/getMetaInfo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/getMetaInfo.js b/src/shared/getMetaInfo.js index db37a76..92613b0 100644 --- a/src/shared/getMetaInfo.js +++ b/src/shared/getMetaInfo.js @@ -85,7 +85,7 @@ export default function _getMetaInfo (options = {}) { // replace title with populated template if (info.titleTemplate) { if (typeof info.titleTemplate === 'function') { - info.title = info.titleTemplate(info.titleChunk) + info.title = info.titleTemplate.call(component, info.titleChunk) } else { info.title = info.titleTemplate.replace(/%s/g, info.titleChunk) }