Skip to content

Commit 9a0c36e

Browse files
committed
preserve whitespace for v-html (fix vuejs#1800)
1 parent cb15494 commit 9a0c36e

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/parsers/template.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ var entityRE = /&\w+;|&#\d+;|&#x[\dA-F]+;/
7878
* strategy found in jQuery & component/domify.
7979
*
8080
* @param {String} templateString
81+
* @param {Boolean} raw
8182
* @return {DocumentFragment}
8283
*/
8384

84-
function stringToFragment (templateString) {
85+
function stringToFragment (templateString, raw) {
8586
// try a cache hit first
8687
var hit = templateCache.get(templateString)
8788
if (hit) {
@@ -106,7 +107,10 @@ function stringToFragment (templateString) {
106107
var suffix = wrap[2]
107108
var node = document.createElement('div')
108109

109-
node.innerHTML = prefix + templateString.trim() + suffix
110+
if (!raw) {
111+
templateString = templateString.trim()
112+
}
113+
node.innerHTML = prefix + templateString + suffix
110114
while (depth--) {
111115
node = node.lastChild
112116
}
@@ -238,17 +242,19 @@ exports.clone = function (node) {
238242
* instance template.
239243
*
240244
* @param {*} template
241-
* Possible values include:
242-
* - DocumentFragment object
243-
* - Node object of type Template
244-
* - id selector: '#some-template-id'
245-
* - template string: '<div><span>{{msg}}</span></div>'
245+
* Possible values include:
246+
* - DocumentFragment object
247+
* - Node object of type Template
248+
* - id selector: '#some-template-id'
249+
* - template string: '<div><span>{{msg}}</span></div>'
246250
* @param {Boolean} clone
247-
* @param {Boolean} noSelector
251+
* @param {Boolean} raw
252+
* inline HTML interpolation. Do not check for id
253+
* selector and keep whitespace in the string.
248254
* @return {DocumentFragment|undefined}
249255
*/
250256

251-
exports.parse = function (template, clone, noSelector) {
257+
exports.parse = function (template, clone, raw) {
252258
var node, frag
253259

254260
// if the template is already a document fragment,
@@ -262,7 +268,7 @@ exports.parse = function (template, clone, noSelector) {
262268

263269
if (typeof template === 'string') {
264270
// id selector
265-
if (!noSelector && template.charAt(0) === '#') {
271+
if (!raw && template.charAt(0) === '#') {
266272
// id selector can be cached too
267273
frag = idSelectorCache.get(template)
268274
if (!frag) {
@@ -275,7 +281,7 @@ exports.parse = function (template, clone, noSelector) {
275281
}
276282
} else {
277283
// normal string template
278-
frag = stringToFragment(template)
284+
frag = stringToFragment(template, raw)
279285
}
280286
} else if (template.nodeType) {
281287
// a direct node

test/unit/specs/directives/public/html_spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,17 @@ if (_.inBrowser) {
3939
expect(el.innerHTML).toBe('')
4040
})
4141

42+
it('inline keep whitespace', function () {
43+
var node = document.createComment('html-test')
44+
el.appendChild(node)
45+
var dir = {
46+
el: node
47+
}
48+
_.extend(dir, def)
49+
dir.bind()
50+
dir.update(' <p>span</p> ')
51+
expect(el.innerHTML).toBe(' <p>span</p> ')
52+
})
53+
4254
})
4355
}

0 commit comments

Comments
 (0)