Răsfoiți Sursa

fix #531 content fallback with multiple insertion points

Evan You 11 ani în urmă
părinte
comite
ee2ae353da
2 a modificat fișierele cu 14 adăugiri și 2 ștergeri
  1. 5 2
      src/compile/transclude.js
  2. 9 0
      test/unit/specs/compile/transclude_spec.js

+ 5 - 2
src/compile/transclude.js

@@ -94,7 +94,7 @@ function transcludeContent (el) {
   var outlets = getOutlets(el)
   var i = outlets.length
   if (!i) return
-  var outlet, select, j, main
+  var outlet, select, selected, j, main
   // first pass, collect corresponding content
   // for each outlet.
   while (i--) {
@@ -102,8 +102,11 @@ function transcludeContent (el) {
     if (rawContent) {
       select = outlet.getAttribute('select')
       if (select) {  // select content
+        selected = rawContent.querySelectorAll(select)
         outlet.content = _.toArray(
-          rawContent.querySelectorAll(select)
+          selected.length
+            ? selected
+            : outlet.childNodes
         )
       } else { // default content
         main = outlet

+ 9 - 0
test/unit/specs/compile/transclude_spec.js

@@ -79,6 +79,15 @@ if (_.inBrowser) {
       expect(res.firstChild.textContent).toBe('fallback')
     })
 
+    it('fallback content with multiple select', function () {
+      el.innerHTML = '<p class="b">select b</p>'
+      options.template = '<content select=".a"><p>fallback a</p></content><content select=".b">fallback b</content>'
+      var res = transclude(el, options)
+      expect(res.childNodes.length).toBe(2)
+      expect(res.firstChild.textContent).toBe('fallback a')
+      expect(res.lastChild.textContent).toBe('select b')
+    })
+
     it('content transclusion with replace', function () {
       el.innerHTML = '<p>hi</p>'
       options.template = '<div><div><content></content></div></div>'