Skip to content

Commit 78271e5

Browse files
committed
add elementDirectives
1 parent 70d5280 commit 78271e5

File tree

8 files changed

+45
-13
lines changed

8 files changed

+45
-13
lines changed

src/api/global.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ exports.use = function (plugin) {
103103

104104
var assetTypes = [
105105
'directive',
106+
'elementDirective',
106107
'filter',
107-
'partial',
108108
'transition'
109109
]
110110

src/compiler/compile.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ function compileElement (el, options) {
194194
}
195195
var linkFn
196196
var hasAttrs = el.hasAttributes()
197+
// check element directives
198+
linkFn = checkElementDirectives(el, options)
197199
// check terminal direcitves (repeat & if)
198-
if (hasAttrs) {
200+
if (!linkFn && hasAttrs) {
199201
linkFn = checkTerminalDirectives(el, options)
200202
}
201203
// check component
@@ -444,6 +446,22 @@ function makePropsLinkFn (props) {
444446
}
445447
}
446448

449+
/**
450+
* Check for element directives (custom elements that should
451+
* be resovled as terminal directives).
452+
*
453+
* @param {Element} el
454+
* @param {Object} options
455+
*/
456+
457+
function checkElementDirectives (el, options) {
458+
var tag = el.tagName.toLowerCase()
459+
var def = options.elementDirectives[tag]
460+
if (def) {
461+
return makeTerminalNodeLinkFn(el, tag, '', options, def)
462+
}
463+
}
464+
447465
/**
448466
* Check if an element is a component. If yes, return
449467
* a component link function.
@@ -503,12 +521,13 @@ skip.terminal = true
503521
* @param {String} dirName
504522
* @param {String} value
505523
* @param {Object} options
524+
* @param {Object} [def]
506525
* @return {Function} terminalLinkFn
507526
*/
508527

509-
function makeTerminalNodeLinkFn (el, dirName, value, options) {
528+
function makeTerminalNodeLinkFn (el, dirName, value, options, def) {
510529
var descriptor = dirParser.parse(value)[0]
511-
var def = options.directives[dirName]
530+
def = def || options.directives[dirName]
512531
var fn = function terminalNodeLinkFn (vm, el, host) {
513532
vm._bindDir(dirName, el, descriptor, def, host)
514533
}

src/directives/component.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var templateParser = require('../parsers/template')
33

44
module.exports = {
55

6-
_internal: true,
76
isLiteral: true,
87

98
/**

src/directives/prop.js

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ var expParser = require('../parsers/expression')
44

55
module.exports = {
66

7-
_internal: true,
8-
97
bind: function () {
108

119
var child = this.vm

src/util/merge-option.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ strats.props = function (parentVal, childVal) {
133133

134134
strats.directives =
135135
strats.filters =
136-
strats.partials =
137136
strats.transitions =
138-
strats.components = function (parentVal, childVal, vm, key) {
137+
strats.components =
138+
strats.elementDirectives = function (parentVal, childVal, vm, key) {
139139
var ret = Object.create(
140140
vm && vm.$parent
141141
? vm.$parent.$options[key]

src/vue.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ extend(Vue, require('./api/global'))
3737
Vue.options = {
3838
directives : require('./directives'),
3939
filters : require('./filters'),
40-
partials : {},
4140
transitions : {},
42-
components : {}
41+
components : {},
42+
elementDirectives: {}
4343
}
4444

4545
/**

test/unit/specs/api/global_spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ describe('Global API', function () {
6161

6262
var Test = Vue.extend()
6363

64-
it('directive / filter / partial / transition', function () {
64+
it('directive / elementDirective / filter / transition', function () {
6565
[
6666
'directive',
67+
'elementDirective',
6768
'filter',
68-
'partial',
6969
'transition'
7070
].forEach(function (type) {
7171
var def = {}

test/unit/specs/compiler/compile_spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -352,5 +352,21 @@ if (_.inBrowser) {
352352
})
353353
})
354354

355+
it('element directive', function () {
356+
var vm = new Vue({
357+
el: el,
358+
template: '<test>{{a}}</test>',
359+
elementDirectives: {
360+
test: {
361+
bind: function () {
362+
this.el.setAttribute('test', '1')
363+
}
364+
}
365+
}
366+
})
367+
// should be terminal
368+
expect(el.innerHTML).toBe('<test test="1">{{a}}</test>')
369+
})
370+
355371
})
356372
}

0 commit comments

Comments
 (0)