Selaa lähdekoodia

preserve whitespace for v-html (fix #1800)

Evan You 10 vuotta sitten
vanhempi
commit
9a0c36e95f
2 muutettua tiedostoa jossa 29 lisäystä ja 11 poistoa
  1. 17 11
      src/parsers/template.js
  2. 12 0
      test/unit/specs/directives/public/html_spec.js

+ 17 - 11
src/parsers/template.js

@@ -78,10 +78,11 @@ var entityRE = /&\w+;|&#\d+;|&#x[\dA-F]+;/
  * strategy found in jQuery & component/domify.
  *
  * @param {String} templateString
+ * @param {Boolean} raw
  * @return {DocumentFragment}
  */
 
-function stringToFragment (templateString) {
+function stringToFragment (templateString, raw) {
   // try a cache hit first
   var hit = templateCache.get(templateString)
   if (hit) {
@@ -106,7 +107,10 @@ function stringToFragment (templateString) {
     var suffix = wrap[2]
     var node = document.createElement('div')
 
-    node.innerHTML = prefix + templateString.trim() + suffix
+    if (!raw) {
+      templateString = templateString.trim()
+    }
+    node.innerHTML = prefix + templateString + suffix
     while (depth--) {
       node = node.lastChild
     }
@@ -238,17 +242,19 @@ exports.clone = function (node) {
  * instance template.
  *
  * @param {*} template
- *    Possible values include:
- *    - DocumentFragment object
- *    - Node object of type Template
- *    - id selector: '#some-template-id'
- *    - template string: '<div><span>{{msg}}</span></div>'
+ *        Possible values include:
+ *        - DocumentFragment object
+ *        - Node object of type Template
+ *        - id selector: '#some-template-id'
+ *        - template string: '<div><span>{{msg}}</span></div>'
  * @param {Boolean} clone
- * @param {Boolean} noSelector
+ * @param {Boolean} raw
+ *        inline HTML interpolation. Do not check for id
+ *        selector and keep whitespace in the string.
  * @return {DocumentFragment|undefined}
  */
 
-exports.parse = function (template, clone, noSelector) {
+exports.parse = function (template, clone, raw) {
   var node, frag
 
   // if the template is already a document fragment,
@@ -262,7 +268,7 @@ exports.parse = function (template, clone, noSelector) {
 
   if (typeof template === 'string') {
     // id selector
-    if (!noSelector && template.charAt(0) === '#') {
+    if (!raw && template.charAt(0) === '#') {
       // id selector can be cached too
       frag = idSelectorCache.get(template)
       if (!frag) {
@@ -275,7 +281,7 @@ exports.parse = function (template, clone, noSelector) {
       }
     } else {
       // normal string template
-      frag = stringToFragment(template)
+      frag = stringToFragment(template, raw)
     }
   } else if (template.nodeType) {
     // a direct node

+ 12 - 0
test/unit/specs/directives/public/html_spec.js

@@ -39,5 +39,17 @@ if (_.inBrowser) {
       expect(el.innerHTML).toBe('')
     })
 
+    it('inline keep whitespace', function () {
+      var node = document.createComment('html-test')
+      el.appendChild(node)
+      var dir = {
+        el: node
+      }
+      _.extend(dir, def)
+      dir.bind()
+      dir.update('    <p>span</p>    ')
+      expect(el.innerHTML).toBe('    <p>span</p>    ')
+    })
+
   })
 }