File tree 4 files changed +64
-5
lines changed
__testfixtures__/slot-attribute
4 files changed +64
-5
lines changed Original file line number Diff line number Diff line change @@ -23,3 +23,11 @@ runTest(
23
23
'vue' ,
24
24
'vue'
25
25
)
26
+
27
+ runTest (
28
+ 'The v-slot attribute already exists in the upper template' ,
29
+ 'slot-attribute' ,
30
+ 'template-with-slot' ,
31
+ 'vue' ,
32
+ 'vue'
33
+ )
Original file line number Diff line number Diff line change
1
+ <template >
2
+ <div class =" img-list" ref =" box" >
3
+ <template slot="title">
4
+ <div >
5
+ <span slot =" title" >title name</span >
6
+ </div >
7
+ </template >
8
+ </div >
9
+ </template >
Original file line number Diff line number Diff line change
1
+ <template >
2
+ <div class =" img-list" ref =" box" >
3
+ <template v-slot :title >
4
+ <div >
5
+ <span >title name</span >
6
+ </div >
7
+ </template >
8
+ </div >
9
+ </template >
Original file line number Diff line number Diff line change @@ -60,11 +60,44 @@ function fix(node: Node): Operation[] {
60
60
// remove v-slot:${slotValue}
61
61
fixOperations . push ( OperationUtils . remove ( node ) )
62
62
// add <template v-slot:${slotValue}>
63
- fixOperations . push (
64
- OperationUtils . insertTextBefore ( element , `<template v-slot:${ slotValue } >` )
65
- )
66
- // add </template>
67
- fixOperations . push ( OperationUtils . insertTextAfter ( element , `</template>` ) )
63
+
64
+ let elder : any = null
65
+ let hasSlotAttr : boolean = false
66
+ let tmp : any = element
67
+ // find template parent
68
+ while ( elder == null && tmp != null ) {
69
+ hasSlotAttr = false
70
+ tmp = tmp . parent
71
+ if ( tmp == null || tmp . type != 'VElement' || tmp . name != 'template' ) {
72
+ continue
73
+ }
74
+
75
+ elder = element
76
+ tmp . startTag . attributes
77
+ . filter (
78
+ ( attr : any ) =>
79
+ attr . type === 'VAttribute' &&
80
+ attr . key . type === 'VIdentifier' &&
81
+ attr . key . name === 'slot'
82
+ )
83
+ . forEach ( ( element : any ) => {
84
+ hasSlotAttr = true
85
+ } )
86
+ if ( hasSlotAttr ) {
87
+ break
88
+ }
89
+ }
90
+
91
+ if ( ! hasSlotAttr ) {
92
+ fixOperations . push (
93
+ OperationUtils . insertTextBefore (
94
+ element ,
95
+ `<template v-slot:${ slotValue } >`
96
+ )
97
+ )
98
+ // add </template>
99
+ fixOperations . push ( OperationUtils . insertTextAfter ( element , `</template>` ) )
100
+ }
68
101
}
69
102
70
103
return fixOperations
You can’t perform that action at this time.
0 commit comments