Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Unified Diff: chrome/third_party/jstemplate/jstemplate.js

Issue 119384: Update JSTemplate to the latest version from Google Code (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/third_party/jstemplate/jstemplate.js
===================================================================
--- chrome/third_party/jstemplate/jstemplate.js (revision 17821)
+++ chrome/third_party/jstemplate/jstemplate.js (working copy)
@@ -1,5 +1,19 @@
-// Copyright 2006 Google Inc. All rights reserved.
+// Copyright 2006 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+// implied. See the License for the specific language governing
+// permissions and limitations under the License.
/**
+ * Author: Steffen Meschkat <mesch@google.com>
+ *
* @fileoverview A simple formatter to project JavaScript data into
* HTML templates. The template is edited in place. I.e. in order to
* instantiate a template, clone it from the DOM first, and then
@@ -7,9 +21,9 @@
* If the templates is processed again, changed values are merely
* updated.
*
- * NOTE: IE DOM doesn't have importNode().
+ * NOTE(mesch): IE DOM doesn't have importNode().
*
- * NOTE: The property name "length" must not be used in input
+ * NOTE(mesch): The property name "length" must not be used in input
* data, see comment in jstSelect_().
*/
@@ -23,373 +37,560 @@
var ATT_instance = 'jsinstance';
var ATT_display = 'jsdisplay';
var ATT_values = 'jsvalues';
+var ATT_vars = 'jsvars';
var ATT_eval = 'jseval';
var ATT_transclude = 'transclude';
var ATT_content = 'jscontent';
+var ATT_skip = 'jsskip';
/**
- * Names of special variables defined by the jstemplate evaluation
- * context. These can be used in js expression in jstemplate
- * attributes.
+ * Name of the attribute that caches a reference to the parsed
+ * template processing attribute values on a template node.
*/
-var VAR_index = '$index';
-var VAR_this = '$this';
+var ATT_jstcache = 'jstcache';
/**
- * Context for processing a jstemplate. The context contains a context
- * object, whose properties can be referred to in jstemplate
- * expressions, and it holds the locally defined variables.
+ * Name of the property that caches the parsed template processing
+ * attribute values on a template node.
+ */
+var PROP_jstcache = '__jstcache';
+
+
+/**
+ * ID of the element that contains dynamically loaded jstemplates.
+ */
+var STRING_jsts = 'jsts';
+
+
+/**
+ * Un-inlined string literals, to avoid object creation in
+ * IE6.
+ */
+var CHAR_asterisk = '*';
+var CHAR_dollar = '$';
+var CHAR_period = '.';
+var CHAR_ampersand = '&';
+var STRING_div = 'div';
+var STRING_id = 'id';
+var STRING_asteriskzero = '*0';
+var STRING_zero = '0';
+
+
+/**
+ * HTML template processor. Data values are bound to HTML templates
+ * using the attributes transclude, jsselect, jsdisplay, jscontent,
+ * jsvalues. The template is modifed in place. The values of those
+ * attributes are JavaScript expressions that are evaluated in the
+ * context of the data object fragment.
*
- * @param {Object} opt_data The context object. Null if no context.
+ * @param {JsEvalContext} context Context created from the input data
+ * object.
*
- * @param {Object} opt_parent The parent context, from which local
- * variables are inherited. Normally the context object of the parent
- * context is the object whose property the parent object is. Null for the
- * context of the root object.
+ * @param {Element} template DOM node of the template. This will be
+ * processed in place. After processing, it will still be a valid
+ * template that, if processed again with the same data, will remain
+ * unchanged.
*
- * @constructor
+ * @param {boolean} opt_debugging Optional flag to collect debugging
+ * information while processing the template. Only takes effect
+ * in MAPS_DEBUG.
*/
-function JsExprContext(opt_data, opt_parent) {
- var me = this;
+function jstProcess(context, template, opt_debugging) {
+ var processor = new JstProcessor;
+ JstProcessor.prepareTemplate_(template);
/**
- * The local context of the input data in which the jstemplate
- * expressions are evaluated. Notice that this is usually an Object,
- * but it can also be a scalar value (and then still the expression
- * $this can be used to refer to it). Notice this can be a scalar
- * value, including undefined.
- *
- * @type {Object}
+ * Caches the document of the template node, so we don't have to
+ * access it through ownerDocument.
+ * @type Document
*/
- me.data_ = opt_data;
+ processor.document_ = ownerDocument(template);
- /**
- * The context for variable definitions in which the jstemplate
- * expressions are evaluated. Other than for the local context,
- * which replaces the parent context, variable definitions of the
- * parent are inherited. The special variable $this points to data_.
- *
- * @type {Object}
- */
- me.vars_ = {};
- if (opt_parent) {
- // If there is a parent node, inherit local variables from the
- // parent.
- copyProperties(me.vars_, opt_parent.vars_);
- }
- // Add the current context object as a special variable $this
- // so it is possible to use it in expressions.
- this.vars_[VAR_this] = me.data_;
+ processor.run_(bindFully(processor, processor.jstProcessOuter_,
+ context, template));
}
/**
- * Evaluates the given expression in the context of the current
- * context object and the current local variables.
- *
- * @param {String} expr A javascript expression.
- *
- * @param {Element} template DOM node of the template.
- *
- * @return The value of that expression.
+ * Internal class used by jstemplates to maintain context. This is
+ * necessary to process deep templates in Safari which has a
+ * relatively shallow maximum recursion depth of 100.
+ * @class
+ * @constructor
*/
-JsExprContext.prototype.jseval = function(expr, template) {
- with (this.vars_) {
- with (this.data_) {
- try {
- // NOTE: Since eval is a built-in function, calling
- // eval.call(template, ...) does not set 'this' to template.
- return (function() {
- return eval('[' + expr + '][0]');
- }).call(template);
- } catch (e) {
- return null;
- }
- }
- }
+function JstProcessor() {
}
/**
- * Clones the current context for a new context object. The cloned
- * context has the data object as its context object and the current
- * context as its parent context. It also sets the $index variable to
- * the given value. This value usually is the position of the data
- * object in a list for which a template is instantiated multiply.
- *
- * @param {Object} data The new context object.
- *
- * @param {Number} index Position of the new context when multiply
- * instantiated. (See implementation of jstSelect().)
- *
- * @return {JsExprContext}
+ * Counter to generate node ids. These ids will be stored in
+ * ATT_jstcache and be used to lookup the preprocessed js attributes
+ * from the jstcache_. The id is stored in an attribute so it
+ * suvives cloneNode() and thus cloned template nodes can share the
+ * same cache entry.
+ * @type number
*/
-JsExprContext.prototype.clone = function(data, index) {
- var ret = new JsExprContext(data, this);
- ret.setVariable(VAR_index, index);
- if (this.resolver_) {
- ret.setSubTemplateResolver(this.resolver_);
- }
- return ret;
-}
+JstProcessor.jstid_ = 0;
/**
- * Binds a local variable to the given value. If set from jstemplate
- * jsvalue expressions, variable names must start with $, but in the
- * API they only have to be valid javascript identifier.
- *
- * @param {String} name
- *
- * @param {Object} value
+ * Map from jstid to processed js attributes.
+ * @type Object
*/
-JsExprContext.prototype.setVariable = function(name, value) {
- this.vars_[name] = value;
-}
+JstProcessor.jstcache_ = {};
+/**
+ * The neutral cache entry. Used for all nodes that don't have any
+ * jst attributes. We still set the jsid attribute on those nodes so
+ * we can avoid to look again for all the other jst attributes that
+ * aren't there. Remember: not only the processing of the js
+ * attribute values is expensive and we thus want to cache it. The
+ * access to the attributes on the Node in the first place is
+ * expensive too.
+ */
+JstProcessor.jstcache_[0] = {};
+
/**
- * Sets the function used to resolve the values of the transclude
- * attribute into DOM nodes. By default, this is jstGetTemplate(). The
- * value set here is inherited by clones of this context.
- *
- * @param {Function} resolver The function used to resolve transclude
- * ids into a DOM node of a subtemplate. The DOM node returned by this
- * function will be inserted into the template instance being
- * processed. Thus, the resolver function must instantiate the
- * subtemplate as necessary.
+ * Map from concatenated attribute string to jstid.
+ * The key is the concatenation of all jst atributes found on a node
+ * formatted as "name1=value1&name2=value2&...", in the order defined by
+ * JST_ATTRIBUTES. The value is the id of the jstcache_ entry that can
+ * be used for this node. This allows the reuse of cache entries in cases
+ * when a cached entry already exists for a given combination of attribute
+ * values. (For example when two different nodes in a template share the same
+ * JST attributes.)
+ * @type Object
*/
-JsExprContext.prototype.setSubTemplateResolver = function(resolver) {
- this.resolver_ = resolver;
-}
+JstProcessor.jstcacheattributes_ = {};
/**
- * Resolves a sub template from an id. Used to process the transclude
- * attribute. If a resolver function was set using
- * setSubTemplateResolver(), it will be used, otherwise
- * jstGetTemplate().
+ * Map for storing temporary attribute values in prepareNode_() so they don't
+ * have to be retrieved twice. (IE6 perf)
+ * @type Object
+ */
+JstProcessor.attributeValues_ = {};
+
+
+/**
+ * A list for storing non-empty attributes found on a node in prepareNode_().
+ * The array is global since it can be reused - this way there is no need to
+ * construct a new array object for each invocation. (IE6 perf)
+ * @type Array
+ */
+JstProcessor.attributeList_ = [];
+
+
+/**
+ * Prepares the template: preprocesses all jstemplate attributes.
*
- * @param {String} id The id of the sub template.
- *
- * @return {Node} The root DOM node of the sub template, for direct
- * insertion into the currently processed template instance.
+ * @param {Element} template
*/
-JsExprContext.prototype.getSubTemplate = function(id) {
- return (this.resolver_ || jstGetTemplate).call(this, id);
-}
+JstProcessor.prepareTemplate_ = function(template) {
+ if (!template[PROP_jstcache]) {
+ domTraverseElements(template, function(node) {
+ JstProcessor.prepareNode_(node);
+ });
+ }
+};
/**
- * HTML template processor. Data values are bound to HTML templates
- * using the attributes transclude, jsselect, jsdisplay, jscontent,
- * jsvalues. The template is modifed in place. The values of those
- * attributes are JavaScript expressions that are evaluated in the
- * context of the data object fragment.
+ * A list of attributes we use to specify jst processing instructions,
+ * and the functions used to parse their values.
*
- * @param {JsExprContext} context Context created from the input data
- * object.
- *
- * @param {Element} template DOM node of the template. This will be
- * processed in place. After processing, it will still be a valid
- * template that, if processed again with the same data, will remain
- * unchanged.
+ * @type Array.<Array>
*/
-function jstProcess(context, template) {
- var processor = new JstProcessor();
- processor.run_([ processor, processor.jstProcess_, context, template ]);
-}
+var JST_ATTRIBUTES = [
+ [ ATT_select, jsEvalToFunction ],
+ [ ATT_display, jsEvalToFunction ],
+ [ ATT_values, jsEvalToValues ],
+ [ ATT_vars, jsEvalToValues ],
+ [ ATT_eval, jsEvalToExpressions ],
+ [ ATT_transclude, jsEvalToSelf ],
+ [ ATT_content, jsEvalToFunction ],
+ [ ATT_skip, jsEvalToFunction ]
+];
/**
- * Internal class used by jstemplates to maintain context.
- * NOTE: This is necessary to process deep templates in Safari
- * which has a relatively shallow stack.
- * @class
+ * Prepares a single node: preprocesses all template attributes of the
+ * node, and if there are any, assigns a jsid attribute and stores the
+ * preprocessed attributes under the jsid in the jstcache.
+ *
+ * @param {Element} node
+ *
+ * @return {Object} The jstcache entry. The processed jst attributes
+ * are properties of this object. If the node has no jst attributes,
+ * returns an object with no properties (the jscache_[0] entry).
*/
-function JstProcessor() {
-}
+JstProcessor.prepareNode_ = function(node) {
+ // If the node already has a cache property, return it.
+ if (node[PROP_jstcache]) {
+ return node[PROP_jstcache];
+ }
+ // If it is not found, we always set the PROP_jstcache property on the node.
+ // Accessing the property is faster than executing getAttribute(). If we
+ // don't find the property on a node that was cloned in jstSelect_(), we
+ // will fall back to check for the attribute and set the property
+ // from cache.
+ // If the node has an attribute indexing a cache object, set it as a property
+ // and return it.
+ var jstid = domGetAttribute(node, ATT_jstcache);
+ if (jstid != null) {
+ return node[PROP_jstcache] = JstProcessor.jstcache_[jstid];
+ }
+
+ var attributeValues = JstProcessor.attributeValues_;
+ var attributeList = JstProcessor.attributeList_;
+ attributeList.length = 0;
+
+ // Look for interesting attributes.
+ for (var i = 0, I = jsLength(JST_ATTRIBUTES); i < I; ++i) {
+ var name = JST_ATTRIBUTES[i][0];
+ var value = domGetAttribute(node, name);
+ attributeValues[name] = value;
+ if (value != null) {
+ attributeList.push(name + "=" + value);
+ }
+ }
+
+ // If none found, mark this node to prevent further inspection, and return
+ // an empty cache object.
+ if (attributeList.length == 0) {
+ domSetAttribute(node, ATT_jstcache, STRING_zero);
+ return node[PROP_jstcache] = JstProcessor.jstcache_[0];
+ }
+
+ // If we already have a cache object corresponding to these attributes,
+ // annotate the node with it, and return it.
+ var attstring = attributeList.join(CHAR_ampersand);
+ if (jstid = JstProcessor.jstcacheattributes_[attstring]) {
+ domSetAttribute(node, ATT_jstcache, jstid);
+ return node[PROP_jstcache] = JstProcessor.jstcache_[jstid];
+ }
+
+ // Otherwise, build a new cache object.
+ var jstcache = {};
+ for (var i = 0, I = jsLength(JST_ATTRIBUTES); i < I; ++i) {
+ var att = JST_ATTRIBUTES[i];
+ var name = att[0];
+ var parse = att[1];
+ var value = attributeValues[name];
+ if (value != null) {
+ jstcache[name] = parse(value);
+ }
+ }
+
+ jstid = STRING_empty + ++JstProcessor.jstid_;
+ domSetAttribute(node, ATT_jstcache, jstid);
+ JstProcessor.jstcache_[jstid] = jstcache;
+ JstProcessor.jstcacheattributes_[attstring] = jstid;
+
+ return node[PROP_jstcache] = jstcache;
+};
+
+
/**
- * Runs the state machine, beginning with function "start".
+ * Runs the given function in our state machine.
*
- * @param {Array} start The first function to run, in the form
- * [object, method, args ...]
+ * It's informative to view the set of all function calls as a tree:
+ * - nodes are states
+ * - edges are state transitions, implemented as calls to the pending
+ * functions in the stack.
+ * - pre-order function calls are downward edges (recursion into call).
+ * - post-order function calls are upward edges (return from call).
+ * - leaves are nodes which do not recurse.
+ * We represent the call tree as an array of array of calls, indexed as
+ * stack[depth][index]. Here [depth] indexes into the call stack, and
+ * [index] indexes into the call queue at that depth. We require a call
+ * queue so that a node may branch to more than one child
+ * (which will be called serially), typically due to a loop structure.
+ *
+ * @param {Function} f The first function to run.
*/
-JstProcessor.prototype.run_ = function(start) {
+JstProcessor.prototype.run_ = function(f) {
var me = this;
- me.queue_ = [ start ];
- while (jsLength(me.queue_)) {
- var f = me.queue_.shift();
- f[1].apply(f[0], f.slice(2));
+ /**
+ * A stack of queues of pre-order calls.
+ * The inner arrays (constituent queues) are structured as
+ * [ arg2, arg1, method, arg2, arg1, method, ...]
+ * ie. a flattened array of methods with 2 arguments, in reverse order
+ * for efficient push/pop.
+ *
+ * The outer array is a stack of such queues.
+ *
+ * @type Array.<Array>
+ */
+ var calls = me.calls_ = [];
+
+ /**
+ * The index into the queue for each depth. NOTE: Alternative would
+ * be to maintain the queues in reverse order (popping off of the
+ * end) but the repeated calls to .pop() consumed 90% of this
+ * function's execution time.
+ * @type Array.<number>
+ */
+ var queueIndices = me.queueIndices_ = [];
+
+ /**
+ * A pool of empty arrays. Minimizes object allocation for IE6's benefit.
+ * @type Array.<Array>
+ */
+ var arrayPool = me.arrayPool_ = [];
+
+ f();
+ var queue, queueIndex;
+ var method, arg1, arg2;
+ var temp;
+ while (calls.length) {
+ queue = calls[calls.length - 1];
+ queueIndex = queueIndices[queueIndices.length - 1];
+ if (queueIndex >= queue.length) {
+ me.recycleArray_(calls.pop());
+ queueIndices.pop();
+ continue;
+ }
+
+ // Run the first function in the queue.
+ method = queue[queueIndex++];
+ arg1 = queue[queueIndex++];
+ arg2 = queue[queueIndex++];
+ queueIndices[queueIndices.length - 1] = queueIndex;
+ method.call(me, arg1, arg2);
}
-}
+};
/**
- * Appends a function to be called later.
- * Analogous to calling that function on a subsequent line, or a subsequent
- * iteration of a loop.
+ * Pushes one or more functions onto the stack. These will be run in sequence,
+ * interspersed with any recursive calls that they make.
*
- * @param {Array} f A function in the form [object, method, args ...]
+ * This method takes ownership of the given array!
+ *
+ * @param {Array} args Array of method calls structured as
+ * [ method, arg1, arg2, method, arg1, arg2, ... ]
*/
-JstProcessor.prototype.enqueue_ = function(f) {
- this.queue_.push(f);
-}
+JstProcessor.prototype.push_ = function(args) {
+ this.calls_.push(args);
+ this.queueIndices_.push(0);
+};
/**
- * Implements internals of jstProcess.
+ * Enable/disable debugging.
+ * @param {boolean} debugging New state
+ */
+JstProcessor.prototype.setDebugging = function(debugging) {
+};
+
+
+JstProcessor.prototype.createArray_ = function() {
+ if (this.arrayPool_.length) {
+ return this.arrayPool_.pop();
+ } else {
+ return [];
+ }
+};
+
+
+JstProcessor.prototype.recycleArray_ = function(array) {
+ arrayClear(array);
+ this.arrayPool_.push(array);
+};
+
+/**
+ * Implements internals of jstProcess. This processes the two
+ * attributes transclude and jsselect, which replace or multiply
+ * elements, hence the name "outer". The remainder of the attributes
+ * is processed in jstProcessInner_(), below. That function
+ * jsProcessInner_() only processes attributes that affect an existing
+ * node, but doesn't create or destroy nodes, hence the name
+ * "inner". jstProcessInner_() is called through jstSelect_() if there
+ * is a jsselect attribute (possibly for newly created clones of the
+ * current template node), or directly from here if there is none.
*
- * @param {JsExprContext} context
+ * @param {JsEvalContext} context
*
* @param {Element} template
*/
-JstProcessor.prototype.jstProcess_ = function(context, template) {
+JstProcessor.prototype.jstProcessOuter_ = function(context, template) {
var me = this;
- var transclude = domGetAttribute(template, ATT_transclude);
+ var jstAttributes = me.jstAttributes_(template);
+
+ var transclude = jstAttributes[ATT_transclude];
if (transclude) {
- var tr = context.getSubTemplate(transclude);
+ var tr = jstGetTemplate(transclude);
if (tr) {
domReplaceChild(tr, template);
- me.enqueue_([ me, me.jstProcess_, context, tr ]);
+ var call = me.createArray_();
+ call.push(me.jstProcessOuter_, context, tr);
+ me.push_(call);
} else {
domRemoveNode(template);
}
return;
}
- var select = domGetAttribute(template, ATT_select);
+ var select = jstAttributes[ATT_select];
if (select) {
me.jstSelect_(context, template, select);
- return;
+ } else {
+ me.jstProcessInner_(context, template);
}
+};
- var display = domGetAttribute(template, ATT_display);
+
+/**
+ * Implements internals of jstProcess. This processes all attributes
+ * except transclude and jsselect. It is called either from
+ * jstSelect_() for nodes that have a jsselect attribute so that the
+ * jsselect attribute will not be processed again, or else directly
+ * from jstProcessOuter_(). See the comment on jstProcessOuter_() for
+ * an explanation of the name.
+ *
+ * @param {JsEvalContext} context
+ *
+ * @param {Element} template
+ */
+JstProcessor.prototype.jstProcessInner_ = function(context, template) {
+ var me = this;
+
+ var jstAttributes = me.jstAttributes_(template);
+
+ // NOTE(mesch): See NOTE on ATT_content why this is a separate
+ // attribute, and not a special value in ATT_values.
+ var display = jstAttributes[ATT_display];
if (display) {
- if (!context.jseval(display, template)) {
+ var shouldDisplay = context.jsexec(display, template);
+ if (!shouldDisplay) {
displayNone(template);
return;
}
-
displayDefault(template);
- // domRemoveAttribute(template, ATT_display);
}
- // NOTE: content is a separate attribute, instead of just a
- // special value mentioned in values, for two reasons: (1) it is
- // fairly common to have only mapped content, and writing
- // content="expr" is shorter than writing values="content:expr", and
- // (2) the presence of content actually terminates traversal, and we
- // need to check for that. Display is a separate attribute for a
- // reason similar to the second, in that its presence *may*
- // terminate traversal.
+ // NOTE(mesch): jsvars is evaluated before jsvalues, because it's
+ // more useful to be able to use var values in attribute value
+ // expressions than vice versa.
+ var values = jstAttributes[ATT_vars];
+ if (values) {
+ me.jstVars_(context, template, values);
+ }
- var values = domGetAttribute(template, ATT_values);
+ values = jstAttributes[ATT_values];
if (values) {
- // domRemoveAttribute(template, ATT_values);
me.jstValues_(context, template, values);
}
// Evaluate expressions immediately. Useful for hooking callbacks
// into jstemplates.
//
- // NOTE: Evaluation order is sometimes significant, e.g. in
- // map_html_addressbook_template.tpl, the expression evaluated in
- // jseval relies on the values set in jsvalues, so it needs to be
- // evaluated *after* jsvalues. TODO: This is quite arbitrary,
- // it would be better if this would have more necessity to it.
- var expressions = domGetAttribute(template, ATT_eval);
+ // NOTE(mesch): Evaluation order is sometimes significant, e.g. when
+ // the expression evaluated in jseval relies on the values set in
+ // jsvalues, so it needs to be evaluated *after*
+ // jsvalues. TODO(mesch): This is quite arbitrary, it would be
+ // better if this would have more necessity to it.
+ var expressions = jstAttributes[ATT_eval];
if (expressions) {
- // See NOTE at top of jstValues.
- foreach(expressions.split(/\s*;\s*/), function(expression) {
- expression = stringTrim(expression);
- if (jsLength(expression)) {
- context.jseval(expression, template);
- }
- });
+ for (var i = 0, I = jsLength(expressions); i < I; ++i) {
+ context.jsexec(expressions[i], template);
+ }
}
- var content = domGetAttribute(template, ATT_content);
+ var skip = jstAttributes[ATT_skip];
+ if (skip) {
+ var shouldSkip = context.jsexec(skip, template);
+ if (shouldSkip) return;
+ }
+
+ // NOTE(mesch): content is a separate attribute, instead of just a
+ // special value mentioned in values, for two reasons: (1) it is
+ // fairly common to have only mapped content, and writing
+ // content="expr" is shorter than writing values="content:expr", and
+ // (2) the presence of content actually terminates traversal, and we
+ // need to check for that. Display is a separate attribute for a
+ // reason similar to the second, in that its presence *may*
+ // terminate traversal.
+ var content = jstAttributes[ATT_content];
if (content) {
- // domRemoveAttribute(template, ATT_content);
me.jstContent_(context, template, content);
} else {
- var childnodes = [];
- // NOTE: We enqueue the processing of each child via
- // enqueue_(), before processing any one of them. This means
- // that any newly generated children will be ignored (as desired).
- for (var i = 0; i < jsLength(template.childNodes); ++i) {
- if (template.childNodes[i].nodeType == DOM_ELEMENT_NODE) {
- me.enqueue_(
- [ me, me.jstProcess_, context, template.childNodes[i] ]);
+ // Newly generated children should be ignored, so we explicitly
+ // store the children to be processed.
+ var queue = me.createArray_();
+ for (var c = template.firstChild; c; c = c.nextSibling) {
+ if (c.nodeType == DOM_ELEMENT_NODE) {
+ queue.push(me.jstProcessOuter_, context, c);
}
}
+ if (queue.length) me.push_(queue);
}
-}
+};
/**
* Implements the jsselect attribute: evalutes the value of the
* jsselect attribute in the current context, with the current
- * variable bindings (see JsExprContext.jseval()). If the value is an
+ * variable bindings (see JsEvalContext.jseval()). If the value is an
* array, the current template node is multiplied once for every
* element in the array, with the array element being the context
* object. If the array is empty, or the value is undefined, then the
* current template node is dropped. If the value is not an array,
* then it is just made the context object.
*
- * @param {JsExprContext} context The current evaluation context.
+ * @param {JsEvalContext} context The current evaluation context.
*
* @param {Element} template The currently processed node of the template.
*
- * @param {String} select The javascript expression to evaluate.
+ * @param {Function} select The javascript expression to evaluate.
*
- * @param {Function} process The function to continue processing with.
+ * @notypecheck FIXME(hmitchell): See OCL6434950. instance and value need
+ * type checks.
*/
JstProcessor.prototype.jstSelect_ = function(context, template, select) {
var me = this;
- var value = context.jseval(select, template);
- domRemoveAttribute(template, ATT_select);
+ var value = context.jsexec(select, template);
// Enable reprocessing: if this template is reprocessed, then only
// fill the section instance here. Otherwise do the cardinal
// processing of a new template.
var instance = domGetAttribute(template, ATT_instance);
- var instance_last = false;
+
+ var instanceLast = false;
if (instance) {
- if (instance.charAt(0) == '*') {
+ if (instance.charAt(0) == CHAR_asterisk) {
instance = parseInt10(instance.substr(1));
- instance_last = true;
+ instanceLast = true;
} else {
- instance = parseInt10(instance);
+ instance = parseInt10(/** @type string */(instance));
}
}
- // NOTE: value instanceof Array is occasionally false for
+ // The expression value instanceof Array is occasionally false for
// arrays, seen in Firefox. Thus we recognize an array as an object
// which is not null that has a length property. Notice that this
// also matches input data with a length property, so this property
// name should be avoided in input data.
- var multiple = (value !== null &&
- typeof value == 'object' &&
- typeof value.length == 'number');
- var multiple_empty = (multiple && value.length == 0);
+ var multiple = isArray(value);
+ var count = multiple ? jsLength(value) : 1;
+ var multipleEmpty = (multiple && count == 0);
if (multiple) {
- if (multiple_empty) {
+ if (multipleEmpty) {
// For an empty array, keep the first template instance and mark
// it last. Remove all other template instances.
if (!instance) {
- domSetAttribute(template, ATT_select, select);
- domSetAttribute(template, ATT_instance, '*0');
+ domSetAttribute(template, ATT_instance, STRING_asteriskzero);
displayNone(template);
} else {
domRemoveNode(template);
@@ -403,97 +604,94 @@
// array. If the template is reprocessed, new template instances
// are only needed if there are more array values than template
// instances. Those additional instances are created by
- // replicating the last template instance. NOTE: If the
- // template is first processed, there is no jsinstance
+ // replicating the last template instance.
+ //
+ // When the template is first processed, there is no jsinstance
// attribute. This is indicated by instance === null, except in
// opera it is instance === "". Notice also that the === is
// essential, because 0 == "", presumably via type coercion to
- // boolean. For completeness, in case any browser returns
- // undefined and not null for getAttribute of nonexistent
- // attributes, we also test for === undefined.
- if (instance === null || instance === "" || instance === undefined ||
- (instance_last && instance < jsLength(value) - 1)) {
- var templatenodes = [];
- var instances_start = instance || 0;
- for (var i = instances_start + 1; i < jsLength(value); ++i) {
+ // boolean.
+ if (instance === null || instance === STRING_empty ||
+ (instanceLast && instance < count - 1)) {
+ // A queue of calls to push.
+ var queue = me.createArray_();
+
+ var instancesStart = instance || 0;
+ var i, I, clone;
+ for (i = instancesStart, I = count - 1; i < I; ++i) {
var node = domCloneNode(template);
- templatenodes.push(node);
domInsertBefore(node, template);
+
+ jstSetInstance(/** @type Element */(node), value, i);
+ clone = context.clone(value[i], i, count);
+
+ queue.push(me.jstProcessInner_, clone, node,
+ JsEvalContext.recycle, clone, null);
+
}
// Push the originally present template instance last to keep
// the order aligned with the DOM order, because the newly
// created template instances are inserted *before* the
// original instance.
- templatenodes.push(template);
-
- for (var i = 0; i < jsLength(templatenodes); ++i) {
- var ii = i + instances_start;
- var v = value[ii];
- var t = templatenodes[i];
-
- me.enqueue_([ me, me.jstProcess_, context.clone(v, ii), t ]);
- var instanceStr = (ii == jsLength(value) - 1 ? '*' : '') + ii;
- me.enqueue_(
- [ null, postProcessMultiple_, t, select, instanceStr ]);
- }
-
- } else if (instance < jsLength(value)) {
+ jstSetInstance(template, value, i);
+ clone = context.clone(value[i], i, count);
+ queue.push(me.jstProcessInner_, clone, template,
+ JsEvalContext.recycle, clone, null);
+ me.push_(queue);
+ } else if (instance < count) {
var v = value[instance];
- me.enqueue_(
- [me, me.jstProcess_, context.clone(v, instance), template]);
- var instanceStr = (instance == jsLength(value) - 1 ? '*' : '')
- + instance;
- me.enqueue_(
- [ null, postProcessMultiple_, template, select, instanceStr ]);
+ jstSetInstance(template, value, instance);
+ var clone = context.clone(v, instance, count);
+ var queue = me.createArray_();
+ queue.push(me.jstProcessInner_, clone, template,
+ JsEvalContext.recycle, clone, null);
+ me.push_(queue);
} else {
domRemoveNode(template);
}
}
} else {
if (value == null) {
- domSetAttribute(template, ATT_select, select);
displayNone(template);
} else {
- me.enqueue_(
- [ me, me.jstProcess_, context.clone(value, 0), template ]);
- me.enqueue_(
- [ null, postProcessSingle_, template, select ]);
+ displayDefault(template);
+ var clone = context.clone(value, 0, 1);
+ var queue = me.createArray_();
+ queue.push(me.jstProcessInner_, clone, template,
+ JsEvalContext.recycle, clone, null);
+ me.push_(queue);
}
}
-}
+};
/**
- * Sets ATT_select and ATT_instance following recursion to jstProcess.
+ * Implements the jsvars attribute: evaluates each of the values and
+ * assigns them to variables in the current context. Similar to
+ * jsvalues, except that all values are treated as vars, independent
+ * of their names.
*
- * @param {Element} template The template
+ * @param {JsEvalContext} context Current evaluation context.
*
- * @param {String} select The jsselect string
+ * @param {Element} template Currently processed template node.
*
- * @param {String} instanceStr The new value for the jsinstance attribute
+ * @param {Array} values Processed value of the jsvalues attribute: a
+ * flattened array of pairs. The second element in the pair is a
+ * function that can be passed to jsexec() for evaluation in the
+ * current jscontext, and the first element is the variable name that
+ * the value returned by jsexec is assigned to.
*/
-function postProcessMultiple_(template, select, instanceStr) {
- domSetAttribute(template, ATT_select, select);
- domSetAttribute(template, ATT_instance, instanceStr);
-}
+JstProcessor.prototype.jstVars_ = function(context, template, values) {
+ for (var i = 0, I = jsLength(values); i < I; i += 2) {
+ var label = values[i];
+ var value = context.jsexec(values[i+1], template);
+ context.setVariable(label, value);
+ }
+};
/**
- * Sets ATT_select and makes the element visible following recursion to
- * jstProcess.
- *
- * @param {Element} template The template
- *
- * @param {String} select The jsselect string
- */
-function postProcessSingle_(template, select) {
- domSetAttribute(template, ATT_select, select);
- displayDefault(template);
-}
-
-
-/**
* Implements the jsvalues attribute: evaluates each of the values and
* assigns them to variables in the current context (if the name
* starts with '$', javascript properties of the current template node
@@ -502,39 +700,34 @@
* strings, the value is coerced to string in the latter case,
* otherwise it's the uncoerced javascript value.
*
- * @param {JsExprContext} context Current evaluation context.
+ * @param {JsEvalContext} context Current evaluation context.
*
* @param {Element} template Currently processed template node.
*
- * @param {String} valuesStr Value of the jsvalues attribute to be
- * processed.
+ * @param {Array} values Processed value of the jsvalues attribute: a
+ * flattened array of pairs. The second element in the pair is a
+ * function that can be passed to jsexec() for evaluation in the
+ * current jscontext, and the first element is the label that
+ * determines where the value returned by jsexec is assigned to.
*/
-JstProcessor.prototype.jstValues_ = function(context, template, valuesStr) {
- // TODO: It is insufficient to split the values by simply
- // finding semi-colons, as the semi-colon may be part of a string constant
- // or escaped. This needs to be fixed.
- var values = valuesStr.split(/\s*;\s*/);
- for (var i = 0; i < jsLength(values); ++i) {
- var colon = values[i].indexOf(':');
- if (colon < 0) {
- continue;
- }
- var label = stringTrim(values[i].substr(0, colon));
- var value = context.jseval(values[i].substr(colon + 1), template);
+JstProcessor.prototype.jstValues_ = function(context, template, values) {
+ for (var i = 0, I = jsLength(values); i < I; i += 2) {
+ var label = values[i];
+ var value = context.jsexec(values[i+1], template);
- if (label.charAt(0) == '$') {
+ if (label.charAt(0) == CHAR_dollar) {
// A jsvalues entry whose name starts with $ sets a local
// variable.
context.setVariable(label, value);
- } else if (label.charAt(0) == '.') {
+ } else if (label.charAt(0) == CHAR_period) {
// A jsvalues entry whose name starts with . sets a property of
// the current template node. The name may have further dot
// separated components, which are translated into namespace
// objects. This specifically allows to set properties on .style
// using jsvalues. NOTE(mesch): Setting the style attribute has
// no effect in IE and hence should not be done anyway.
- var nameSpaceLabel = label.substr(1).split('.');
+ var nameSpaceLabel = label.substr(1).split(CHAR_period);
var nameSpaceObject = template;
var nameSpaceDepth = jsLength(nameSpaceLabel);
for (var j = 0, J = nameSpaceDepth - 1; j < J; ++j) {
@@ -545,10 +738,11 @@
nameSpaceObject = nameSpaceObject[jLabel];
}
nameSpaceObject[nameSpaceLabel[nameSpaceDepth - 1]] = value;
+
} else if (label) {
// Any other jsvalues entry sets an attribute of the current
// template node.
- if (typeof value == 'boolean') {
+ if (typeof value == TYPE_boolean) {
// Handle boolean values that are set as attributes specially,
// according to the XML/HTML convention.
if (value) {
@@ -557,11 +751,11 @@
domRemoveAttribute(template, label);
}
} else {
- domSetAttribute(template, label, '' + value);
+ domSetAttribute(template, label, STRING_empty + value);
}
}
}
-}
+};
/**
@@ -570,15 +764,19 @@
* and assigns its string value to the content of the current template
* node.
*
- * @param {JsExprContext} context Current evaluation context.
+ * @param {JsEvalContext} context Current evaluation context.
*
* @param {Element} template Currently processed template node.
*
- * @param {String} content Value of the jscontent attribute to be
- * processed.
+ * @param {Function} content Processed value of the jscontent
+ * attribute.
*/
JstProcessor.prototype.jstContent_ = function(context, template, content) {
- var value = '' + context.jseval(content, template);
+ // NOTE(mesch): Profiling shows that this method costs significant
+ // time. In jstemplate_perf.html, it's about 50%. I tried to replace
+ // by HTML escaping and assignment to innerHTML, but that was even
+ // slower.
+ var value = STRING_empty + context.jsexec(content, template);
// Prevent flicker when refreshing a template and the value doesn't
// change.
if (template.innerHTML == value) {
@@ -587,31 +785,183 @@
while (template.firstChild) {
domRemoveNode(template.firstChild);
}
- var t = domCreateTextNode(ownerDocument(template), value);
+ var t = domCreateTextNode(this.document_, value);
domAppendChild(template, t);
-}
+};
/**
+ * Caches access to and parsing of template processing attributes. If
+ * domGetAttribute() is called every time a template attribute value
+ * is used, it takes more than 10% of the time.
+ *
+ * @param {Element} template A DOM element node of the template.
+ *
+ * @return {Object} A javascript object that has all js template
+ * processing attribute values of the node as properties.
+ */
+JstProcessor.prototype.jstAttributes_ = function(template) {
+ if (template[PROP_jstcache]) {
+ return template[PROP_jstcache];
+ }
+
+ var jstid = domGetAttribute(template, ATT_jstcache);
+ if (jstid) {
+ return template[PROP_jstcache] = JstProcessor.jstcache_[jstid];
+ }
+
+ return JstProcessor.prepareNode_(template);
+};
+
+
+/**
* Helps to implement the transclude attribute, and is the initial
* call to get hold of a template from its ID.
*
- * @param {String} name The ID of the HTML element used as template.
+ * If the ID is not present in the DOM, and opt_loadHtmlFn is specified, this
+ * function will call that function and add the result to the DOM, before
+ * returning the template.
*
- * @returns {Element} The DOM node of the template. (Only element
- * nodes can be found by ID, hence it's a Element.)
+ * @param {string} name The ID of the HTML element used as template.
+ * @param {Function} opt_loadHtmlFn A function which, when called, will return
+ * HTML that contains an element whose ID is 'name'.
+ *
+ * @return {Element|null} The DOM node of the template. (Only element nodes
+ * can be found by ID, hence it's a Element.)
*/
-function jstGetTemplate(name) {
- var section = domGetElementById(document, name);
+function jstGetTemplate(name, opt_loadHtmlFn) {
+ var doc = document;
+ var section;
+ if (opt_loadHtmlFn) {
+ section = jstLoadTemplateIfNotPresent(doc, name, opt_loadHtmlFn);
+ } else {
+ section = domGetElementById(doc, name);
+ }
if (section) {
- var ret = domCloneNode(section);
- domRemoveAttribute(ret, 'id');
+ JstProcessor.prepareTemplate_(section);
+ var ret = domCloneElement(section);
+ domRemoveAttribute(ret, STRING_id);
return ret;
} else {
return null;
}
}
-window['jstGetTemplate'] = jstGetTemplate;
-window['jstProcess'] = jstProcess;
-window['JsExprContext'] = JsExprContext;
+/**
+ * This function is the same as 'jstGetTemplate' but, if the template
+ * does not exist, throw an exception.
+ *
+ * @param {string} name The ID of the HTML element used as template.
+ * @param {Function} opt_loadHtmlFn A function which, when called, will return
+ * HTML that contains an element whose ID is 'name'.
+ *
+ * @return {Element} The DOM node of the template. (Only element nodes
+ * can be found by ID, hence it's a Element.)
+ */
+function jstGetTemplateOrDie(name, opt_loadHtmlFn) {
+ var x = jstGetTemplate(name, opt_loadHtmlFn);
+ check(x !== null);
+ return /** @type Element */(x);
+}
+
+
+/**
+ * If an element with id 'name' is not present in the document, call loadHtmlFn
+ * and insert the result into the DOM.
+ *
+ * @param {Document} doc
+ * @param {string} name
+ * @param {Function} loadHtmlFn A function that returns HTML to be inserted
+ * into the DOM.
+ * @param {string} opt_target The id of a DOM object under which to attach the
+ * HTML once it's inserted. An object with this id is created if it does not
+ * exist.
+ * @return {Element} The node whose id is 'name'
+ */
+function jstLoadTemplateIfNotPresent(doc, name, loadHtmlFn, opt_target) {
+ var section = domGetElementById(doc, name);
+ if (section) {
+ return section;
+ }
+ // Load any necessary HTML and try again.
+ jstLoadTemplate_(doc, loadHtmlFn(), opt_target || STRING_jsts);
+ var section = domGetElementById(doc, name);
+ if (!section) {
+ log("Error: jstGetTemplate was provided with opt_loadHtmlFn, " +
+ "but that function did not provide the id '" + name + "'.");
+ }
+ return /** @type Element */(section);
+}
+
+
+/**
+ * Loads the given HTML text into the given document, so that
+ * jstGetTemplate can find it.
+ *
+ * We append it to the element identified by targetId, which is hidden.
+ * If it doesn't exist, it is created.
+ *
+ * @param {Document} doc The document to create the template in.
+ *
+ * @param {string} html HTML text to be inserted into the document.
+ *
+ * @param {string} targetId The id of a DOM object under which to attach the
+ * HTML once it's inserted. An object with this id is created if it does not
+ * exist.
+ */
+function jstLoadTemplate_(doc, html, targetId) {
+ var existing_target = domGetElementById(doc, targetId);
+ var target;
+ if (!existing_target) {
+ target = domCreateElement(doc, STRING_div);
+ target.id = targetId;
+ displayNone(target);
+ positionAbsolute(target);
+ domAppendChild(doc.body, target);
+ } else {
+ target = existing_target;
+ }
+ var div = domCreateElement(doc, STRING_div);
+ target.appendChild(div);
+ div.innerHTML = html;
+}
+
+
+/**
+ * Sets the jsinstance attribute on a node according to its context.
+ *
+ * @param {Element} template The template DOM node to set the instance
+ * attribute on.
+ *
+ * @param {Array} values The current input context, the array of
+ * values of which the template node will render one instance.
+ *
+ * @param {number} index The index of this template node in values.
+ */
+function jstSetInstance(template, values, index) {
+ if (index == jsLength(values) - 1) {
+ domSetAttribute(template, ATT_instance, CHAR_asterisk + index);
+ } else {
+ domSetAttribute(template, ATT_instance, STRING_empty + index);
+ }
+}
+
+
+/**
+ * Log the current state.
+ * @param {string} caller An identifier for the caller of .log_.
+ * @param {Element} template The template node being processed.
+ * @param {Object} jstAttributeValues The jst attributes of the template node.
+ */
+JstProcessor.prototype.logState_ = function(
+ caller, template, jstAttributeValues) {
+};
+
+
+/**
+ * Retrieve the processing logs.
+ * @return {Array.<string>} The processing logs.
+ */
+JstProcessor.prototype.getLogs = function() {
+ return this.logs_;
+};
« no previous file with comments | « chrome/third_party/jstemplate/jsevalcontext.js ('k') | chrome/third_party/jstemplate/jstemplate_compiled.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698