Author: mhermanto Date: Wed May 11 07:01:25 2011 New Revision: 1101764 URL: http://svn.apache.org/viewvc?rev=1101764&view=rev Log: Further refactoring for core dom and string ... and fix string concat.
http://codereview.appspot.com/4540042/ Modified: shindig/trunk/features/pom.xml shindig/trunk/features/src/main/javascript/features/core.util.dom/dom.js shindig/trunk/features/src/main/javascript/features/core.util.string/feature.xml shindig/trunk/features/src/main/javascript/features/core.util.string/string.js shindig/trunk/features/src/main/javascript/features/core.util.string/taming.js shindig/trunk/features/src/main/javascript/features/core.util/feature.xml shindig/trunk/features/src/main/javascript/features/core.util/taming.js shindig/trunk/features/src/main/javascript/features/core.util/util.js shindig/trunk/features/src/main/javascript/features/features.txt shindig/trunk/features/src/test/javascript/features/alltests.js Modified: shindig/trunk/features/pom.xml URL: http://svn.apache.org/viewvc/shindig/trunk/features/pom.xml?rev=1101764&r1=1101763&r2=1101764&view=diff ============================================================================== --- shindig/trunk/features/pom.xml (original) +++ shindig/trunk/features/pom.xml Wed May 11 07:01:25 2011 @@ -115,6 +115,8 @@ <source>core.json/json-jsimpl.js</source> <source>core.json/json-flatten.js</source> <source>shindig.auth/auth.js</source> + <source>core.util.dom/dom.js</source> + <source>core.util.string/string.js</source> <source>core.util.urlparams/urlparams.js</source> <source>core.util/util.js</source> <source>core.prefs/prefs.js</source> Modified: shindig/trunk/features/src/main/javascript/features/core.util.dom/dom.js URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/core.util.dom/dom.js?rev=1101764&r1=1101763&r2=1101764&view=diff ============================================================================== --- shindig/trunk/features/src/main/javascript/features/core.util.dom/dom.js (original) +++ shindig/trunk/features/src/main/javascript/features/core.util.dom/dom.js Wed May 11 07:01:25 2011 @@ -66,4 +66,4 @@ gadgets.util = gadgets.util || {}; return document.documentElement || document; }; -})(); \ No newline at end of file +})(); Modified: shindig/trunk/features/src/main/javascript/features/core.util.string/feature.xml URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/core.util.string/feature.xml?rev=1101764&r1=1101763&r2=1101764&view=diff ============================================================================== --- shindig/trunk/features/src/main/javascript/features/core.util.string/feature.xml (original) +++ shindig/trunk/features/src/main/javascript/features/core.util.string/feature.xml Wed May 11 07:01:25 2011 @@ -23,6 +23,8 @@ <script src="string.js"/> <script src="taming.js" caja="1"/> <api> + <exports type="js">gadgets.util.escape</exports> + <exports type="js">gadgets.util.escapeString</exports> <exports type="js">gadgets.util.unescapeString</exports> </api> </all> Modified: shindig/trunk/features/src/main/javascript/features/core.util.string/string.js URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/core.util.string/string.js?rev=1101764&r1=1101763&r2=1101764&view=diff ============================================================================== --- shindig/trunk/features/src/main/javascript/features/core.util.string/string.js (original) +++ shindig/trunk/features/src/main/javascript/features/core.util.string/string.js Wed May 11 07:01:25 2011 @@ -31,6 +31,47 @@ gadgets.util = gadgets.util || {}; (function() { /** + * @enum {boolean} + * @const + * @private + * Maps code points to the value to replace them with. + * If the value is "false", the character is removed entirely, otherwise + * it will be replaced with an html entity. + */ + var escapeCodePoints = { + // nul; most browsers truncate because they use c strings under the covers. + 0 : false, + // new line + 10 : true, + // carriage return + 13 : true, + // double quote + 34 : true, + // single quote + 39 : true, + // less than + 60 : true, + // greater than + 62 : true, + // backslash + 92 : true, + // line separator + 8232 : true, + // paragraph separator + 8233 : true, + // fullwidth quotation mark + 65282 : true, + // fullwidth apostrophe + 65287 : true, + // fullwidth less-than sign + 65308 : true, + // fullwidth greater-than sign + 65310 : true, + // fullwidth reverse solidus + 65340 : true + }; + + /** * Regular expression callback that returns strings from unicode code points. * * @param {Array} match Ignored. @@ -59,7 +100,70 @@ gadgets.util = gadgets.util || {}; // ((value === 0) ? '' : '\ufffd'); return String.fromCharCode(value); } - + + /** + * Escapes the input using html entities to make it safer. + * + * If the input is a string, uses gadgets.util.escapeString. + * If it is an array, calls escape on each of the array elements + * if it is an object, will only escape all the mapped keys and values if + * the opt_escapeObjects flag is set. This operation involves creating an + * entirely new object so only set the flag when the input is a simple + * string to string map. + * Otherwise, does not attempt to modify the input. + * + * @param {Object} input The object to escape. + * @param {boolean=} opt_escapeObjects Whether to escape objects. + * @return {Object} The escaped object. + * @private Only to be used by the container, not gadgets. + */ + gadgets.util.escape = function(input, opt_escapeObjects) { + if (!input) { + return input; + } else if (typeof input === 'string') { + return gadgets.util.escapeString(input); + } else if (typeof input === 'array') { + for (var i = 0, j = input.length; i < j; ++i) { + input[i] = gadgets.util.escape(input[i]); + } + } else if (typeof input === 'object' && opt_escapeObjects) { + var newObject = {}; + for (var field in input) { + if (input.hasOwnProperty(field)) { + newObject[gadgets.util.escapeString(field)] = + gadgets.util.escape(input[field], true); + } + } + return newObject; + } + return input; + }; + + /** + * Escapes the input using html entities to make it safer. + * + * Currently not in the spec -- future proposals may change + * how this is handled. + * + * @param {string} str The string to escape. + * @return {string} The escaped string. + */ + gadgets.util.escapeString = function(str) { + if (!str) return str; + var out = [], ch, shouldEscape; + for (var i = 0, j = str.length; i < j; ++i) { + ch = str.charCodeAt(i); + shouldEscape = escapeCodePoints[ch]; + if (shouldEscape === true) { + out.push('&#', ch, ';'); + } else if (shouldEscape !== false) { + // undefined or null are OK. + out.push(str.charAt(i)); + } + } + return out.join(''); + }; + /** * Reverses escapeString * Modified: shindig/trunk/features/src/main/javascript/features/core.util.string/taming.js URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/core.util.string/taming.js?rev=1101764&r1=1101763&r2=1101764&view=diff ============================================================================== --- shindig/trunk/features/src/main/javascript/features/core.util.string/taming.js (original) +++ shindig/trunk/features/src/main/javascript/features/core.util.string/taming.js Wed May 11 07:01:25 2011 @@ -23,6 +23,8 @@ */ tamings___.push(function(imports) { caja___.whitelistFuncs([ + [gadgets.util, 'escape'], + [gadgets.util, 'escapeString'], [gadgets.util, 'unescapeString'] ]); }); Modified: shindig/trunk/features/src/main/javascript/features/core.util/feature.xml URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/core.util/feature.xml?rev=1101764&r1=1101763&r2=1101764&view=diff ============================================================================== --- shindig/trunk/features/src/main/javascript/features/core.util/feature.xml (original) +++ shindig/trunk/features/src/main/javascript/features/core.util/feature.xml Wed May 11 07:01:25 2011 @@ -37,9 +37,6 @@ <exports type="js">gadgets.util.runOnLoadHandlers</exports> <exports type="js">gadgets.util.attachBrowserEvent</exports> <exports type="js">gadgets.util.removeBrowserEvent</exports> - <!-- TODO: move to core.util.string --> - <exports type="js">gadgets.util.escape</exports> - <exports type="js">gadgets.util.escapeString</exports> <!-- TODO: move to core.util.dom --> <exports type="js">gadgets.util.createIframeElement</exports> </api> Modified: shindig/trunk/features/src/main/javascript/features/core.util/taming.js URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/core.util/taming.js?rev=1101764&r1=1101763&r2=1101764&view=diff ============================================================================== --- shindig/trunk/features/src/main/javascript/features/core.util/taming.js (original) +++ shindig/trunk/features/src/main/javascript/features/core.util/taming.js Wed May 11 07:01:25 2011 @@ -23,8 +23,6 @@ */ tamings___.push(function(imports) { caja___.whitelistFuncs([ - [gadgets.util, 'escape'], - [gadgets.util, 'escapeString'], [gadgets.util, 'getFeatureParameters'], [gadgets.util, 'hasFeature'], [gadgets.util, 'registerOnLoadHandler'] Modified: shindig/trunk/features/src/main/javascript/features/core.util/util.js URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/core.util/util.js?rev=1101764&r1=1101763&r2=1101764&view=diff ============================================================================== --- shindig/trunk/features/src/main/javascript/features/core.util/util.js (original) +++ shindig/trunk/features/src/main/javascript/features/core.util/util.js Wed May 11 07:01:25 2011 @@ -44,77 +44,24 @@ gadgets.util = gadgets.util || {}; } function stringifyElement(tagName, opt_attribs) { - var arr = []; - arr.push('<').push(tagName); + var arr = ['<', tagName]; var attribs = opt_attribs || {}; for (var attrib in attribs) { if (attribs.hasOwnProperty(attrib)) { - var value = escapeString(attribs[attrib]); - arr.push(' ').push(attrib).push('="').push(value).push('"'); + arr.push(' '); + arr.push(attrib); + arr.push('="'); + arr.push(gadgets.util.escapeString(attribs[attrib])); + arr.push('"'); } } - arr.push('></').push(tagName).push('>'); + arr.push('></'); + arr.push(tagName); + arr.push('>'); return arr.join(''); } /** - * @enum {boolean} - * @const - * @private - * Maps code points to the value to replace them with. - * If the value is "false", the character is removed entirely, otherwise - * it will be replaced with an html entity. - */ - var escapeCodePoints = { - // nul; most browsers truncate because they use c strings under the covers. - 0 : false, - // new line - 10 : true, - // carriage return - 13 : true, - // double quote - 34 : true, - // single quote - 39 : true, - // less than - 60 : true, - // greater than - 62 : true, - // backslash - 92 : true, - // line separator - 8232 : true, - // paragraph separator - 8233 : true, - // fullwidth quotation mark - 65282 : true, - // fullwidth apostrophe - 65287 : true, - // fullwidth less-than sign - 65308 : true, - // fullwidth greater-than sign - 65310 : true, - // fullwidth reverse solidus - 65340 : true - }; - - function escapeString(str) { - if (!str) return str; - var out = [], ch, shouldEscape; - for (var i = 0, j = str.length; i < j; ++i) { - ch = str.charCodeAt(i); - shouldEscape = escapeCodePoints[ch]; - if (shouldEscape === true) { - out.push('&#', ch, ';'); - } else if (shouldEscape !== false) { - // undefined or null are OK. - out.push(str.charAt(i)); - } - } - return out.join(''); - } - - /** * Initializes feature parameters. */ function init(config) { @@ -230,56 +177,6 @@ gadgets.util = gadgets.util || {}; }; /** - * Escapes the input using html entities to make it safer. - * - * If the input is a string, uses gadgets.util.escapeString. - * If it is an array, calls escape on each of the array elements - * if it is an object, will only escape all the mapped keys and values if - * the opt_escapeObjects flag is set. This operation involves creating an - * entirely new object so only set the flag when the input is a simple - * string to string map. - * Otherwise, does not attempt to modify the input. - * - * @param {Object} input The object to escape. - * @param {boolean=} opt_escapeObjects Whether to escape objects. - * @return {Object} The escaped object. - * @private Only to be used by the container, not gadgets. - */ - gadgets.util.escape = function(input, opt_escapeObjects) { - // TODO: move to core.util.string. - if (!input) { - return input; - } else if (typeof input === 'string') { - return gadgets.util.escapeString(input); - } else if (typeof input === 'array') { - for (var i = 0, j = input.length; i < j; ++i) { - input[i] = gadgets.util.escape(input[i]); - } - } else if (typeof input === 'object' && opt_escapeObjects) { - var newObject = {}; - for (var field in input) { - if (input.hasOwnProperty(field)) { - newObject[gadgets.util.escapeString(field)] = gadgets.util.escape(input[field], true); - } - } - return newObject; - } - return input; - }; - - /** - * Escapes the input using html entities to make it safer. - * - * Currently not in the spec -- future proposals may change - * how this is handled. - * - * @param {string} str The string to escape. - * @return {string} The escaped string. - */ - // TODO: move to core.util.string. - gadgets.util.escapeString = escapeString; - - /** * Attach an event listener to given DOM element (Not a gadget standard) * * @param {Object} elem DOM element on which to attach event. Modified: shindig/trunk/features/src/main/javascript/features/features.txt URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/features.txt?rev=1101764&r1=1101763&r2=1101764&view=diff ============================================================================== --- shindig/trunk/features/src/main/javascript/features/features.txt (original) +++ shindig/trunk/features/src/main/javascript/features/features.txt Wed May 11 07:01:25 2011 @@ -36,8 +36,8 @@ features/core.log/feature.xml features/core.none/feature.xml features/core.prefs/feature.xml features/core.util/feature.xml -features/core.util.dom/feature.xml features/core.util.string/feature.xml +features/core.util.dom/feature.xml features/core.util.urlparams/feature.xml features/core/feature.xml features/dynamic-height.height/feature.xml Modified: shindig/trunk/features/src/test/javascript/features/alltests.js URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/test/javascript/features/alltests.js?rev=1101764&r1=1101763&r2=1101764&view=diff ============================================================================== --- shindig/trunk/features/src/test/javascript/features/alltests.js (original) +++ shindig/trunk/features/src/test/javascript/features/alltests.js Wed May 11 07:01:25 2011 @@ -39,8 +39,8 @@ if (!this.JsUtil) { eval(JsUtil.prototype.include(srcDir + '/core.json/json-native.js')); eval(JsUtil.prototype.include(srcDir + '/core.json/json-jsimpl.js')); eval(JsUtil.prototype.include(srcDir + '/core.json/json-flatten.js')); - eval(JsUtil.prototype.include(srcDir + '/core.util.string/string.js')); eval(JsUtil.prototype.include(srcDir + '/core.util.dom/dom.js')); + eval(JsUtil.prototype.include(srcDir + '/core.util.string/string.js')); eval(JsUtil.prototype.include(srcDir + '/core.util.urlparams/urlparams.js')); eval(JsUtil.prototype.include(srcDir + '/core.util/util.js')); eval(JsUtil.prototype.include(srcDir + '/core.prefs/prefs.js'));
