Skip to content

Commit b2117dc

Browse files
committed
Factor into functions.
1 parent 758c483 commit b2117dc

2 files changed

Lines changed: 57 additions & 44 deletions

File tree

‎src/lib/annotations/annotations.html‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,23 @@
150150
}
151151
},
152152

153+
_literalFromParts: function(parts) {
154+
var s = '';
155+
for (var i=0; i<parts.length; i++) {
156+
var literal = parts[i].literal;
157+
s += literal || '';
158+
}
159+
return s;
160+
},
161+
153162
// add annotations gleaned from TextNode `node` to `list`
154163
_parseTextNodeAnnotation: function(node, list) {
155164
var parts = this._parseBindings(node.textContent);
156165
if (parts) {
157166
// Initialize the textContent with any literal parts
158167
// NOTE: default to a space here so the textNode remains; some browsers
159168
// (IE) evacipate an empty textNode following cloneNode/importNode.
160-
node.textContent = parts.map(function(part) {
161-
return part.literal;
162-
}).join('') || ' ';
169+
node.textContent = this._literalFromParts(parts) || ' ';
163170
var annote = {
164171
bindings: [{
165172
kind: 'text',
@@ -295,9 +302,7 @@
295302
kind = 'attribute';
296303
}
297304
// Initialize attribute bindings with any literal parts
298-
var literal = parts.map(function(part) {
299-
return part.literal;
300-
}).join('');
305+
var literal = this._literalFromParts(parts);
301306
if (kind == 'attribute') {
302307
node.setAttribute(name, literal);
303308
}

‎src/standard/annotations.html‎

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -225,44 +225,52 @@
225225
for (var i=0; i<notes.length; i++) {
226226
var note = notes[i];
227227
var node = nodes[i];
228-
// nested template contents have been stored prototypically to avoid
229-
// unnecessary duplication, here we put references to the
230-
// indirected contents onto the nested template instances
231-
if (note.templateContent) {
232-
// note: we can rely on _nodes being set here and having the same
233-
// index as _notes
234-
this._nodes[i]._content = note.templateContent;
235-
}
236-
// Compound bindings utilize private storage on the node to store
237-
// the current state of each value that will be concatenated to generate
238-
// the final property/attribute/text value
239-
// Here we initialize the private storage array on the node with any
240-
// literal parts that won't change (could get fancy and use WeakMap),
241-
// and configure property bindings to children with the literal parts
242-
// (textContent and annotations were already initialized in the template)
243-
var bindings = note.bindings;
244-
for (var j=0; j<bindings.length; j++) {
245-
var binding = bindings[j];
246-
if (binding.isCompound) {
247-
// Create compound storage map
248-
var storage = node.__compoundStorage__ ||
249-
(node.__compoundStorage__ = {});
250-
var parts = binding.parts;
251-
// Copy literals from parts into storage for this binding
252-
var literals = new Array(parts.length);
253-
for (var k=0; k<parts.length; k++) {
254-
literals[k] = parts[k].literal;
255-
}
256-
var name = binding.name;
257-
storage[name] = literals;
258-
// Configure properties with their literal parts
259-
if (binding.kind == 'property') {
260-
var literal = literals.join('');
261-
if (node._configValue) {
262-
node._configValue(name, literal);
263-
} else {
264-
node[name] = literal;
265-
}
228+
this._configureTemplateContent(note, node);
229+
this._configureCompoundBindings(note, node);
230+
}
231+
},
232+
233+
// nested template contents have been stored prototypically to avoid
234+
// unnecessary duplication, here we put references to the
235+
// indirected contents onto the nested template instances
236+
_configureTemplateContent: function(note, node) {
237+
if (note.templateContent) {
238+
// note: we can rely on _nodes being set here and having the same
239+
// index as _notes
240+
node._content = note.templateContent;
241+
}
242+
},
243+
244+
// Compound bindings utilize private storage on the node to store
245+
// the current state of each value that will be concatenated to generate
246+
// the final property/attribute/text value
247+
// Here we initialize the private storage array on the node with any
248+
// literal parts that won't change (could get fancy and use WeakMap),
249+
// and configure property bindings to children with the literal parts
250+
// (textContent and annotations were already initialized in the template)
251+
_configureCompoundBindings: function(note, node) {
252+
var bindings = note.bindings;
253+
for (var i=0; i<bindings.length; i++) {
254+
var binding = bindings[i];
255+
if (binding.isCompound) {
256+
// Create compound storage map
257+
var storage = node.__compoundStorage__ ||
258+
(node.__compoundStorage__ = {});
259+
var parts = binding.parts;
260+
// Copy literals from parts into storage for this binding
261+
var literals = new Array(parts.length);
262+
for (var j=0; j<parts.length; j++) {
263+
literals[j] = parts[j].literal;
264+
}
265+
var name = binding.name;
266+
storage[name] = literals;
267+
// Configure properties with their literal parts
268+
if (binding.kind == 'property') {
269+
var literal = literals.join('');
270+
if (node._configValue) {
271+
node._configValue(name, literal);
272+
} else {
273+
node[name] = literal;
266274
}
267275
}
268276
}

0 commit comments

Comments
 (0)