diff --git a/lib/AndroidLayoutFile.js b/lib/AndroidLayoutFile.js index fdc56f157..244b98afd 100644 --- a/lib/AndroidLayoutFile.js +++ b/lib/AndroidLayoutFile.js @@ -158,13 +158,79 @@ AndroidLayoutFile.prototype.getContext = function() { return this.context; }; -AndroidLayoutFile.prototype.makeKey = function(type, source) { +AndroidLayoutFile.prototype.makeOldKey = function(type, source) { if (type.substring(0, 8) === "android:") { type = type.substring(8); } return (type + '_' + source).replace(/[^a-zA-Z0-9\_]/g, "_"); }; +var reUnicodeChar = /\\u([a-fA-F0-9]{1,4})/g; +var reOctalChar = /\\([0-8]{1,3})/g; + +/** + * Unescape the string to make the same string that would be + * in memory in the target programming language. This includes + * unescaping both special and Unicode characters. + * + * @static + * @param {String} string the string to unescape + * @returns {String} the unescaped string + */ +AndroidLayoutFile.prototype.unescapeString = function(string) { + var unescaped = string; + + while ((match = reUnicodeChar.exec(unescaped))) { + if (match && match.length > 1) { + var value = parseInt(match[1], 16); + unescaped = unescaped.replace(match[0], IString.fromCodePoint(value)); + reUnicodeChar.lastIndex = 0; + } + } + + while ((match = reOctalChar.exec(unescaped))) { + if (match && match.length > 1) { + var value = parseInt(match[1], 8); + unescaped = unescaped.replace(match[0], IString.fromCodePoint(value)); + reOctalChar.lastIndex = 0; + } + } + + unescaped = unescaped. + replace(/^\\\\/g, "\\"). + replace(/([^\\])\\\\/g, "$1\\"). + replace(/\\'/g, "'"). + replace(/\\"/g, '"'); + + return unescaped; +}; + +/** + * Clean the string to make a source string. This means + * removing leading and trailing white space, compressing + * whitespaces, and unescaping characters. This changes + * the string from what it looks like in the source + * code. + * + * @static + * @param {String} string the string to clean + * @returns {String} the cleaned string + */ +AndroidLayoutFile.prototype.cleanString = function(string) { + var unescaped = this.unescapeString(string); + + unescaped = unescaped. + replace(/\\[btnfr]/g, " "). + replace(/[ \n\t\r\f]+/g, " "). + trim(); + + return unescaped; +}; + +AndroidLayoutFile.prototype.makeKey = function(type, source) { + return utils.hashKey(this.cleanString(source)) +}; + var localizableAttributes = { "android:title": true, "android:text": true,