From 70d8fe129be69932d855ab94c6bafeda585d8f07 Mon Sep 17 00:00:00 2001 From: Justin Bennett Date: Mon, 13 Feb 2017 20:07:03 -0500 Subject: [PATCH 1/4] Pass attributes to parseComponent --- packages/vue-template-compiler/build.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/vue-template-compiler/build.js b/packages/vue-template-compiler/build.js index 3c29a19e43b..222277f51cb 100644 --- a/packages/vue-template-compiler/build.js +++ b/packages/vue-template-compiler/build.js @@ -5822,6 +5822,16 @@ function parseComponent ( var depth = 0; var currentBlock = null; + function reduceAttrs(attrs) { + return attrs.reduce(function (cumulated, ref) { + var name = ref.name; + var value = ref.value; + + cumulated[name] = value; + return cumulated + }, Object.create(null)); + } + function start ( tag, attrs, @@ -5834,7 +5844,8 @@ function parseComponent ( currentBlock = { type: tag, content: '', - start: end + start: end, + attrs: reduceAttrs(attrs) }; checkAttrs(currentBlock, attrs); if (tag === 'style') { @@ -5847,13 +5858,7 @@ function parseComponent ( type: tag, content: '', start: end, - attrs: attrs.reduce(function (cumulated, ref) { - var name = ref.name; - var value = ref.value; - - cumulated[name] = value; - return cumulated - }, Object.create(null)) + attrs: reduceAttrs(attrs) }; sfc.customBlocks.push(currentBlock); } From 2bb96c7251d871cdf1832df3b8cfc70a0083b578 Mon Sep 17 00:00:00 2001 From: Justin Bennett Date: Mon, 13 Feb 2017 21:32:44 -0500 Subject: [PATCH 2/4] Edited source in the right place, added test --- src/sfc/parser.js | 23 +++++++++-------------- test/unit/modules/sfc/sfc-parser.spec.js | 4 +++- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/sfc/parser.js b/src/sfc/parser.js index 4f61616adbb..4ac29d2d1df 100644 --- a/src/sfc/parser.js +++ b/src/sfc/parser.js @@ -36,12 +36,16 @@ export function parseComponent ( end: number ) { if (depth === 0) { + currentBlock = { + type: tag, + content: '', + start: end, + attrs: attrs.reduce((cumulated, { name, value }) => { + cumulated[name] = value || true + return cumulated + }, Object.create(null)) + } if (isSpecialTag(tag)) { - currentBlock = { - type: tag, - content: '', - start: end - } checkAttrs(currentBlock, attrs) if (tag === 'style') { sfc.styles.push(currentBlock) @@ -49,15 +53,6 @@ export function parseComponent ( sfc[tag] = currentBlock } } else { // custom blocks - currentBlock = { - type: tag, - content: '', - start: end, - attrs: attrs.reduce((cumulated, { name, value }) => { - cumulated[name] = value - return cumulated - }, Object.create(null)) - } sfc.customBlocks.push(currentBlock) } } diff --git a/test/unit/modules/sfc/sfc-parser.spec.js b/test/unit/modules/sfc/sfc-parser.spec.js index 8e77bcfcc44..935632471a0 100644 --- a/test/unit/modules/sfc/sfc-parser.spec.js +++ b/test/unit/modules/sfc/sfc-parser.spec.js @@ -16,6 +16,7 @@ describe('Single File Component parser', () => { + @@ -24,12 +25,13 @@ describe('Single File Component parser', () => { `) expect(res.template.content.trim()).toBe('
hi
') - expect(res.styles.length).toBe(3) + expect(res.styles.length).toBe(4) expect(res.styles[0].src).toBe('./test.css') expect(res.styles[1].lang).toBe('stylus') expect(res.styles[1].scoped).toBe(true) expect(res.styles[1].content.trim()).toBe('h1\n color red\nh2\n color green') expect(res.styles[2].module).toBe(true) + expect(res.styles[3].attrs['custom-attr']).toBe(true) expect(res.script.content.trim()).toBe('export default {}') }) From e12ca6c113c677ecc0b75fb596a4ee4242ab05ea Mon Sep 17 00:00:00 2001 From: Justin Bennett Date: Mon, 13 Feb 2017 21:38:09 -0500 Subject: [PATCH 3/4] Back out changes made to the generated compiler build file --- packages/vue-template-compiler/build.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/packages/vue-template-compiler/build.js b/packages/vue-template-compiler/build.js index 222277f51cb..3c29a19e43b 100644 --- a/packages/vue-template-compiler/build.js +++ b/packages/vue-template-compiler/build.js @@ -5822,16 +5822,6 @@ function parseComponent ( var depth = 0; var currentBlock = null; - function reduceAttrs(attrs) { - return attrs.reduce(function (cumulated, ref) { - var name = ref.name; - var value = ref.value; - - cumulated[name] = value; - return cumulated - }, Object.create(null)); - } - function start ( tag, attrs, @@ -5844,8 +5834,7 @@ function parseComponent ( currentBlock = { type: tag, content: '', - start: end, - attrs: reduceAttrs(attrs) + start: end }; checkAttrs(currentBlock, attrs); if (tag === 'style') { @@ -5858,7 +5847,13 @@ function parseComponent ( type: tag, content: '', start: end, - attrs: reduceAttrs(attrs) + attrs: attrs.reduce(function (cumulated, ref) { + var name = ref.name; + var value = ref.value; + + cumulated[name] = value; + return cumulated + }, Object.create(null)) }; sfc.customBlocks.push(currentBlock); } From 986fe49cc0d297497bd3b41a069b931eae2c7301 Mon Sep 17 00:00:00 2001 From: Justin Bennett Date: Mon, 13 Feb 2017 22:32:01 -0500 Subject: [PATCH 4/4] Add a few more checks --- test/unit/modules/sfc/sfc-parser.spec.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/unit/modules/sfc/sfc-parser.spec.js b/test/unit/modules/sfc/sfc-parser.spec.js index 935632471a0..be3f518a584 100644 --- a/test/unit/modules/sfc/sfc-parser.spec.js +++ b/test/unit/modules/sfc/sfc-parser.spec.js @@ -16,7 +16,7 @@ describe('Single File Component parser', () => { - + @@ -31,7 +31,8 @@ describe('Single File Component parser', () => { expect(res.styles[1].scoped).toBe(true) expect(res.styles[1].content.trim()).toBe('h1\n color red\nh2\n color green') expect(res.styles[2].module).toBe(true) - expect(res.styles[3].attrs['custom-attr']).toBe(true) + expect(res.styles[3].attrs['bool-attr']).toBe(true) + expect(res.styles[3].attrs['val-attr']).toBe('test') expect(res.script.content.trim()).toBe('export default {}') })