Skip to content

Commit a01f056

Browse files
committed
handle Safari template clone bug even when nested (fix #1118)
1 parent 3d81980 commit a01f056

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/parsers/template.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ map.rect = [
5656
'</svg>'
5757
]
5858

59+
/**
60+
* Check if a node is a supported template node with a
61+
* DocumentFragment content.
62+
*
63+
* @param {Node} node
64+
* @return {Boolean}
65+
*/
66+
67+
function isRealTemplate (node) {
68+
return _.isTemplate(node) &&
69+
node.content instanceof DocumentFragment
70+
}
71+
5972
var tagRE = /<([\w:]+)/
6073
var entityRE = /&\w+;/
6174

@@ -120,10 +133,7 @@ function stringToFragment (templateString) {
120133
function nodeToFragment (node) {
121134
// if its a template tag and the browser supports it,
122135
// its content is already a document fragment.
123-
if (
124-
_.isTemplate(node) &&
125-
node.content instanceof DocumentFragment
126-
) {
136+
if (isRealTemplate(node)) {
127137
_.trimNode(node.content)
128138
return node.content
129139
}
@@ -174,16 +184,21 @@ var hasTextareaCloneBug = _.inBrowser
174184
*/
175185

176186
exports.clone = function (node) {
177-
var res = node.cloneNode(true)
178187
if (!node.querySelectorAll) {
179-
return res
188+
return node.cloneNode()
180189
}
190+
var res = node.cloneNode(true)
181191
var i, original, cloned
182192
/* istanbul ignore if */
183193
if (hasBrokenTemplate) {
194+
var clone = res
195+
if (isRealTemplate(node)) {
196+
node = node.content
197+
clone = res.content
198+
}
184199
original = node.querySelectorAll('template')
185200
if (original.length) {
186-
cloned = res.querySelectorAll('template')
201+
cloned = clone.querySelectorAll('template')
187202
i = cloned.length
188203
while (i--) {
189204
cloned[i].parentNode.replaceChild(

test/unit/specs/parsers/template_spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ if (_.inBrowser) {
137137
expect(c.firstChild.innerHTML).toBe('1')
138138
})
139139

140+
it('should deal with Safari template clone bug even when nested', function () {
141+
var a = document.createElement('div')
142+
a.innerHTML = '<template><div>1</div><template>2</template></template>'
143+
var c = templateParser.clone(a)
144+
expect(c.firstChild.innerHTML).toBe('<div>1</div><template>2</template>')
145+
})
146+
140147
it('should deal with IE textarea clone bug', function () {
141148
var t = document.createElement('textarea')
142149
t.placeholder = 't'

0 commit comments

Comments
 (0)