Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for {{}} syntax #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/vue-linkify.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,29 @@
import linkify from 'linkifyjs/html'

(function () {
function install (el, binding) {
el.innerHTML = linkify(el.innerHTML, binding.value)
function surround (tagName, content) {
return `<${tagName}>${content}</${tagName}>`
}

function traverse (vnode, opts, isParent) {
const { text, tag, children } = vnode
if (text) return linkify(text, opts)
if (children) {
const content = children.map(childVNode => traverse(childVNode, opts, false)).join('')
if (isParent) return content
return surround(tag, content)
}
}

function install (el, binding, vnode) {
if (vnode.data.domProps && vnode.data.domProps.innerHTML) {
// when v-html is used
el.innerHTML = linkify(el.innerHTML, binding.value)
} else {
// when `{{}}` syntax is used
const isParent = true
el.innerHTML = traverse(vnode, binding.value, isParent)
}
}

if (typeof exports === 'object') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"lint": "./node_modules/.bin/eslint index.js",
"build": "./node_modules/.bin/eslint index.js && BABEL_ENV=production && babel index.js -o ./dist/vue-linkify.min.js",
"test": "npm build && karma start test/unit/karma.conf.js --single-run"
"test": "npm run build && karma start test/unit/karma.conf.js --single-run"
},
"keywords": [
"vue",
Expand Down
42 changes: 42 additions & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,46 @@ describe('vue-linkify', () => {
expect(vm.$el.innerHTML).to.be
.equal('Hello from <a href="http://vuejs.org" class="foo" target="_blank">vuejs.org</a>')
})

it('should correctly linkify content with {{}} syntax', () => {
const vm = new Vue({
el: document.createElement('div'),
data: {
msg: 'https://vuejs.org'
},
template: '<h v-linkified>Site url: {{ msg }}</h>'
}).$mount()

expect(vm.$el.innerHTML).to.be
.equal('Site url: <a href="https://vuejs.org" class="linkified" target="_blank">https://vuejs.org</a>')
})

it('should correctly linkify content with {{}} syntax and options', () => {
const vm = new Vue({
el: document.createElement('div'),
data: {
msg: 'https://vuejs.org'
},
template: '<h v-linkified v-linkified:options="{ className: \'foo\' }">Site url: {{ msg }}</h>'
}).$mount()

expect(vm.$el.innerHTML).to.be
.equal('Site url: <a href="https://vuejs.org" class="foo" target="_blank">https://vuejs.org</a>')
})

it('should correctly linkify content with {{}} even after update', () => {
const vm = new Vue({
el: document.createElement('div'),
data: {
msg: 'https://vuejs.org'
},
template: '<h v-linkified>Site url: {{ msg }}</h>'
}).$mount()

vm.msg = 'https://reactjs.org'
Vue.nextTick(() => {
expect(vm.$el.innerHTML).to.be
.equal('Site url: <a href="https://reactjs.org" class="linkified" target="_blank">https://reactjs.org</a>')
})
})
})