Skip to content

Commit

Permalink
feature(directive): add .preserve modifier to v-t directive
Browse files Browse the repository at this point in the history
  • Loading branch information
bponomarenko committed Dec 28, 2018
1 parent aa20962 commit 8a000c3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export function unbind (el: any, binding: Object, vnode: any, oldVNode: any): vo
return
}

el.textContent = ''
if (!binding.modifiers.preserve) {
el.textContent = ''
}
el._vt = undefined
delete el['_vt']
el._locale = undefined
Expand Down
48 changes: 48 additions & 0 deletions test/unit/directive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,53 @@ describe('custom directive', () => {
}).then(done)
})
})

describe('preserve content', () => {
it('should clear element content on destroy by default', done => {
const vm = createVM({
i18n,
data: () => ({ visible: true }),
render (h) {
// <p ref="text" v-t="'message.hello'"></p>
const directives = this.visible ? [{
name: 't', rawName: 'v-t', value: ('message.hello'), expression: "'message.hello'"
}] : []
return h('p', { ref: 'text', directives })
}
})

nextTick(() => {
assert.strictEqual(vm.$refs.text.textContent, messages.en.message.hello)

vm.visible = false
vm.$forceUpdate()
}).then(() => {
assert.strictEqual(vm.$refs.text.textContent, '')
}).then(done)
})

it('should not clear element content with "preserve" modifier', done => {
const vm = createVM({
i18n,
data: () => ({ visible: true }),
render (h) {
// <p ref="text" v-t.preserve="'message.hello'"></p>
const directives = this.visible ? [{
name: 't', rawName: 'v-t', value: ('message.hello'), expression: "'message.hello'", modifiers: { preserve: true }
}] : []
return h('p', { ref: 'text', directives })
}
})

nextTick(() => {
assert.strictEqual(vm.$refs.text.textContent, messages.en.message.hello)

vm.visible = false
vm.$forceUpdate()
}).then(() => {
assert.strictEqual(vm.$refs.text.textContent, messages.en.message.hello)
}).then(done)
})
})
})
})

0 comments on commit 8a000c3

Please sign in to comment.