Skip to content

Commit c949c74

Browse files
committed
change ref to a built-in attribute
1 parent e3fb6fe commit c949c74

File tree

8 files changed

+56
-62
lines changed

8 files changed

+56
-62
lines changed

src/compiler/codegen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function genData (el: ASTElement): string | void {
126126
}
127127
// ref
128128
if (el.ref) {
129-
data += `ref:"${el.ref}",`
129+
data += `ref:${el.ref},`
130130
}
131131
if (el.refInFor) {
132132
data += `refInFor:true,`

src/compiler/directives/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import ref from './ref'
21
import bind from './bind'
32
import { noop } from 'shared/util'
43

54
export default {
6-
ref,
75
bind,
86
cloak: noop
97
}

src/compiler/directives/ref.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/compiler/parser/index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,16 @@ export function parse (
116116
if (inPre) {
117117
processRawAttrs(element)
118118
} else {
119-
processKey(element)
120119
processFor(element)
121120
processIf(element)
122121
processOnce(element)
122+
123123
// determine whether this is a plain element after
124-
// removing if/for/once attributes
124+
// removing structural attributes
125125
element.plain = !element.key && !attrs.length
126+
127+
processKey(element)
128+
processRef(element)
126129
processSlot(element)
127130
processComponent(element)
128131
for (let i = 0; i < transforms.length; i++) {
@@ -252,6 +255,21 @@ function processKey (el) {
252255
}
253256
}
254257

258+
function processRef (el) {
259+
const ref = getBindingAttr(el, 'ref')
260+
if (ref) {
261+
el.ref = ref
262+
let parent = el
263+
while (parent) {
264+
if (parent.for !== undefined) {
265+
el.refInFor = true
266+
break
267+
}
268+
parent = parent.parent
269+
}
270+
}
271+
}
272+
255273
function processFor (el) {
256274
let exp
257275
if ((exp = getAndRemoveAttr(el, 'v-for'))) {

test/unit/features/options/props.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('Options props', () => {
66
data: {
77
b: 'bar'
88
},
9-
template: '<test v-bind:b="b" v-ref:child></test>',
9+
template: '<test v-bind:b="b" ref="child"></test>',
1010
components: {
1111
test: {
1212
props: ['b'],
@@ -30,7 +30,7 @@ describe('Options props', () => {
3030
data: {
3131
b: 'bar'
3232
},
33-
template: '<test v-bind:b="b" v-ref:child></test>',
33+
template: '<test v-bind:b="b" ref="child"></test>',
3434
components: {
3535
test: {
3636
props: { b: String },
@@ -266,7 +266,7 @@ describe('Options props', () => {
266266

267267
it('treat boolean props properly', () => {
268268
const vm = new Vue({
269-
template: '<comp v-ref:child prop-a prop-b="prop-b"></comp>',
269+
template: '<comp ref="child" prop-a prop-b="prop-b"></comp>',
270270
components: {
271271
comp: {
272272
template: '<div></div>',

test/unit/features/directives/ref.spec.js renamed to test/unit/features/ref.spec.js

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vue from 'vue'
22

3-
describe('Directive v-ref', () => {
3+
describe('ref', () => {
44
const components = {
55
test: {
66
id: 'test'
@@ -10,19 +10,22 @@ describe('Directive v-ref', () => {
1010
}
1111
}
1212

13-
it('should accept hyphenated refs', () => {
13+
it('should work', () => {
1414
const vm = new Vue({
15+
data: {
16+
value: 'bar'
17+
},
1518
template: `<div>
16-
<test v-ref:test></test>
17-
<test2 v-ref:test-hyphen></test2>
19+
<test ref="foo"></test>
20+
<test2 :ref="value"></test2>
1821
</div>`,
1922
components
2023
})
2124
vm.$mount()
22-
expect(vm.$refs.test).toBeTruthy()
23-
expect(vm.$refs.test.$options.id).toBe('test')
24-
expect(vm.$refs['test-hyphen']).toBeTruthy()
25-
expect(vm.$refs['test-hyphen'].$options.id).toBe('test2')
25+
expect(vm.$refs.foo).toBeTruthy()
26+
expect(vm.$refs.foo.$options.id).toBe('test')
27+
expect(vm.$refs.bar).toBeTruthy()
28+
expect(vm.$refs.bar.$options.id).toBe('test2')
2629
})
2730

2831
it('should work as a hyperscript prop', () => {
@@ -39,25 +42,9 @@ describe('Directive v-ref', () => {
3942
expect(vm.$refs.test.$options.id).toBe('test')
4043
})
4144

42-
it('should accept camelCase refs', () => {
43-
const vm = new Vue({
44-
template:
45-
`<div>
46-
<test v-ref:test></test>
47-
<test2 v-ref:testCase></test2>
48-
</div>`,
49-
components
50-
})
51-
vm.$mount()
52-
expect(vm.$refs.test).toBeTruthy()
53-
expect(vm.$refs.test.$options.id).toBe('test')
54-
expect(vm.$refs.testCase).toBeTruthy()
55-
expect(vm.$refs.testCase.$options.id).toBe('test2')
56-
})
57-
5845
it('should accept HOC component', () => {
5946
const vm = new Vue({
60-
template: '<test v-ref:test></test>',
47+
template: '<test ref="test"></test>',
6148
components
6249
})
6350
vm.$mount()
@@ -68,7 +55,7 @@ describe('Directive v-ref', () => {
6855
it('should accept dynamic component', done => {
6956
const vm = new Vue({
7057
template: `<div>
71-
<component :is="test" v-ref:test></component>
58+
<component :is="test" ref="test"></component>
7259
</div>`,
7360
components,
7461
data: { test: 'test' }
@@ -91,7 +78,7 @@ describe('Directive v-ref', () => {
9178
},
9279
template: `
9380
<div>
94-
<div v-for="n in items" v-ref:list>{{n}}</div>
81+
<div v-for="n in items" ref="list">{{n}}</div>
9582
</div>
9683
`
9784
}).$mount()
@@ -117,7 +104,7 @@ describe('Directive v-ref', () => {
117104
},
118105
template: `
119106
<div>
120-
<test v-for="n in items" v-ref:list :n="n"></test>
107+
<test v-for="n in items" ref="list" :n="n"></test>
121108
</div>
122109
`,
123110
components: {

test/unit/modules/compiler/codegen.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ describe('codegen', () => {
6868
)
6969
})
7070

71-
it('generate v-ref directive', () => {
71+
it('generate ref', () => {
7272
assertCodegen(
73-
'<p v-ref:component1></p>',
73+
'<p ref="component1"></p>',
7474
`with(this){return _h(_e('p',{ref:"component1"}))}`
7575
)
7676
})
7777

78-
it('generate v-ref directive on v-for', () => {
78+
it('generate ref on v-for', () => {
7979
assertCodegen(
80-
'<ul><li v-for="item in items" v-ref:component1></li></ul>',
80+
'<ul><li v-for="item in items" ref="component1"></li></ul>',
8181
`with(this){return _h(_e('ul'),[(items)&&_l((items),function(item){return _h(_e('li',{ref:"component1",refInFor:true}))})])}`
8282
)
8383
})

test/unit/modules/compiler/optimizer.spec.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,33 @@ describe('optimizer', () => {
152152
expect(ast.children[0].static).toBe(false)
153153
})
154154

155-
it('transition', () => {
156-
const ast = parse('<p v-if="show" transition="expand">hello world</p>', baseOptions)
155+
it('key', () => {
156+
const ast = parse('<p key="foo">hello world</p>', baseOptions)
157157
optimize(ast, baseOptions)
158158
expect(ast.static).toBe(false)
159159
expect(ast.children[0].static).toBe(true)
160160
})
161161

162-
it('v-bind directive', () => {
163-
const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
162+
it('ref', () => {
163+
const ast = parse('<p ref="foo">hello world</p>', baseOptions)
164164
optimize(ast, baseOptions)
165165
expect(ast.static).toBe(false)
166+
expect(ast.children[0].static).toBe(true)
166167
})
167168

168-
it('v-ref directive', () => {
169-
const ast = parse('<p v-ref:foo>hello world</p>', baseOptions)
169+
it('transition', () => {
170+
const ast = parse('<p v-if="show" transition="expand">hello world</p>', baseOptions)
170171
optimize(ast, baseOptions)
171172
expect(ast.static).toBe(false)
172173
expect(ast.children[0].static).toBe(true)
173174
})
174175

176+
it('v-bind directive', () => {
177+
const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
178+
optimize(ast, baseOptions)
179+
expect(ast.static).toBe(false)
180+
})
181+
175182
it('v-on directive', () => {
176183
const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
177184
optimize(ast, baseOptions)

0 commit comments

Comments
 (0)