Skip to content

Commit ff56802

Browse files
committed
use innerHTML from template nodes for consistent behavior across browesrs (fix #2805)
1 parent 81c195f commit ff56802

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/parsers/template.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,12 @@ function stringToFragment (templateString, raw) {
142142

143143
function nodeToFragment (node) {
144144
// if its a template tag and the browser supports it,
145-
// its content is already a document fragment.
145+
// its content is already a document fragment. However, iOS Safari has
146+
// bug when using directly cloned template content with touch
147+
// events and can cause crashes the nodes are removed from DOM, so we have
148+
// to treat template elements as string templates. (#2805)
146149
if (isRealTemplate(node)) {
147-
trimNode(node.content)
148-
return node.content
150+
return stringToFragment(node.innerHTML)
149151
}
150152
// script template
151153
if (node.tagName === 'SCRIPT') {

test/unit/specs/parsers/template_spec.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ describe('Template Parser', function () {
99
expect(res).toBe(frag)
1010
})
1111

12-
it('should return content if argument is a valid template node', function () {
13-
var templateNode = document.createElement('template')
14-
if (!templateNode.content) {
15-
// mock the content
16-
templateNode.content = document.createDocumentFragment()
17-
}
18-
var res = parse(templateNode)
19-
expect(res).toBe(templateNode.content)
20-
})
21-
2212
it('should parse if argument is a template string', function () {
2313
var res = parse(testString)
2414
expect(res.nodeType).toBe(11)
@@ -50,6 +40,15 @@ describe('Template Parser', function () {
5040
expect(res.firstChild.nodeValue).toBe('')
5141
})
5242

43+
it('should parse innerHTML if argument is a template node', function () {
44+
var templateNode = document.createElement('template')
45+
templateNode.innerHTML = testString
46+
var res = parse(templateNode)
47+
expect(res.nodeType).toBe(11)
48+
expect(res.childNodes.length).toBe(2)
49+
expect(res.querySelector('.test').textContent).toBe('world')
50+
})
51+
5352
it('should parse textContent if argument is a script node', function () {
5453
var node = document.createElement('script')
5554
node.textContent = testString

0 commit comments

Comments
 (0)