@@ -18,40 +18,51 @@ module.exports = {
18
18
var raw = contentOwner . $options . _content
19
19
var content
20
20
if ( ! raw ) {
21
- // fallback content
22
- // extract as a fragment
23
- content = _ . extractContent ( this . el , true )
24
- this . compile ( content , vm )
21
+ this . fallback ( )
25
22
return
26
23
}
27
24
var parent = contentOwner . $parent
28
25
var selector = this . el . getAttribute ( 'select' )
29
26
if ( ! selector ) {
30
- // default content
31
- // Importent: clone the rawContent before extracting
32
- // content because the <content> may be inside a v-if
33
- // and need to be compiled more than once.
34
- content = _ . extractContent ( raw . cloneNode ( true ) , true )
35
- this . compile ( content , parent , vm )
27
+ // Default content
28
+ var self = this
29
+ var compileDefaultContent = function ( ) {
30
+ self . compile (
31
+ extractFragment ( raw . childNodes , raw , true ) ,
32
+ contentOwner . $parent ,
33
+ vm
34
+ )
35
+ }
36
+ if ( ! contentOwner . _isCompiled ) {
37
+ // defer until the end of instance compilation,
38
+ // because the default outlet must wait until all
39
+ // other possible outlets with selectors have picked
40
+ // out their contents.
41
+ contentOwner . $once ( 'hook:compiled' , compileDefaultContent )
42
+ } else {
43
+ compileDefaultContent ( )
44
+ }
36
45
} else {
37
46
// select content
38
47
selector = vm . $interpolate ( selector )
39
48
var nodes = raw . querySelectorAll ( selector )
40
49
if ( nodes . length ) {
41
- content = document . createDocumentFragment ( )
42
- for ( var i = 0 , l = nodes . length ; i < l ; i ++ ) {
43
- // only allow top-level select
44
- if ( nodes [ i ] . parentNode === raw ) {
45
- content . appendChild ( nodes [ i ] . cloneNode ( true ) )
46
- }
47
- }
50
+ content = extractFragment ( nodes , raw )
48
51
if ( content . hasChildNodes ( ) ) {
49
52
this . compile ( content , parent , vm )
53
+ } else {
54
+ this . fallback ( )
50
55
}
56
+ } else {
57
+ this . fallback ( )
51
58
}
52
59
}
53
60
} ,
54
61
62
+ fallback : function ( ) {
63
+ this . compile ( _ . extractContent ( this . el , true ) , this . vm )
64
+ } ,
65
+
55
66
compile : function ( content , owner , host ) {
56
67
if ( content && owner ) {
57
68
this . unlink = owner . $compile ( content , host )
@@ -68,5 +79,33 @@ module.exports = {
68
79
this . unlink ( )
69
80
}
70
81
}
82
+ }
83
+
84
+ /**
85
+ * Extract qualified content nodes from a node list.
86
+ *
87
+ * @param {NodeList } nodes
88
+ * @param {Element } parent
89
+ * @param {Boolean } main
90
+ * @return {DocumentFragment }
91
+ */
71
92
93
+ function extractFragment ( nodes , parent , main ) {
94
+ var frag = document . createDocumentFragment ( )
95
+ for ( var i = 0 , l = nodes . length ; i < l ; i ++ ) {
96
+ var node = nodes [ i ]
97
+ // if this is the main outlet, we want to skip all
98
+ // previously selected nodes;
99
+ // otherwise, we want to mark the node as selected.
100
+ // clone the node so the original raw content remains
101
+ // intact. this ensures proper re-compilation in cases
102
+ // where the outlet is inside a conditional block
103
+ if ( main && ! node . selected ) {
104
+ frag . appendChild ( node . cloneNode ( true ) )
105
+ } else if ( ! main && node . parentNode === parent ) {
106
+ node . selected = true
107
+ frag . appendChild ( node . cloneNode ( true ) )
108
+ }
109
+ }
110
+ return frag
72
111
}
0 commit comments