Skip to content

Commit 97d36bc

Browse files
zephraphyyx990803
authored andcommitted
Pass attributes to parseComponent (addresses vuejs#4914) (vuejs#4925)
* Pass attributes to parseComponent * Edited source in the right place, added test * Back out changes made to the generated compiler build file * Add a few more checks
1 parent 01151ce commit 97d36bc

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

src/sfc/parser.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,23 @@ export function parseComponent (
3636
end: number
3737
) {
3838
if (depth === 0) {
39+
currentBlock = {
40+
type: tag,
41+
content: '',
42+
start: end,
43+
attrs: attrs.reduce((cumulated, { name, value }) => {
44+
cumulated[name] = value || true
45+
return cumulated
46+
}, Object.create(null))
47+
}
3948
if (isSpecialTag(tag)) {
40-
currentBlock = {
41-
type: tag,
42-
content: '',
43-
start: end
44-
}
4549
checkAttrs(currentBlock, attrs)
4650
if (tag === 'style') {
4751
sfc.styles.push(currentBlock)
4852
} else {
4953
sfc[tag] = currentBlock
5054
}
5155
} else { // custom blocks
52-
currentBlock = {
53-
type: tag,
54-
content: '',
55-
start: end,
56-
attrs: attrs.reduce((cumulated, { name, value }) => {
57-
cumulated[name] = value
58-
return cumulated
59-
}, Object.create(null))
60-
}
6156
sfc.customBlocks.push(currentBlock)
6257
}
6358
}

test/unit/modules/sfc/sfc-parser.spec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('Single File Component parser', () => {
1616
<style module>
1717
h1 { font-weight: bold }
1818
</style>
19+
<style bool-attr val-attr="test"></style>
1920
<script>
2021
export default {}
2122
</script>
@@ -24,12 +25,14 @@ describe('Single File Component parser', () => {
2425
</div>
2526
`)
2627
expect(res.template.content.trim()).toBe('<div>hi</div>')
27-
expect(res.styles.length).toBe(3)
28+
expect(res.styles.length).toBe(4)
2829
expect(res.styles[0].src).toBe('./test.css')
2930
expect(res.styles[1].lang).toBe('stylus')
3031
expect(res.styles[1].scoped).toBe(true)
3132
expect(res.styles[1].content.trim()).toBe('h1\n color red\nh2\n color green')
3233
expect(res.styles[2].module).toBe(true)
34+
expect(res.styles[3].attrs['bool-attr']).toBe(true)
35+
expect(res.styles[3].attrs['val-attr']).toBe('test')
3336
expect(res.script.content.trim()).toBe('export default {}')
3437
})
3538

0 commit comments

Comments
 (0)