Parcourir la source

<content> select should only match children.

Fixes 786
Nikola Kovacs il y a 11 ans
Parent
commit
cfdfda35aa
2 fichiers modifiés avec 16 ajouts et 5 suppressions
  1. 7 5
      src/compiler/transclude.js
  2. 9 0
      test/unit/specs/compiler/transclude_spec.js

+ 7 - 5
src/compiler/transclude.js

@@ -87,11 +87,13 @@ function transcludeContent (el, raw) {
       select = outlet.getAttribute('select')
       if (select) {  // select content
         selected = raw.querySelectorAll(select)
-        outlet.content = _.toArray(
-          selected.length
-            ? selected
-            : outlet.childNodes
-        )
+        if (selected.length) {
+          outlet.content = _.toArray(selected).filter(function(element) {
+            return element.parentNode === raw
+          })
+        } else {
+          outlet.content = _.toArray(outlet.childNodes)
+        }
       } else { // default content
         main = outlet
       }

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

@@ -109,5 +109,14 @@ if (_.inBrowser) {
       expect(res.childNodes[3].tagName).toBe('SPAN')
     })
 
+    it('select should only match children', function () {
+      el.innerHTML = '<p class="b">select b</p><span><p class="b">nested b</p></span>'
+      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')
+    })
+
   })
 }