2012-06-28 14:01:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012, Mozilla Foundation and contributors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-08-30 12:12:02 +00:00
|
|
|
|
2013-03-13 04:51:30 +00:00
|
|
|
this.EXPORTED_SYMBOLS = [ "template" ];
|
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "console",
|
2015-10-15 10:45:22 +00:00
|
|
|
"resource://gre/modules/Console.jsm");
|
2011-08-30 12:12:02 +00:00
|
|
|
|
2015-04-23 09:24:49 +00:00
|
|
|
'use strict';
|
2011-10-03 15:09:51 +00:00
|
|
|
|
2012-04-03 12:45:00 +00:00
|
|
|
/**
|
|
|
|
* For full documentation, see:
|
|
|
|
* https://github.com/mozilla/domtemplate/blob/master/README.md
|
|
|
|
*/
|
|
|
|
|
2011-08-30 12:12:02 +00:00
|
|
|
/**
|
2011-12-08 12:37:20 +00:00
|
|
|
* Begin a new templating process.
|
|
|
|
* @param node A DOM element or string referring to an element's id
|
|
|
|
* @param data Data to use in filling out the template
|
|
|
|
* @param options Options to customize the template processing. One of:
|
|
|
|
* - allowEval: boolean (default false) Basic template interpolations are
|
2012-04-03 12:45:00 +00:00
|
|
|
* either property paths (e.g. ${a.b.c.d}), or if allowEval=true then we
|
|
|
|
* allow arbitrary JavaScript
|
|
|
|
* - stack: string or array of strings (default empty array) The template
|
|
|
|
* engine maintains a stack of tasks to help debug where it is. This allows
|
|
|
|
* this stack to be prefixed with a template name
|
|
|
|
* - blankNullUndefined: By default DOMTemplate exports null and undefined
|
|
|
|
* values using the strings 'null' and 'undefined', which can be helpful for
|
|
|
|
* debugging, but can introduce unnecessary extra logic in a template to
|
|
|
|
* convert null/undefined to ''. By setting blankNullUndefined:true, this
|
|
|
|
* conversion is handled by DOMTemplate
|
2011-08-30 12:12:02 +00:00
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
var template = function(node, data, options) {
|
|
|
|
var state = {
|
|
|
|
options: options || {},
|
|
|
|
// We keep a track of the nodes that we've passed through so we can keep
|
|
|
|
// data.__element pointing to the correct node
|
|
|
|
nodes: []
|
|
|
|
};
|
|
|
|
|
|
|
|
state.stack = state.options.stack;
|
|
|
|
|
|
|
|
if (!Array.isArray(state.stack)) {
|
|
|
|
if (typeof state.stack === 'string') {
|
|
|
|
state.stack = [ options.stack ];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state.stack = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
processNode(state, node, data);
|
|
|
|
};
|
|
|
|
|
2015-04-23 09:24:49 +00:00
|
|
|
if (typeof exports !== 'undefined') {
|
|
|
|
exports.template = template;
|
|
|
|
}
|
2011-12-08 12:37:20 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-03 11:20:27 +00:00
|
|
|
* Helper for the places where we need to act asynchronously and keep track of
|
|
|
|
* where we are right now
|
2011-12-08 12:37:20 +00:00
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function cloneState(state) {
|
|
|
|
return {
|
|
|
|
options: state.options,
|
|
|
|
stack: state.stack.slice(),
|
|
|
|
nodes: state.nodes.slice()
|
|
|
|
};
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
|
|
|
|
2011-12-08 12:37:20 +00:00
|
|
|
/**
|
2013-09-03 11:20:27 +00:00
|
|
|
* Regex used to find ${...} sections in some text.
|
2011-12-08 12:37:20 +00:00
|
|
|
* Performance note: This regex uses ( and ) to capture the 'script' for
|
|
|
|
* further processing. Not all of the uses of this regex use this feature so
|
|
|
|
* if use of the capturing group is a performance drain then we should split
|
|
|
|
* this regex in two.
|
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
var TEMPLATE_REGION = /\$\{([^}]*)\}/g;
|
2011-12-08 12:37:20 +00:00
|
|
|
|
2011-08-30 12:12:02 +00:00
|
|
|
/**
|
|
|
|
* Recursive function to walk the tree processing the attributes as it goes.
|
2011-10-03 15:05:41 +00:00
|
|
|
* @param node the node to process. If you pass a string in instead of a DOM
|
|
|
|
* element, it is assumed to be an id for use with document.getElementById()
|
2011-08-30 12:12:02 +00:00
|
|
|
* @param data the data to use for node processing.
|
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function processNode(state, node, data) {
|
2011-10-03 15:05:41 +00:00
|
|
|
if (typeof node === 'string') {
|
|
|
|
node = document.getElementById(node);
|
|
|
|
}
|
2011-10-03 15:09:51 +00:00
|
|
|
if (data == null) {
|
2011-10-03 15:05:41 +00:00
|
|
|
data = {};
|
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
state.stack.push(node.nodeName + (node.id ? '#' + node.id : ''));
|
2012-05-30 07:47:28 +00:00
|
|
|
var pushedNode = false;
|
2011-08-30 12:12:02 +00:00
|
|
|
try {
|
|
|
|
// Process attributes
|
|
|
|
if (node.attributes && node.attributes.length) {
|
|
|
|
// We need to handle 'foreach' and 'if' first because they might stop
|
|
|
|
// some types of processing from happening, and foreach must come first
|
|
|
|
// because it defines new data on which 'if' might depend.
|
|
|
|
if (node.hasAttribute('foreach')) {
|
2013-09-03 11:20:27 +00:00
|
|
|
processForEach(state, node, data);
|
2011-08-30 12:12:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (node.hasAttribute('if')) {
|
2013-09-03 11:20:27 +00:00
|
|
|
if (!processIf(state, node, data)) {
|
2011-08-30 12:12:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Only make the node available once we know it's not going away
|
2013-09-03 11:20:27 +00:00
|
|
|
state.nodes.push(data.__element);
|
2011-08-30 12:12:02 +00:00
|
|
|
data.__element = node;
|
2012-05-30 07:47:28 +00:00
|
|
|
pushedNode = true;
|
2011-08-30 12:12:02 +00:00
|
|
|
// It's good to clean up the attributes when we've processed them,
|
|
|
|
// but if we do it straight away, we mess up the array index
|
|
|
|
var attrs = Array.prototype.slice.call(node.attributes);
|
2011-10-03 15:05:41 +00:00
|
|
|
for (var i = 0; i < attrs.length; i++) {
|
2011-08-30 12:12:02 +00:00
|
|
|
var value = attrs[i].value;
|
|
|
|
var name = attrs[i].name;
|
2013-03-13 04:51:30 +00:00
|
|
|
|
2013-09-03 11:20:27 +00:00
|
|
|
state.stack.push(name);
|
2011-08-30 12:12:02 +00:00
|
|
|
try {
|
|
|
|
if (name === 'save') {
|
|
|
|
// Save attributes are a setter using the node
|
2013-09-03 11:20:27 +00:00
|
|
|
value = stripBraces(state, value);
|
|
|
|
property(state, value, data, node);
|
2011-08-30 12:12:02 +00:00
|
|
|
node.removeAttribute('save');
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
else if (name.substring(0, 2) === 'on') {
|
|
|
|
// If this attribute value contains only an expression
|
|
|
|
if (value.substring(0, 2) === '${' && value.slice(-1) === '}' &&
|
|
|
|
value.indexOf('${', 2) === -1) {
|
2013-09-03 11:20:27 +00:00
|
|
|
value = stripBraces(state, value);
|
|
|
|
var func = property(state, value, data);
|
2013-03-13 04:51:30 +00:00
|
|
|
if (typeof func === 'function') {
|
|
|
|
node.removeAttribute(name);
|
|
|
|
var capture = node.hasAttribute('capture' + name.substring(2));
|
|
|
|
node.addEventListener(name.substring(2), func, capture);
|
|
|
|
if (capture) {
|
|
|
|
node.removeAttribute('capture' + name.substring(2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Attribute value is not a function - use as a DOM-L0 string
|
|
|
|
node.setAttribute(name, func);
|
|
|
|
}
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
2013-03-13 04:51:30 +00:00
|
|
|
else {
|
|
|
|
// Attribute value is not a single expression use as DOM-L0
|
2013-09-03 11:20:27 +00:00
|
|
|
node.setAttribute(name, processString(state, value, data));
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.removeAttribute(name);
|
2011-08-30 12:12:02 +00:00
|
|
|
// Remove '_' prefix of attribute names so the DOM won't try
|
|
|
|
// to use them before we've processed the template
|
|
|
|
if (name.charAt(0) === '_') {
|
2013-03-13 04:51:30 +00:00
|
|
|
name = name.substring(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async attributes can only work if the whole attribute is async
|
|
|
|
var replacement;
|
2013-09-03 11:20:27 +00:00
|
|
|
if (value.indexOf('${') === 0 &&
|
|
|
|
value.charAt(value.length - 1) === '}') {
|
|
|
|
replacement = envEval(state, value.slice(2, -1), data, value);
|
2013-03-13 04:51:30 +00:00
|
|
|
if (replacement && typeof replacement.then === 'function') {
|
|
|
|
node.setAttribute(name, '');
|
2015-04-23 09:24:49 +00:00
|
|
|
/* jshint loopfunc:true */
|
2013-03-13 04:51:30 +00:00
|
|
|
replacement.then(function(newValue) {
|
|
|
|
node.setAttribute(name, newValue);
|
2013-09-03 11:20:27 +00:00
|
|
|
}).then(null, console.error);
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-09-03 11:20:27 +00:00
|
|
|
if (state.options.blankNullUndefined && replacement == null) {
|
2013-03-13 04:51:30 +00:00
|
|
|
replacement = '';
|
|
|
|
}
|
|
|
|
node.setAttribute(name, replacement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-09-03 11:20:27 +00:00
|
|
|
node.setAttribute(name, processString(state, value, data));
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
finally {
|
2013-09-03 11:20:27 +00:00
|
|
|
state.stack.pop();
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through our children calling processNode. First clone them, so the
|
|
|
|
// set of nodes that we visit will be unaffected by additions or removals.
|
2011-10-03 15:05:41 +00:00
|
|
|
var childNodes = Array.prototype.slice.call(node.childNodes);
|
|
|
|
for (var j = 0; j < childNodes.length; j++) {
|
2013-09-03 11:20:27 +00:00
|
|
|
processNode(state, childNodes[j], data);
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
|
|
|
|
2012-04-03 12:45:00 +00:00
|
|
|
if (node.nodeType === 3 /*Node.TEXT_NODE*/) {
|
2013-09-03 11:20:27 +00:00
|
|
|
processTextNode(state, node, data);
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
finally {
|
2012-05-30 07:47:28 +00:00
|
|
|
if (pushedNode) {
|
2013-09-03 11:20:27 +00:00
|
|
|
data.__element = state.nodes.pop();
|
2012-05-30 07:47:28 +00:00
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
state.stack.pop();
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-08-30 12:12:02 +00:00
|
|
|
|
2013-03-13 04:51:30 +00:00
|
|
|
/**
|
|
|
|
* Handle attribute values where the output can only be a string
|
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function processString(state, value, data) {
|
|
|
|
return value.replace(TEMPLATE_REGION, function(path) {
|
|
|
|
var insert = envEval(state, path.slice(2, -1), data, value);
|
|
|
|
return state.options.blankNullUndefined && insert == null ? '' : insert;
|
|
|
|
});
|
|
|
|
}
|
2013-03-13 04:51:30 +00:00
|
|
|
|
2011-08-30 12:12:02 +00:00
|
|
|
/**
|
|
|
|
* Handle <x if="${...}">
|
|
|
|
* @param node An element with an 'if' attribute
|
2013-09-03 11:20:27 +00:00
|
|
|
* @param data The data to use with envEval()
|
2011-08-30 12:12:02 +00:00
|
|
|
* @returns true if processing should continue, false otherwise
|
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function processIf(state, node, data) {
|
|
|
|
state.stack.push('if');
|
2011-08-30 12:12:02 +00:00
|
|
|
try {
|
|
|
|
var originalValue = node.getAttribute('if');
|
2013-09-03 11:20:27 +00:00
|
|
|
var value = stripBraces(state, originalValue);
|
2011-08-30 12:12:02 +00:00
|
|
|
var recurse = true;
|
|
|
|
try {
|
2013-09-03 11:20:27 +00:00
|
|
|
var reply = envEval(state, value, data, originalValue);
|
2011-08-30 12:12:02 +00:00
|
|
|
recurse = !!reply;
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
catch (ex) {
|
2013-09-03 11:20:27 +00:00
|
|
|
handleError(state, 'Error with \'' + value + '\'', ex);
|
2011-08-30 12:12:02 +00:00
|
|
|
recurse = false;
|
|
|
|
}
|
|
|
|
if (!recurse) {
|
|
|
|
node.parentNode.removeChild(node);
|
|
|
|
}
|
|
|
|
node.removeAttribute('if');
|
|
|
|
return recurse;
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
finally {
|
2013-09-03 11:20:27 +00:00
|
|
|
state.stack.pop();
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-08-30 12:12:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle <x foreach="param in ${array}"> and the special case of
|
2011-10-03 15:09:51 +00:00
|
|
|
* <loop foreach="param in ${array}">.
|
|
|
|
* This function is responsible for extracting what it has to do from the
|
|
|
|
* attributes, and getting the data to work on (including resolving promises
|
2013-09-03 11:20:27 +00:00
|
|
|
* in getting the array). It delegates to processForEachLoop to actually
|
2011-10-03 15:09:51 +00:00
|
|
|
* unroll the data.
|
2011-08-30 12:12:02 +00:00
|
|
|
* @param node An element with a 'foreach' attribute
|
2013-09-03 11:20:27 +00:00
|
|
|
* @param data The data to use with envEval()
|
2011-08-30 12:12:02 +00:00
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function processForEach(state, node, data) {
|
|
|
|
state.stack.push('foreach');
|
2011-08-30 12:12:02 +00:00
|
|
|
try {
|
|
|
|
var originalValue = node.getAttribute('foreach');
|
|
|
|
var value = originalValue;
|
|
|
|
|
|
|
|
var paramName = 'param';
|
|
|
|
if (value.charAt(0) === '$') {
|
|
|
|
// No custom loop variable name. Use the default: 'param'
|
2013-09-03 11:20:27 +00:00
|
|
|
value = stripBraces(state, value);
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-08-30 12:12:02 +00:00
|
|
|
// Extract the loop variable name from 'NAME in ${ARRAY}'
|
|
|
|
var nameArr = value.split(' in ');
|
|
|
|
paramName = nameArr[0].trim();
|
2013-09-03 11:20:27 +00:00
|
|
|
value = stripBraces(state, nameArr[1].trim());
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
|
|
|
node.removeAttribute('foreach');
|
|
|
|
try {
|
2013-09-03 11:20:27 +00:00
|
|
|
var evaled = envEval(state, value, data, originalValue);
|
|
|
|
var cState = cloneState(state);
|
|
|
|
handleAsync(evaled, node, function(reply, siblingNode) {
|
|
|
|
processForEachLoop(cState, reply, node, siblingNode, data, paramName);
|
|
|
|
});
|
2011-10-03 15:09:51 +00:00
|
|
|
node.parentNode.removeChild(node);
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
catch (ex) {
|
2013-09-03 11:20:27 +00:00
|
|
|
handleError(state, 'Error with \'' + value + '\'', ex);
|
2011-10-03 15:09:51 +00:00
|
|
|
}
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
finally {
|
2013-09-03 11:20:27 +00:00
|
|
|
state.stack.pop();
|
2011-10-03 15:09:51 +00:00
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-08-30 12:12:02 +00:00
|
|
|
|
2011-10-03 15:09:51 +00:00
|
|
|
/**
|
2013-09-03 11:20:27 +00:00
|
|
|
* Called by processForEach to handle looping over the data in a foreach loop.
|
2011-10-03 15:09:51 +00:00
|
|
|
* This works with both arrays and objects.
|
2013-09-03 11:20:27 +00:00
|
|
|
* Calls processForEachMember() for each member of 'set'
|
2011-10-03 15:09:51 +00:00
|
|
|
* @param set The object containing the data to loop over
|
2013-09-03 11:20:27 +00:00
|
|
|
* @param templNode The node to copy for each set member
|
2011-10-03 15:09:51 +00:00
|
|
|
* @param sibling The sibling node to which we add things
|
|
|
|
* @param data the data to use for node processing
|
|
|
|
* @param paramName foreach loops have a name for the parameter currently being
|
|
|
|
* processed. The default is 'param'. e.g. <loop foreach="param in ${x}">...
|
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function processForEachLoop(state, set, templNode, sibling, data, paramName) {
|
2011-10-03 15:09:51 +00:00
|
|
|
if (Array.isArray(set)) {
|
|
|
|
set.forEach(function(member, i) {
|
2013-09-03 11:20:27 +00:00
|
|
|
processForEachMember(state, member, templNode, sibling,
|
|
|
|
data, paramName, '' + i);
|
|
|
|
});
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-10-03 15:09:51 +00:00
|
|
|
for (var member in set) {
|
|
|
|
if (set.hasOwnProperty(member)) {
|
2013-09-03 11:20:27 +00:00
|
|
|
processForEachMember(state, member, templNode, sibling,
|
|
|
|
data, paramName, member);
|
2011-10-03 15:09:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-08-30 12:12:02 +00:00
|
|
|
|
2011-10-03 15:09:51 +00:00
|
|
|
/**
|
2013-09-03 11:20:27 +00:00
|
|
|
* Called by processForEachLoop() to resolve any promises in the array (the
|
2011-10-03 15:09:51 +00:00
|
|
|
* array itself can also be a promise, but that is resolved by
|
2013-09-03 11:20:27 +00:00
|
|
|
* processForEach()). Handle <LOOP> elements (which are taken out of the DOM),
|
|
|
|
* clone the template node, and pass the processing on to processNode().
|
2011-10-03 15:09:51 +00:00
|
|
|
* @param member The data item to use in templating
|
2013-09-03 11:20:27 +00:00
|
|
|
* @param templNode The node to copy for each set member
|
2011-10-03 15:09:51 +00:00
|
|
|
* @param siblingNode The parent node to which we add things
|
|
|
|
* @param data the data to use for node processing
|
|
|
|
* @param paramName The name given to 'member' by the foreach attribute
|
|
|
|
* @param frame A name to push on the stack for debugging
|
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function processForEachMember(state, member, templNode, siblingNode, data, paramName, frame) {
|
|
|
|
state.stack.push(frame);
|
2011-10-03 15:09:51 +00:00
|
|
|
try {
|
2013-09-03 11:20:27 +00:00
|
|
|
var cState = cloneState(state);
|
|
|
|
handleAsync(member, siblingNode, function(reply, node) {
|
2014-05-22 10:04:47 +00:00
|
|
|
// Clone data because we can't be sure that we can safely mutate it
|
|
|
|
var newData = Object.create(null);
|
|
|
|
Object.keys(data).forEach(function(key) {
|
|
|
|
newData[key] = data[key];
|
|
|
|
});
|
|
|
|
newData[paramName] = reply;
|
2013-03-13 04:51:30 +00:00
|
|
|
if (node.parentNode != null) {
|
2015-04-23 09:24:49 +00:00
|
|
|
var clone;
|
2013-09-03 11:20:27 +00:00
|
|
|
if (templNode.nodeName.toLowerCase() === 'loop') {
|
|
|
|
for (var i = 0; i < templNode.childNodes.length; i++) {
|
2015-04-23 09:24:49 +00:00
|
|
|
clone = templNode.childNodes[i].cloneNode(true);
|
2013-03-13 04:51:30 +00:00
|
|
|
node.parentNode.insertBefore(clone, node);
|
2014-05-22 10:04:47 +00:00
|
|
|
processNode(cState, clone, newData);
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-04-23 09:24:49 +00:00
|
|
|
clone = templNode.cloneNode(true);
|
2013-03-13 04:51:30 +00:00
|
|
|
clone.removeAttribute('foreach');
|
2011-10-03 15:09:51 +00:00
|
|
|
node.parentNode.insertBefore(clone, node);
|
2014-05-22 10:04:47 +00:00
|
|
|
processNode(cState, clone, newData);
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
});
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
finally {
|
2013-09-03 11:20:27 +00:00
|
|
|
state.stack.pop();
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-08-30 12:12:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a text node and replace it with another text node with the ${...}
|
|
|
|
* sections parsed out. We replace the node by altering node.parentNode but
|
|
|
|
* we could probably use a DOM Text API to achieve the same thing.
|
|
|
|
* @param node The Text node to work on
|
2013-09-03 11:20:27 +00:00
|
|
|
* @param data The data to use in calls to envEval()
|
2011-08-30 12:12:02 +00:00
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function processTextNode(state, node, data) {
|
2011-08-30 12:12:02 +00:00
|
|
|
// Replace references in other attributes
|
|
|
|
var value = node.data;
|
|
|
|
// We can't use the string.replace() with function trick (see generic
|
|
|
|
// attribute processing in processNode()) because we need to support
|
|
|
|
// functions that return DOM nodes, so we can't have the conversion to a
|
|
|
|
// string.
|
|
|
|
// Instead we process the string as an array of parts. In order to split
|
|
|
|
// the string up, we first replace '${' with '\uF001$' and '}' with '\uF002'
|
|
|
|
// We can then split using \uF001 or \uF002 to get an array of strings
|
|
|
|
// where scripts are prefixed with $.
|
|
|
|
// \uF001 and \uF002 are just unicode chars reserved for private use.
|
2013-09-03 11:20:27 +00:00
|
|
|
value = value.replace(TEMPLATE_REGION, '\uF001$$$1\uF002');
|
|
|
|
// Split a string using the unicode chars F001 and F002.
|
|
|
|
var parts = value.split(/\uF001|\uF002/);
|
2011-08-30 12:12:02 +00:00
|
|
|
if (parts.length > 1) {
|
|
|
|
parts.forEach(function(part) {
|
|
|
|
if (part === null || part === undefined || part === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (part.charAt(0) === '$') {
|
2013-09-03 11:20:27 +00:00
|
|
|
part = envEval(state, part.slice(1), data, node.data);
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
var cState = cloneState(state);
|
|
|
|
handleAsync(part, node, function(reply, siblingNode) {
|
2012-04-03 12:45:00 +00:00
|
|
|
var doc = siblingNode.ownerDocument;
|
|
|
|
if (reply == null) {
|
2013-09-03 11:20:27 +00:00
|
|
|
reply = cState.options.blankNullUndefined ? '' : '' + reply;
|
2012-04-03 12:45:00 +00:00
|
|
|
}
|
|
|
|
if (typeof reply.cloneNode === 'function') {
|
|
|
|
// i.e. if (reply instanceof Element) { ...
|
2013-09-03 11:20:27 +00:00
|
|
|
reply = maybeImportNode(cState, reply, doc);
|
2012-04-03 12:45:00 +00:00
|
|
|
siblingNode.parentNode.insertBefore(reply, siblingNode);
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
else if (typeof reply.item === 'function' && reply.length) {
|
2013-09-03 11:20:27 +00:00
|
|
|
// NodeLists can be live, in which case maybeImportNode can
|
2012-05-30 07:47:28 +00:00
|
|
|
// remove them from the document, and thus the NodeList, which in
|
|
|
|
// turn breaks iteration. So first we clone the list
|
|
|
|
var list = Array.prototype.slice.call(reply, 0);
|
|
|
|
list.forEach(function(child) {
|
2013-09-03 11:20:27 +00:00
|
|
|
var imported = maybeImportNode(cState, child, doc);
|
2012-05-30 07:47:28 +00:00
|
|
|
siblingNode.parentNode.insertBefore(imported, siblingNode);
|
2013-09-03 11:20:27 +00:00
|
|
|
});
|
2012-04-03 12:45:00 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// if thing isn't a DOM element then wrap its string value in one
|
|
|
|
reply = doc.createTextNode(reply.toString());
|
|
|
|
siblingNode.parentNode.insertBefore(reply, siblingNode);
|
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
});
|
|
|
|
});
|
2011-08-30 12:12:02 +00:00
|
|
|
node.parentNode.removeChild(node);
|
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-08-30 12:12:02 +00:00
|
|
|
|
2011-10-03 15:09:51 +00:00
|
|
|
/**
|
2012-04-03 12:45:00 +00:00
|
|
|
* Return node or a import of node, if it's not in the given document
|
|
|
|
* @param node The node that we want to be properly owned
|
|
|
|
* @param doc The document that the given node should belong to
|
|
|
|
* @return A node that belongs to the given document
|
2011-10-03 15:09:51 +00:00
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function maybeImportNode(state, node, doc) {
|
2012-04-03 12:45:00 +00:00
|
|
|
return node.ownerDocument === doc ? node : doc.importNode(node, true);
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-10-03 15:09:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A function to handle the fact that some nodes can be promises, so we check
|
|
|
|
* and resolve if needed using a marker node to keep our place before calling
|
|
|
|
* an inserter function.
|
|
|
|
* @param thing The object which could be real data or a promise of real data
|
|
|
|
* we use it directly if it's not a promise, or resolve it if it is.
|
|
|
|
* @param siblingNode The element before which we insert new elements.
|
|
|
|
* @param inserter The function to to the insertion. If thing is not a promise
|
2013-09-03 11:20:27 +00:00
|
|
|
* then handleAsync() is just 'inserter(thing, siblingNode)'
|
2011-10-03 15:09:51 +00:00
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function handleAsync(thing, siblingNode, inserter) {
|
2011-11-19 16:34:05 +00:00
|
|
|
if (thing != null && typeof thing.then === 'function') {
|
2011-10-03 15:09:51 +00:00
|
|
|
// Placeholder element to be replaced once we have the real data
|
|
|
|
var tempNode = siblingNode.ownerDocument.createElement('span');
|
|
|
|
siblingNode.parentNode.insertBefore(tempNode, siblingNode);
|
|
|
|
thing.then(function(delayed) {
|
|
|
|
inserter(delayed, tempNode);
|
2013-03-13 04:51:30 +00:00
|
|
|
if (tempNode.parentNode != null) {
|
|
|
|
tempNode.parentNode.removeChild(tempNode);
|
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
}).then(null, function(error) {
|
2013-03-13 04:51:30 +00:00
|
|
|
console.error(error.stack);
|
|
|
|
});
|
2011-10-03 15:09:51 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
inserter(thing, siblingNode);
|
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-10-03 15:09:51 +00:00
|
|
|
|
2011-08-30 12:12:02 +00:00
|
|
|
/**
|
|
|
|
* Warn of string does not begin '${' and end '}'
|
|
|
|
* @param str the string to check.
|
|
|
|
* @return The string stripped of ${ and }, or untouched if it does not match
|
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function stripBraces(state, str) {
|
|
|
|
if (!str.match(TEMPLATE_REGION)) {
|
|
|
|
handleError(state, 'Expected ' + str + ' to match ${...}');
|
2011-08-30 12:12:02 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
return str.slice(2, -1);
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-08-30 12:12:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Combined getter and setter that works with a path through some data set.
|
|
|
|
* For example:
|
|
|
|
* <ul>
|
2013-09-03 11:20:27 +00:00
|
|
|
* <li>property(state, 'a.b', { a: { b: 99 }}); // returns 99
|
|
|
|
* <li>property(state, 'a', { a: { b: 99 }}); // returns { b: 99 }
|
|
|
|
* <li>property(state, 'a', { a: { b: 99 }}, 42); // returns 99 and alters the
|
2011-08-30 12:12:02 +00:00
|
|
|
* input data to be { a: { b: 42 }}
|
|
|
|
* </ul>
|
|
|
|
* @param path An array of strings indicating the path through the data, or
|
|
|
|
* a string to be cut into an array using <tt>split('.')</tt>
|
2011-10-12 17:09:38 +00:00
|
|
|
* @param data the data to use for node processing
|
2011-08-30 12:12:02 +00:00
|
|
|
* @param newValue (optional) If defined, this value will replace the
|
|
|
|
* original value for the data at the path specified.
|
|
|
|
* @return The value pointed to by <tt>path</tt> before any
|
|
|
|
* <tt>newValue</tt> is applied.
|
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function property(state, path, data, newValue) {
|
2011-08-30 12:12:02 +00:00
|
|
|
try {
|
|
|
|
if (typeof path === 'string') {
|
|
|
|
path = path.split('.');
|
|
|
|
}
|
|
|
|
var value = data[path[0]];
|
|
|
|
if (path.length === 1) {
|
|
|
|
if (newValue !== undefined) {
|
|
|
|
data[path[0]] = newValue;
|
|
|
|
}
|
|
|
|
if (typeof value === 'function') {
|
|
|
|
return value.bind(data);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (!value) {
|
2013-09-03 11:20:27 +00:00
|
|
|
handleError(state, '"' + path[0] + '" is undefined');
|
2011-08-30 12:12:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
return property(state, path.slice(1), value, newValue);
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
catch (ex) {
|
2013-09-03 11:20:27 +00:00
|
|
|
handleError(state, 'Path error with \'' + path + '\'', ex);
|
2012-04-03 12:45:00 +00:00
|
|
|
return '${' + path + '}';
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-08-30 12:12:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Like eval, but that creates a context of the variables in <tt>env</tt> in
|
|
|
|
* which the script is evaluated.
|
|
|
|
* @param script The string to be evaluated.
|
2011-10-03 15:09:51 +00:00
|
|
|
* @param data The environment in which to eval the script.
|
|
|
|
* @param frame Optional debugging string in case of failure.
|
2011-08-30 12:12:02 +00:00
|
|
|
* @return The return value of the script, or the error message if the script
|
|
|
|
* execution failed.
|
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function envEval(state, script, data, frame) {
|
2011-12-08 12:37:20 +00:00
|
|
|
try {
|
2013-09-03 11:20:27 +00:00
|
|
|
state.stack.push(frame.replace(/\s+/g, ' '));
|
|
|
|
// Detect if a script is capable of being interpreted using property()
|
|
|
|
if (/^[_a-zA-Z0-9.]*$/.test(script)) {
|
|
|
|
return property(state, script, data);
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-09-03 11:20:27 +00:00
|
|
|
if (!state.options.allowEval) {
|
|
|
|
handleError(state, 'allowEval is not set, however \'' + script + '\'' +
|
2011-12-08 12:37:20 +00:00
|
|
|
' can not be resolved using a simple property path.');
|
|
|
|
return '${' + script + '}';
|
|
|
|
}
|
2015-04-23 09:24:49 +00:00
|
|
|
|
|
|
|
// What we're looking to do is basically:
|
|
|
|
// with(data) { return eval(script); }
|
|
|
|
// except in strict mode where 'with' is banned.
|
|
|
|
// So we create a function which has a parameter list the same as the
|
|
|
|
// keys in 'data' and with 'script' as its function body.
|
|
|
|
// We then call this function with the values in 'data'
|
|
|
|
var keys = allKeys(data);
|
|
|
|
var func = Function.apply(null, keys.concat("return " + script));
|
|
|
|
|
|
|
|
var values = keys.map(function(key) { return data[key]; });
|
|
|
|
return func.apply(null, values);
|
|
|
|
|
|
|
|
// TODO: The 'with' method is different from the code above in the value
|
|
|
|
// of 'this' when calling functions. For example:
|
|
|
|
// envEval(state, 'foo()', { foo: function() { return this; } }, ...);
|
|
|
|
// The global for 'foo' when using 'with' is the data object. However the
|
|
|
|
// code above, the global is null. (Using 'func.apply(data, values)'
|
|
|
|
// changes 'this' in the 'foo()' frame, but not in the inside the body
|
|
|
|
// of 'foo', so that wouldn't help)
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
catch (ex) {
|
2013-09-03 11:20:27 +00:00
|
|
|
handleError(state, 'Template error evaluating \'' + script + '\'', ex);
|
2011-12-08 12:37:20 +00:00
|
|
|
return '${' + script + '}';
|
2013-03-13 04:51:30 +00:00
|
|
|
}
|
|
|
|
finally {
|
2013-09-03 11:20:27 +00:00
|
|
|
state.stack.pop();
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-08-30 12:12:02 +00:00
|
|
|
|
2015-04-23 09:24:49 +00:00
|
|
|
/**
|
|
|
|
* Object.keys() that respects the prototype chain
|
|
|
|
*/
|
|
|
|
function allKeys(data) {
|
|
|
|
var keys = [];
|
|
|
|
for (var key in data) { keys.push(key); }
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2011-08-30 12:12:02 +00:00
|
|
|
/**
|
|
|
|
* A generic way of reporting errors, for easy overloading in different
|
|
|
|
* environments.
|
|
|
|
* @param message the error message to report.
|
|
|
|
* @param ex optional associated exception.
|
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function handleError(state, message, ex) {
|
|
|
|
logError(message + ' (In: ' + state.stack.join(' > ') + ')');
|
2011-08-30 12:12:02 +00:00
|
|
|
if (ex) {
|
2013-09-03 11:20:27 +00:00
|
|
|
logError(ex);
|
2011-08-30 12:12:02 +00:00
|
|
|
}
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|
2011-08-30 12:12:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A generic way of reporting errors, for easy overloading in different
|
|
|
|
* environments.
|
|
|
|
* @param message the error message to report.
|
|
|
|
*/
|
2013-09-03 11:20:27 +00:00
|
|
|
function logError(message) {
|
2015-04-23 09:24:49 +00:00
|
|
|
console.error(message);
|
2013-09-03 11:20:27 +00:00
|
|
|
}
|