@@ -177,7 +177,6 @@ function Compiler (vm, options) {
177177 }
178178
179179 // done!
180- compiler . rawContent = null
181180 compiler . init = false
182181
183182 // post compile / ready hook
@@ -197,14 +196,16 @@ CompilerProto.setupElement = function (options) {
197196 : options . el || document . createElement ( options . tagName || 'div' )
198197
199198 var template = options . template ,
200- child , frag , replacer , i , attr , attrs
199+ child , replacer , i , attr , attrs
201200
202201 if ( template ) {
203202 // collect anything already in there
204- /* jshint boss: true */
205- frag = this . rawContent = document . createDocumentFragment ( )
206- while ( child = el . firstChild ) {
207- frag . appendChild ( child )
203+ if ( el . hasChildNodes ( ) ) {
204+ this . rawContent = document . createElement ( 'div' )
205+ /* jshint boss: true */
206+ while ( child = el . firstChild ) {
207+ this . rawContent . appendChild ( child )
208+ }
208209 }
209210 // replace option: use the first node in
210211 // the template directly
@@ -227,6 +228,8 @@ CompilerProto.setupElement = function (options) {
227228 } else {
228229 el . appendChild ( template . cloneNode ( true ) )
229230 }
231+
232+ this . setupContent ( el )
230233 }
231234
232235 // apply element options
@@ -242,6 +245,59 @@ CompilerProto.setupElement = function (options) {
242245 return el
243246}
244247
248+ /**
249+ * Deal with <content> insertion points
250+ * per the Web Components spec
251+ */
252+ CompilerProto . setupContent = function ( el ) {
253+
254+ var outlets = slice . call ( el . getElementsByTagName ( 'content' ) ) ,
255+ raw = this . rawContent ,
256+ outlet , select , i , j , main
257+
258+ i = outlets . length
259+ if ( i ) {
260+ // first pass, collect corresponding content
261+ // for each outlet.
262+ while ( i -- ) {
263+ outlet = outlets [ i ]
264+ if ( raw ) {
265+ select = outlet . getAttribute ( 'select' )
266+ if ( select ) { // select content
267+ outlet . content =
268+ slice . call ( raw . querySelectorAll ( select ) )
269+ } else { // default content
270+ main = outlet
271+ }
272+ } else { // fallback content
273+ outlet . content =
274+ slice . call ( outlet . childNodes )
275+ }
276+ }
277+ // second pass, actually insert the contents
278+ for ( i = 0 , j = outlets . length ; i < j ; i ++ ) {
279+ outlet = outlets [ i ]
280+ if ( outlet === main ) continue
281+ insert ( outlet , outlet . content )
282+ }
283+ // finally insert the main content
284+ if ( raw ) {
285+ insert ( main , slice . call ( raw . childNodes ) )
286+ }
287+ }
288+
289+ function insert ( outlet , contents ) {
290+ var parent = outlet . parentNode ,
291+ i = 0 , j = contents . length
292+ for ( ; i < j ; i ++ ) {
293+ parent . insertBefore ( contents [ i ] , outlet )
294+ }
295+ parent . removeChild ( outlet )
296+ }
297+
298+ this . rawContent = null
299+ }
300+
245301/**
246302 * Setup observer.
247303 * The observer listens for get/set/mutate events on all VM
0 commit comments