Skip to content

Commit

Permalink
change ref to a built-in attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 17, 2016
1 parent e3fb6fe commit c949c74
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/compiler/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function genData (el: ASTElement): string | void {
}
// ref
if (el.ref) {
data += `ref:"${el.ref}",`
data += `ref:${el.ref},`
}
if (el.refInFor) {
data += `refInFor:true,`
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/directives/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import ref from './ref'
import bind from './bind'
import { noop } from 'shared/util'

export default {
ref,
bind,
cloak: noop
}
16 changes: 0 additions & 16 deletions src/compiler/directives/ref.js

This file was deleted.

22 changes: 20 additions & 2 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,16 @@ export function parse (
if (inPre) {
processRawAttrs(element)
} else {
processKey(element)
processFor(element)
processIf(element)
processOnce(element)

// determine whether this is a plain element after
// removing if/for/once attributes
// removing structural attributes
element.plain = !element.key && !attrs.length

processKey(element)
processRef(element)
processSlot(element)
processComponent(element)
for (let i = 0; i < transforms.length; i++) {
Expand Down Expand Up @@ -252,6 +255,21 @@ function processKey (el) {
}
}

function processRef (el) {
const ref = getBindingAttr(el, 'ref')
if (ref) {
el.ref = ref
let parent = el
while (parent) {
if (parent.for !== undefined) {
el.refInFor = true
break
}
parent = parent.parent
}
}
}

function processFor (el) {
let exp
if ((exp = getAndRemoveAttr(el, 'v-for'))) {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/features/options/props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('Options props', () => {
data: {
b: 'bar'
},
template: '<test v-bind:b="b" v-ref:child></test>',
template: '<test v-bind:b="b" ref="child"></test>',
components: {
test: {
props: ['b'],
Expand All @@ -30,7 +30,7 @@ describe('Options props', () => {
data: {
b: 'bar'
},
template: '<test v-bind:b="b" v-ref:child></test>',
template: '<test v-bind:b="b" ref="child"></test>',
components: {
test: {
props: { b: String },
Expand Down Expand Up @@ -266,7 +266,7 @@ describe('Options props', () => {

it('treat boolean props properly', () => {
const vm = new Vue({
template: '<comp v-ref:child prop-a prop-b="prop-b"></comp>',
template: '<comp ref="child" prop-a prop-b="prop-b"></comp>',
components: {
comp: {
template: '<div></div>',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Vue from 'vue'

describe('Directive v-ref', () => {
describe('ref', () => {
const components = {
test: {
id: 'test'
Expand All @@ -10,19 +10,22 @@ describe('Directive v-ref', () => {
}
}

it('should accept hyphenated refs', () => {
it('should work', () => {
const vm = new Vue({
data: {
value: 'bar'
},
template: `<div>
<test v-ref:test></test>
<test2 v-ref:test-hyphen></test2>
<test ref="foo"></test>
<test2 :ref="value"></test2>
</div>`,
components
})
vm.$mount()
expect(vm.$refs.test).toBeTruthy()
expect(vm.$refs.test.$options.id).toBe('test')
expect(vm.$refs['test-hyphen']).toBeTruthy()
expect(vm.$refs['test-hyphen'].$options.id).toBe('test2')
expect(vm.$refs.foo).toBeTruthy()
expect(vm.$refs.foo.$options.id).toBe('test')
expect(vm.$refs.bar).toBeTruthy()
expect(vm.$refs.bar.$options.id).toBe('test2')
})

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

it('should accept camelCase refs', () => {
const vm = new Vue({
template:
`<div>
<test v-ref:test></test>
<test2 v-ref:testCase></test2>
</div>`,
components
})
vm.$mount()
expect(vm.$refs.test).toBeTruthy()
expect(vm.$refs.test.$options.id).toBe('test')
expect(vm.$refs.testCase).toBeTruthy()
expect(vm.$refs.testCase.$options.id).toBe('test2')
})

it('should accept HOC component', () => {
const vm = new Vue({
template: '<test v-ref:test></test>',
template: '<test ref="test"></test>',
components
})
vm.$mount()
Expand All @@ -68,7 +55,7 @@ describe('Directive v-ref', () => {
it('should accept dynamic component', done => {
const vm = new Vue({
template: `<div>
<component :is="test" v-ref:test></component>
<component :is="test" ref="test"></component>
</div>`,
components,
data: { test: 'test' }
Expand All @@ -91,7 +78,7 @@ describe('Directive v-ref', () => {
},
template: `
<div>
<div v-for="n in items" v-ref:list>{{n}}</div>
<div v-for="n in items" ref="list">{{n}}</div>
</div>
`
}).$mount()
Expand All @@ -117,7 +104,7 @@ describe('Directive v-ref', () => {
},
template: `
<div>
<test v-for="n in items" v-ref:list :n="n"></test>
<test v-for="n in items" ref="list" :n="n"></test>
</div>
`,
components: {
Expand Down
8 changes: 4 additions & 4 deletions test/unit/modules/compiler/codegen.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ describe('codegen', () => {
)
})

it('generate v-ref directive', () => {
it('generate ref', () => {
assertCodegen(
'<p v-ref:component1></p>',
'<p ref="component1"></p>',
`with(this){return _h(_e('p',{ref:"component1"}))}`
)
})

it('generate v-ref directive on v-for', () => {
it('generate ref on v-for', () => {
assertCodegen(
'<ul><li v-for="item in items" v-ref:component1></li></ul>',
'<ul><li v-for="item in items" ref="component1"></li></ul>',
`with(this){return _h(_e('ul'),[(items)&&_l((items),function(item){return _h(_e('li',{ref:"component1",refInFor:true}))})])}`
)
})
Expand Down
19 changes: 13 additions & 6 deletions test/unit/modules/compiler/optimizer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,33 @@ describe('optimizer', () => {
expect(ast.children[0].static).toBe(false)
})

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

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

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

it('v-bind directive', () => {
const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
optimize(ast, baseOptions)
expect(ast.static).toBe(false)
})

it('v-on directive', () => {
const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
optimize(ast, baseOptions)
Expand Down

0 comments on commit c949c74

Please sign in to comment.