Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion lib/AndroidLayoutFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are unicode and octal escape sequences even valid in the Android layout xml files?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

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,
Expand Down