| OLD | NEW |
| (Empty) |
| 1 // Copyright 2005-2006 Google Inc. All Rights Reserved. | |
| 2 /** | |
| 3 * @fileoverview This file contains miscellaneous basic functionality. | |
| 4 * | |
| 5 */ | |
| 6 | |
| 7 /** | |
| 8 * Creates a DOM element with the given tag name in the document of the | |
| 9 * owner element. | |
| 10 * | |
| 11 * @param {String} tagName The name of the tag to create. | |
| 12 * @param {Element} owner The intended owner (i.e., parent element) of | |
| 13 * the created element. | |
| 14 * @param {Point} opt_position The top-left corner of the created element. | |
| 15 * @param {Size} opt_size The size of the created element. | |
| 16 * @param {Boolean} opt_noAppend Do not append the new element to the owner. | |
| 17 * @return {Element} The newly created element node. | |
| 18 */ | |
| 19 function createElement(tagName, owner, opt_position, opt_size, opt_noAppend) { | |
| 20 var element = ownerDocument(owner).createElement(tagName); | |
| 21 // NOTE: Firefox/1.5 makes elements visible as soon as they | |
| 22 // are inserted in the DOM. Hence, position must be set before | |
| 23 // appending the element to its parent in order to avoid it showing | |
| 24 // up transiently at the origin before placed at the right | |
| 25 // position. NOTE: I don't think that this triggers the | |
| 26 // reverse DOM insertion memory leak in IE. | |
| 27 if (opt_position) { | |
| 28 setPosition(element, opt_position); | |
| 29 } | |
| 30 if (opt_size) { | |
| 31 setSize(element, opt_size); | |
| 32 } | |
| 33 if (owner && !opt_noAppend) { | |
| 34 appendChild(owner, element); | |
| 35 } | |
| 36 | |
| 37 return element; | |
| 38 } | |
| 39 | |
| 40 /** | |
| 41 * Creates a text node with the given value. | |
| 42 * | |
| 43 * @param {String} value The text to place in the new node. | |
| 44 * @param {Element} owner The owner (i.e., parent element) of the new | |
| 45 * text node. | |
| 46 * @return {Text} The newly created text node. | |
| 47 */ | |
| 48 function createTextNode(value, owner) { | |
| 49 var element = ownerDocument(owner).createTextNode(value); | |
| 50 if (owner) { | |
| 51 appendChild(owner, element); | |
| 52 } | |
| 53 return element; | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * Returns the document owner of the given element. In particular, | |
| 58 * returns window.document if node is null or the browser does not | |
| 59 * support ownerDocument. | |
| 60 * | |
| 61 * @param {Node} node The node whose ownerDocument is required. | |
| 62 * @returns {Document|Null} The owner document or null if unsupported. | |
| 63 */ | |
| 64 function ownerDocument(node) { | |
| 65 return (node ? node.ownerDocument : null) || document; | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Wrapper function to create CSS units (pixels) string | |
| 70 * | |
| 71 * @param {Number} numPixels Number of pixels, may be floating point. | |
| 72 * @returns {String} Corresponding CSS units string. | |
| 73 */ | |
| 74 function px(numPixels) { | |
| 75 return round(numPixels) + "px"; | |
| 76 } | |
| 77 | |
| 78 /** | |
| 79 * Sets the left and top of the given element to the given point. | |
| 80 * | |
| 81 * @param {Element} element The dom element to manipulate. | |
| 82 * @param {Point} point The desired position. | |
| 83 */ | |
| 84 function setPosition(element, point) { | |
| 85 var style = element.style; | |
| 86 style.position = "absolute"; | |
| 87 style.left = px(point.x); | |
| 88 style.top = px(point.y); | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Sets the width and height style attributes to the given size. | |
| 93 * | |
| 94 * @param {Element} element The dom element to manipulate. | |
| 95 * @param {Size} size The desired size. | |
| 96 */ | |
| 97 function setSize(element, size) { | |
| 98 var style = element.style; | |
| 99 style.width = px(size.width); | |
| 100 style.height = px(size.height); | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Sets display to none. Doing this as a function saves a few bytes for | |
| 105 * the 'style.display' property and the 'none' literal. | |
| 106 * | |
| 107 * @param {Element} node The dom element to manipulate. | |
| 108 */ | |
| 109 function displayNone(node) { | |
| 110 node.style.display = 'none'; | |
| 111 } | |
| 112 | |
| 113 /** | |
| 114 * Sets display to default. | |
| 115 * | |
| 116 * @param {Element} node The dom element to manipulate. | |
| 117 */ | |
| 118 function displayDefault(node) { | |
| 119 node.style.display = ''; | |
| 120 } | |
| 121 | |
| 122 /** | |
| 123 * Appends the given child to the given parent in the DOM | |
| 124 * | |
| 125 * @param {Element} parent The parent dom element. | |
| 126 * @param {Node} child The new child dom node. | |
| 127 */ | |
| 128 function appendChild(parent, child) { | |
| 129 parent.appendChild(child); | |
| 130 } | |
| 131 | |
| 132 | |
| 133 /** | |
| 134 * Wrapper for the eval() builtin function to evaluate expressions and | |
| 135 * obtain their value. It wraps the expression in parentheses such | |
| 136 * that object literals are really evaluated to objects. Without the | |
| 137 * wrapping, they are evaluated as block, and create syntax | |
| 138 * errors. Also protects against other syntax errors in the eval()ed | |
| 139 * code and returns null if the eval throws an exception. | |
| 140 * | |
| 141 * @param {String} expr | |
| 142 * @return {Object|Null} | |
| 143 */ | |
| 144 function jsEval(expr) { | |
| 145 try { | |
| 146 // NOTE: An alternative idiom would be: | |
| 147 // | |
| 148 // eval('(' + expr + ')'); | |
| 149 // | |
| 150 // Note that using the square brackets as below, "" evals to undefined. | |
| 151 // The alternative of using parentheses does not work when evaluating | |
| 152 // function literals in IE. | |
| 153 // e.g. eval("(function() {})") returns undefined, and not a function | |
| 154 // object, in IE. | |
| 155 return eval('[' + expr + '][0]'); | |
| 156 } catch (e) { | |
| 157 return null; | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 | |
| 162 /** | |
| 163 * Wrapper for the eval() builtin function to execute statements. This | |
| 164 * guards against exceptions thrown, but doesn't return a | |
| 165 * value. Still, mostly for testability, it returns a boolean to | |
| 166 * indicate whether execution was successful. NOTE: | |
| 167 * javascript's eval semantics is murky in that it confounds | |
| 168 * expression evaluation and statement execution into a single | |
| 169 * construct. Cf. jsEval(). | |
| 170 * | |
| 171 * @param {String} stmt | |
| 172 * @return {Boolean} | |
| 173 */ | |
| 174 function jsExec(stmt) { | |
| 175 try { | |
| 176 eval(stmt); | |
| 177 return true; | |
| 178 } catch (e) { | |
| 179 return false; | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 | |
| 184 /** | |
| 185 * Wrapper for eval with a context. NOTE: The style guide | |
| 186 * deprecates eval, so this is the exception that proves the | |
| 187 * rule. Notice also that since the value of the expression is | |
| 188 * returned rather than assigned to a local variable, one major | |
| 189 * objection aganist the use of the with() statement, namely that | |
| 190 * properties of the with() target override local variables of the | |
| 191 * same name, is void here. | |
| 192 * | |
| 193 * @param {String} expr | |
| 194 * @param {Object} context | |
| 195 * @return {Object|Null} | |
| 196 */ | |
| 197 function jsEvalWith(expr, context) { | |
| 198 try { | |
| 199 with (context) { | |
| 200 // See comment in jsEval. | |
| 201 return eval('[' + expr + '][0]'); | |
| 202 } | |
| 203 } catch (e) { | |
| 204 return null; | |
| 205 } | |
| 206 } | |
| OLD | NEW |