| OLD | NEW |
| (Empty) |
| 1 // Copyright 2006 Google | |
| 2 /** | |
| 3 * @pageoverview Some logic for the jstemplates test pages. | |
| 4 * | |
| 5 * @author Steffen Meschkat <mesch@google.com> | |
| 6 */ | |
| 7 | |
| 8 /** | |
| 9 * Returns the element with the given ID | |
| 10 * | |
| 11 * @param {String} id The id of the element to return. | |
| 12 * @param {Document} opt_context The context in which to search (optional). | |
| 13 * @return {Element} The corresponding element. | |
| 14 */ | |
| 15 function elem(id, opt_context) { | |
| 16 if (opt_context && ownerDocument(opt_context)) { | |
| 17 return ownerDocument(opt_context).getElementById(id); | |
| 18 } else { | |
| 19 return document.getElementById(id); | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 /** | |
| 24 * Wrapper for the eval() builtin function to evaluate expressions and | |
| 25 * obtain their value. It wraps the expression in parentheses such | |
| 26 * that object literals are really evaluated to objects. Without the | |
| 27 * wrapping, they are evaluated as block, and create syntax | |
| 28 * errors. Also protects against other syntax errors in the eval()ed | |
| 29 * code and returns null if the eval throws an exception. | |
| 30 * | |
| 31 * @param {String} expr | |
| 32 * @return {Object|Null} | |
| 33 */ | |
| 34 function jsEval(expr) { | |
| 35 try { | |
| 36 // NOTE(mesch): An alternative idiom would be: | |
| 37 // | |
| 38 // eval('(' + expr + ')'); | |
| 39 // | |
| 40 // Note that using the square brackets as below, "" evals to undefined. | |
| 41 // The alternative of using parentheses does not work when evaluating | |
| 42 // function literals in IE. | |
| 43 // e.g. eval("(function() {})") returns undefined, and not a function | |
| 44 // object, in IE. | |
| 45 return eval('[' + expr + '][0]'); | |
| 46 } catch (e) { | |
| 47 return null; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 /** | |
| 52 * Initializer for interactive test: copies the value from the actual | |
| 53 * template HTML into a text area to make the HTML source visible. | |
| 54 */ | |
| 55 function jsinit() { | |
| 56 elem('template').value = elem('tc').innerHTML; | |
| 57 } | |
| 58 | |
| 59 | |
| 60 /** | |
| 61 * Interactive test. | |
| 62 * | |
| 63 * @param {Boolean} reprocess If true, reprocesses the output of the | |
| 64 * previous invocation. | |
| 65 */ | |
| 66 function jstest(reprocess) { | |
| 67 var jstext = elem('js').value; | |
| 68 var input = jsEval(jstext); | |
| 69 var t; | |
| 70 if (reprocess) { | |
| 71 t = elem('out'); | |
| 72 } else { | |
| 73 elem('tc').innerHTML = elem('template').value; | |
| 74 t = jstGetTemplate('t'); | |
| 75 elem('out').innerHTML = ''; | |
| 76 elem('out').appendChild(t); | |
| 77 } | |
| 78 jstProcess(new JsExprContext(input), t); | |
| 79 elem('html').value = elem('out').innerHTML; | |
| 80 } | |
| OLD | NEW |