Skip to content

Commit 7555abd

Browse files
committed
fix template check in svg (fix #922)
1 parent 3310440 commit 7555abd

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

src/compiler/transclude.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ exports.transclude = function (el, options) {
2525
}
2626
// for template tags, what we want is its content as
2727
// a documentFragment (for block instances)
28-
if (el.tagName === 'TEMPLATE') {
28+
if (_.isTemplate(el)) {
2929
el = templateParser.parse(el)
3030
}
3131
if (options && options.template) {

src/directives/if.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
this.end = _.createAnchor('v-if-end')
1313
_.replace(el, this.end)
1414
_.before(this.start, this.end)
15-
if (el.tagName === 'TEMPLATE') {
15+
if (_.isTemplate(el)) {
1616
this.template = templateParser.parse(el, true)
1717
} else {
1818
this.template = document.createDocumentFragment()

src/directives/repeat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828
_.replace(this.el, this.end)
2929
_.before(this.start, this.end)
3030
// check if this is a block repeat
31-
this.template = this.el.tagName === 'TEMPLATE'
31+
this.template = _.isTemplate(this.el)
3232
? templateParser.parse(this.el, true)
3333
: this.el
3434
// check other directives that need to be handled

src/parsers/template.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,16 @@ function stringToFragment (templateString) {
117117
*/
118118

119119
function nodeToFragment (node) {
120-
var tag = node.tagName
121120
// if its a template tag and the browser supports it,
122121
// its content is already a document fragment.
123122
if (
124-
tag === 'TEMPLATE' &&
123+
_.isTemplate(node) &&
125124
node.content instanceof DocumentFragment
126125
) {
127126
return node.content
128127
}
129128
// script template
130-
if (tag === 'SCRIPT') {
129+
if (node.tagName === 'SCRIPT') {
131130
return stringToFragment(node.textContent)
132131
}
133132
// normal node, clone it to avoid mutating the original

src/util/dom.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ exports.extractContent = function (el, asFragment) {
177177
var rawContent
178178
/* istanbul ignore if */
179179
if (
180-
el.tagName === 'TEMPLATE' &&
180+
exports.isTemplate(el) &&
181181
el.content instanceof DocumentFragment
182182
) {
183183
el = el.content
@@ -193,3 +193,16 @@ exports.extractContent = function (el, asFragment) {
193193
}
194194
return rawContent
195195
}
196+
197+
/**
198+
* Check if an element is a template tag.
199+
* Note if the template appears inside an SVG its tagName
200+
* will be in lowercase.
201+
*
202+
* @param {Element} el
203+
*/
204+
205+
exports.isTemplate = function (el) {
206+
return el.tagName &&
207+
el.tagName.toLowerCase() === 'template'
208+
}

test/unit/specs/misc_spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,17 @@ describe('Misc', function () {
6565
expect(vm.$el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div>')
6666
})
6767

68+
// #922
69+
it('template repeat inside svg', function () {
70+
var el = document.createElement('div')
71+
var vm = new Vue({
72+
el: el,
73+
template: '<svg><template v-repeat="list"><text>{{$value}}</text></template></svg>',
74+
data: {
75+
list: [1, 2, 3]
76+
}
77+
})
78+
expect(el.innerHTML).toBe('<svg><text>1</text><text>2</text><text>3</text></svg>')
79+
})
80+
6881
})

0 commit comments

Comments
 (0)