11var _ = require ( '../util' )
22var config = require ( '../config' )
33var templateParser = require ( '../parsers/template' )
4- var transcludedFlagAttr = '__vue__transcluded'
54
65/**
76 * Process an element or a DocumentFragment based on a
@@ -24,26 +23,6 @@ module.exports = function transclude (el, options) {
2423 if ( options ) {
2524 options . _containerAttrs = extractAttrs ( el )
2625 }
27- // Mark content nodes and attrs so that the compiler
28- // knows they should be compiled in parent scope.
29- if ( options && options . _asComponent ) {
30- var i = el . childNodes . length
31- while ( i -- ) {
32- var node = el . childNodes [ i ]
33- if ( node . nodeType === 1 ) {
34- node . setAttribute ( transcludedFlagAttr , '' )
35- } else if ( node . nodeType === 3 && node . data . trim ( ) ) {
36- // wrap transcluded textNodes in spans, because
37- // raw textNodes can't be persisted through clones
38- // by attaching attributes.
39- var wrapper = document . createElement ( 'span' )
40- wrapper . textContent = node . data
41- wrapper . setAttribute ( '__vue__wrap' , '' )
42- wrapper . setAttribute ( transcludedFlagAttr , '' )
43- el . replaceChild ( wrapper , node )
44- }
45- }
46- }
4726 // for template tags, what we want is its content as
4827 // a documentFragment (for block instances)
4928 if ( el . tagName === 'TEMPLATE' ) {
@@ -77,7 +56,7 @@ function transcludeTemplate (el, options) {
7756 if ( ! frag ) {
7857 _ . warn ( 'Invalid template option: ' + template )
7958 } else {
80- var rawContent = options . _content || _ . extractContent ( el )
59+ options . _content = _ . extractContent ( el )
8160 var replacer = frag . firstChild
8261 if ( options . replace ) {
8362 if (
@@ -88,117 +67,22 @@ function transcludeTemplate (el, options) {
8867 // block instance. (#835)
8968 replacer . hasAttribute ( config . prefix + 'repeat' )
9069 ) {
91- transcludeContent ( frag , rawContent )
9270 return frag
9371 } else {
9472 options . _replacerAttrs = extractAttrs ( replacer )
9573 mergeAttrs ( el , replacer )
96- transcludeContent ( replacer , rawContent )
9774 return replacer
9875 }
9976 } else {
10077 el . appendChild ( frag )
101- transcludeContent ( el , rawContent )
10278 return el
10379 }
10480 }
10581}
10682
107- /**
108- * Resolve <content> insertion points mimicking the behavior
109- * of the Shadow DOM spec:
110- *
111- * http://w3c.github.io/webcomponents/spec/shadow/#insertion-points
112- *
113- * @param {Element|DocumentFragment } el
114- * @param {Element } raw
115- */
116-
117- function transcludeContent ( el , raw ) {
118- var outlets = getOutlets ( el )
119- var i = outlets . length
120- if ( ! i ) return
121- var outlet , select , selected , j , main
122-
123- function isDirectChild ( node ) {
124- return node . parentNode === raw
125- }
126-
127- // first pass, collect corresponding content
128- // for each outlet.
129- while ( i -- ) {
130- outlet = outlets [ i ]
131- if ( raw ) {
132- select = outlet . getAttribute ( 'select' )
133- if ( select ) { // select content
134- selected = raw . querySelectorAll ( select )
135- if ( selected . length ) {
136- // according to Shadow DOM spec, `select` can
137- // only select direct children of the host node.
138- // enforcing this also fixes #786.
139- selected = [ ] . filter . call ( selected , isDirectChild )
140- }
141- outlet . content = selected . length
142- ? selected
143- : _ . toArray ( outlet . childNodes )
144- } else { // default content
145- main = outlet
146- }
147- } else { // fallback content
148- outlet . content = _ . toArray ( outlet . childNodes )
149- }
150- }
151- // second pass, actually insert the contents
152- for ( i = 0 , j = outlets . length ; i < j ; i ++ ) {
153- outlet = outlets [ i ]
154- if ( outlet !== main ) {
155- insertContentAt ( outlet , outlet . content )
156- }
157- }
158- // finally insert the main content
159- if ( main ) {
160- insertContentAt ( main , _ . toArray ( raw . childNodes ) )
161- }
162- }
163-
164- /**
165- * Get <content> outlets from the element/list
166- *
167- * @param {Element|Array } el
168- * @return {Array }
169- */
170-
171- var concat = [ ] . concat
172- function getOutlets ( el ) {
173- return _ . isArray ( el )
174- ? concat . apply ( [ ] , el . map ( getOutlets ) )
175- : el . querySelectorAll
176- ? _ . toArray ( el . querySelectorAll ( 'content' ) )
177- : [ ]
178- }
179-
180- /**
181- * Insert an array of nodes at outlet,
182- * then remove the outlet.
183- *
184- * @param {Element } outlet
185- * @param {Array } contents
186- */
187-
188- function insertContentAt ( outlet , contents ) {
189- // not using util DOM methods here because
190- // parentNode can be cached
191- var parent = outlet . parentNode
192- for ( var i = 0 , j = contents . length ; i < j ; i ++ ) {
193- parent . insertBefore ( contents [ i ] , outlet )
194- }
195- parent . removeChild ( outlet )
196- }
197-
19883/**
19984 * Helper to extract a component container's attribute names
200- * into a map. The resulting map will be used in compiler to
201- * determine whether an attribute is transcluded.
85+ * into a map.
20286 *
20387 * @param {Element } el
20488 * @return {Object }
0 commit comments