Skip to content

Commit fb023b3

Browse files
committed
fix #558 ie10/11 textarea placeholder bug
1 parent 25fb48d commit fb023b3

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

src/parse/template.js

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@ var _ = require('../util')
22
var Cache = require('../cache')
33
var templateCache = new Cache(100)
44

5-
/**
6-
* Test for the presence of the Safari template cloning bug
7-
* https://bugs.webkit.org/show_bug.cgi?id=137755
8-
*/
9-
10-
var hasBrokenTemplate = _.inBrowser
11-
? (function () {
12-
var a = document.createElement('div')
13-
a.innerHTML = '<template>1</template>'
14-
return !a.cloneNode(true).firstChild.innerHTML
15-
})()
16-
: false
17-
185
var map = {
196
_default : [0, '', ''],
207
legend : [1, '<fieldset>', '</fieldset>'],
@@ -141,30 +128,67 @@ function nodeToFragment (node) {
141128
: stringToFragment(node.innerHTML)
142129
}
143130

131+
// Test for the presence of the Safari template cloning bug
132+
// https://bugs.webkit.org/show_bug.cgi?id=137755
133+
var hasBrokenTemplate = _.inBrowser
134+
? (function () {
135+
var a = document.createElement('div')
136+
a.innerHTML = '<template>1</template>'
137+
return !a.cloneNode(true).firstChild.innerHTML
138+
})()
139+
: false
140+
141+
// Test for IE10/11 textarea placeholder clone bug
142+
var hasTextareaCloneBug = _.inBrowser
143+
? (function () {
144+
var t = document.createElement('textarea')
145+
t.placeholder = 't'
146+
return t.cloneNode(true).value === 't'
147+
})()
148+
: false
149+
144150
/**
145-
* Deal with Safari cloning nested <template> bug by
146-
* manually cloning all template instances.
151+
* 1. Deal with Safari cloning nested <template> bug by
152+
* manually cloning all template instances.
153+
* 2. Deal with IE10/11 textarea placeholder bug by setting
154+
* the correct value after cloning.
147155
*
148156
* @param {Element|DocumentFragment} node
149157
* @return {Element|DocumentFragment}
150158
*/
151159

152160
exports.clone = function (node) {
153161
var res = node.cloneNode(true)
162+
var i, original, cloned
154163
/* istanbul ignore if */
155164
if (hasBrokenTemplate) {
156-
var templates = node.querySelectorAll('template')
157-
if (templates.length) {
158-
var cloned = res.querySelectorAll('template')
159-
var i = cloned.length
165+
original = node.querySelectorAll('template')
166+
if (original.length) {
167+
cloned = res.querySelectorAll('template')
168+
i = cloned.length
160169
while (i--) {
161170
cloned[i].parentNode.replaceChild(
162-
templates[i].cloneNode(true),
171+
original[i].cloneNode(true),
163172
cloned[i]
164173
)
165174
}
166175
}
167176
}
177+
/* istanbul ignore if */
178+
if (hasTextareaCloneBug) {
179+
if (node.tagName === 'TEXTAREA') {
180+
res.value = node.value
181+
} else {
182+
original = node.querySelectorAll('textarea')
183+
if (original.length) {
184+
cloned = res.querySelectorAll('textarea')
185+
i = cloned.length
186+
while (i--) {
187+
cloned[i].value = original[i].value
188+
}
189+
}
190+
}
191+
}
168192
return res
169193
}
170194

test/unit/specs/parse/template_spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,19 @@ if (_.inBrowser) {
116116
expect(res1).toBe(res2)
117117
document.head.removeChild(node)
118118
})
119+
120+
it('should deal with Safari template clone bug', function () {
121+
var a = document.createElement('div')
122+
a.innerHTML = '<template>1</template>'
123+
var c = templateParser.clone(a)
124+
expect(a.firstChild.innerHTML).toBe('1')
125+
})
126+
127+
it('should deal with IE textarea clone bug', function () {
128+
var t = document.createElement('textarea')
129+
t.placeholder = 't'
130+
var c = templateParser.clone(t)
131+
expect(c.value).toBe('')
132+
})
119133
})
120134
}

0 commit comments

Comments
 (0)