Skip to content

Commit 9fc0068

Browse files
committed
_.attr: make it more generic
1 parent c350b76 commit 9fc0068

File tree

8 files changed

+11
-22
lines changed

8 files changed

+11
-22
lines changed

src/compiler/compile-props.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ module.exports = function compileProps (el, propOptions) {
5050

5151
// first check literal version
5252
attr = _.hyphenate(name)
53-
value = prop.raw = el.getAttribute(attr)
54-
if (value !== null) {
55-
el.removeAttribute(attr)
56-
} else {
57-
53+
value = prop.raw = _.attr(el, attr)
54+
if (value === null) {
5855
// then check dynamic version
5956
if ((value = _.getBindAttr(el, attr)) === null) {
6057
if ((value = _.getBindAttr(el, attr + '.sync')) !== null) {

src/compiler/compile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ function checkComponent (el, options) {
490490

491491
function checkTerminalDirectives (el, options) {
492492
// skip v-pre
493-
if (_.attr(el, 'pre') !== null) {
493+
if (_.attr(el, 'v-pre') !== null) {
494494
return skip
495495
}
496496
// skip v-else block, but only if following v-if

src/directive.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,8 @@ Directive.prototype._checkStatement = function () {
160160
*/
161161

162162
Directive.prototype.param = function (name) {
163-
var param = this.el.getAttribute(name)
163+
var param = _.attr(this.el, name)
164164
if (param != null) {
165-
this.el.removeAttribute(name)
166165
param = (this._scope || this.vm).$interpolate(param)
167166
} else {
168167
param = _.getBindAttr(this.el, name)

src/directives/public/if.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
if (!el.__vue__) {
1111
// check else block
1212
var next = el.nextElementSibling
13-
if (next && _.attr(next, 'else') !== null) {
13+
if (next && _.attr(next, 'v-else') !== null) {
1414
_.remove(next)
1515
this.elseFactory = new FragmentFactory(this.vm, next)
1616
}

src/directives/public/show.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
bind: function () {
77
// check else block
88
var next = this.el.nextElementSibling
9-
if (next && _.attr(next, 'else') !== null) {
9+
if (next && _.attr(next, 'v-else') !== null) {
1010
this.elseEl = next
1111
}
1212
},

src/util/component.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ exports.checkComponent = function (el, options) {
4444

4545
function getIsBinding (el) {
4646
// dynamic syntax
47-
var exp = el.getAttribute('is')
47+
var exp = _.attr(el, 'is')
4848
if (exp != null) {
49-
el.removeAttribute('is')
5049
return { id: exp }
5150
} else {
5251
exp = _.getBindAttr(el, 'is')

src/util/dom.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ exports.inDoc = function (node) {
4242
}
4343

4444
/**
45-
* Extract an attribute from a node.
45+
* Get and remove an attribute from a node.
4646
*
4747
* @param {Node} node
4848
* @param {String} attr
4949
*/
5050

5151
exports.attr = function (node, attr) {
52-
attr = 'v-' + attr
5352
var val = node.getAttribute(attr)
5453
if (val !== null) {
5554
node.removeAttribute(attr)
@@ -66,14 +65,9 @@ exports.attr = function (node, attr) {
6665
*/
6766

6867
exports.getBindAttr = function (node, name) {
69-
var attr = ':' + name
70-
var val = node.getAttribute(attr)
68+
var val = exports.attr(node, ':' + name)
7169
if (val === null) {
72-
attr = 'v-bind:' + name
73-
val = node.getAttribute(attr)
74-
}
75-
if (val !== null) {
76-
node.removeAttribute(attr)
70+
val = exports.attr(node, 'v-bind:' + name)
7771
}
7872
return val
7973
}

test/unit/specs/util/dom_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if (_.inBrowser) {
2727

2828
it('attr', function () {
2929
target.setAttribute('v-test', 'ok')
30-
var val = _.attr(target, 'test')
30+
var val = _.attr(target, 'v-test')
3131
expect(val).toBe('ok')
3232
expect(target.hasAttribute('v-test')).toBe(false)
3333
})

0 commit comments

Comments
 (0)