1
1
var _ = require ( '../util' )
2
2
var config = require ( '../config' )
3
3
var templateParser = require ( '../parsers/template' )
4
- var transcludedFlagAttr = '__vue__transcluded'
5
4
6
5
/**
7
6
* Process an element or a DocumentFragment based on a
@@ -24,26 +23,6 @@ module.exports = function transclude (el, options) {
24
23
if ( options ) {
25
24
options . _containerAttrs = extractAttrs ( el )
26
25
}
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
- }
47
26
// for template tags, what we want is its content as
48
27
// a documentFragment (for block instances)
49
28
if ( el . tagName === 'TEMPLATE' ) {
@@ -77,7 +56,7 @@ function transcludeTemplate (el, options) {
77
56
if ( ! frag ) {
78
57
_ . warn ( 'Invalid template option: ' + template )
79
58
} else {
80
- var rawContent = options . _content || _ . extractContent ( el )
59
+ options . _content = _ . extractContent ( el )
81
60
var replacer = frag . firstChild
82
61
if ( options . replace ) {
83
62
if (
@@ -88,117 +67,22 @@ function transcludeTemplate (el, options) {
88
67
// block instance. (#835)
89
68
replacer . hasAttribute ( config . prefix + 'repeat' )
90
69
) {
91
- transcludeContent ( frag , rawContent )
92
70
return frag
93
71
} else {
94
72
options . _replacerAttrs = extractAttrs ( replacer )
95
73
mergeAttrs ( el , replacer )
96
- transcludeContent ( replacer , rawContent )
97
74
return replacer
98
75
}
99
76
} else {
100
77
el . appendChild ( frag )
101
- transcludeContent ( el , rawContent )
102
78
return el
103
79
}
104
80
}
105
81
}
106
82
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
-
198
83
/**
199
84
* 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.
202
86
*
203
87
* @param {Element } el
204
88
* @return {Object }
0 commit comments