|
75 | 75 | parseAnnotations: function(template) { |
76 | 76 | var list = []; |
77 | 77 | var content = template._content || template.content; |
78 | | - this._parseNodeAnnotations(content, list); |
| 78 | + this._parseNodeAnnotations(content, list, |
| 79 | + template.hasAttribute('strip-whitespace')); |
79 | 80 | return list; |
80 | 81 | }, |
81 | 82 |
|
82 | 83 | // add annotations gleaned from subtree at `node` to `list` |
83 | | - _parseNodeAnnotations: function(node, list) { |
| 84 | + _parseNodeAnnotations: function(node, list, stripWhiteSpace) { |
84 | 85 | return node.nodeType === Node.TEXT_NODE ? |
85 | 86 | this._parseTextNodeAnnotation(node, list) : |
86 | 87 | // TODO(sjmiles): are there other nodes we may encounter |
87 | 88 | // that are not TEXT_NODE but also not ELEMENT? |
88 | | - this._parseElementAnnotations(node, list); |
| 89 | + this._parseElementAnnotations(node, list, stripWhiteSpace); |
89 | 90 | }, |
90 | 91 |
|
91 | 92 | _bindingRegex: /([^{[]*)({{|\[\[)([^}\]]*)(?:]]|}})/g, |
|
181 | 182 | }, |
182 | 183 |
|
183 | 184 | // add annotations gleaned from Element `node` to `list` |
184 | | - _parseElementAnnotations: function(element, list) { |
| 185 | + _parseElementAnnotations: function(element, list, stripWhiteSpace) { |
185 | 186 | var annote = { |
186 | 187 | bindings: [], |
187 | 188 | events: [] |
188 | 189 | }; |
189 | 190 | if (element.localName === 'content') { |
190 | 191 | list._hasContent = true; |
191 | 192 | } |
192 | | - this._parseChildNodesAnnotations(element, annote, list); |
| 193 | + this._parseChildNodesAnnotations(element, annote, list, stripWhiteSpace); |
193 | 194 | // TODO(sjmiles): is this for non-ELEMENT nodes? If so, we should |
194 | 195 | // change the contract of this method, or filter these out above. |
195 | 196 | if (element.attributes) { |
|
210 | 211 |
|
211 | 212 | // add annotations gleaned from children of `root` to `list`, `root`'s |
212 | 213 | // `annote` is supplied as it is the annote.parent of added annotations |
213 | | - _parseChildNodesAnnotations: function(root, annote, list, callback) { |
| 214 | + _parseChildNodesAnnotations: function(root, annote, list, stripWhiteSpace) { |
214 | 215 | if (root.firstChild) { |
215 | | - for (var i=0, node=root.firstChild; node; node=node.nextSibling, i++) { |
| 216 | + var node = root.firstChild; |
| 217 | + var i = 0; |
| 218 | + while (node) { |
| 219 | + var next = node.nextSibling; |
216 | 220 | if (node.localName === 'template' && |
217 | 221 | !node.hasAttribute('preserve-content')) { |
218 | 222 | this._parseTemplate(node, i, list, annote); |
|
222 | 226 | // note that root.normalize() should work but does not so we do this |
223 | 227 | // manually. |
224 | 228 | if (node.nodeType === Node.TEXT_NODE) { |
225 | | - var n = node.nextSibling; |
| 229 | + var n = next; |
226 | 230 | while (n && (n.nodeType === Node.TEXT_NODE)) { |
227 | 231 | node.textContent += n.textContent; |
| 232 | + next = n.nextSibling; |
228 | 233 | root.removeChild(n); |
229 | | - n = n.nextSibling; |
| 234 | + n = next; |
| 235 | + } |
| 236 | + // optionally strip whitespace |
| 237 | + if (stripWhiteSpace && !node.textContent.trim()) { |
| 238 | + root.removeChild(node); |
230 | 239 | } |
231 | 240 | } |
232 | | - var childAnnotation = this._parseNodeAnnotations(node, list, callback); |
233 | | - if (childAnnotation) { |
234 | | - childAnnotation.parent = annote; |
235 | | - childAnnotation.index = i; |
| 241 | + // if this node didn't get evacipated, parse it. |
| 242 | + if (node.parentNode) { |
| 243 | + var childAnnotation = this._parseNodeAnnotations(node, list, |
| 244 | + stripWhiteSpace); |
| 245 | + if (childAnnotation) { |
| 246 | + childAnnotation.parent = annote; |
| 247 | + childAnnotation.index = i; |
| 248 | + } |
236 | 249 | } |
| 250 | + node = next; |
| 251 | + i++; |
237 | 252 | } |
238 | 253 | } |
239 | 254 | }, |
|
0 commit comments