mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-23 18:17:20 +00:00
Additional utilities for stringifying and escaping strings
Needed for JavaScript compilation
This commit is contained in:
parent
2b5676e187
commit
0de633d28b
47
js/Utils.js
47
js/Utils.js
@ -156,7 +156,11 @@ utils.getHours12 = function(date) {
|
||||
// Convert & to "&", < to "<", > to ">" and " to """
|
||||
utils.htmlEncode = function(s)
|
||||
{
|
||||
return s.toString().replace(/&/mg,"&").replace(/</mg,"<").replace(/>/mg,">").replace(/\"/mg,""");
|
||||
if(s) {
|
||||
return s.toString().replace(/&/mg,"&").replace(/</mg,"<").replace(/>/mg,">").replace(/\"/mg,""");
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
};
|
||||
|
||||
// Convert "&" to &, "<" to <, ">" to > and """ to "
|
||||
@ -190,4 +194,45 @@ utils.unescapeLineBreaks = function(s) {
|
||||
return s.replace(/\\n/mg,"\n").replace(/\\b/mg," ").replace(/\\s/mg,"\\").replace(/\r/mg,"");
|
||||
};
|
||||
|
||||
/*
|
||||
* Returns an escape sequence for given character. Uses \x for characters <=
|
||||
* 0xFF to save space, \u for the rest.
|
||||
*
|
||||
* The code needs to be in sync with th code template in the compilation
|
||||
* function for "action" nodes.
|
||||
*/
|
||||
// Copied from peg.js, thanks to David Majda
|
||||
utils.escape = function(ch) {
|
||||
var escapeChar,
|
||||
length,
|
||||
charCode = ch.charCodeAt(0);
|
||||
if (charCode <= 0xFF) {
|
||||
escapeChar = 'x';
|
||||
length = 2;
|
||||
} else {
|
||||
escapeChar = 'u';
|
||||
length = 4;
|
||||
}
|
||||
return '\\' + escapeChar + utils.zeroPad(charCode.toString(16).toUpperCase(),length);
|
||||
};
|
||||
|
||||
// Turns a string into a legal JavaScript string
|
||||
// Copied from peg.js, thanks to David Majda
|
||||
utils.stringify = function(s) {
|
||||
/*
|
||||
* ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
|
||||
* literal except for the closing quote character, backslash, carriage return,
|
||||
* line separator, paragraph separator, and line feed. Any character may
|
||||
* appear in the form of an escape sequence.
|
||||
*
|
||||
* For portability, we also escape escape all non-ASCII characters.
|
||||
*/
|
||||
return '"' + s
|
||||
.replace(/\\/g, '\\\\') // backslash
|
||||
.replace(/"/g, '\\"') // closing quote character
|
||||
.replace(/\r/g, '\\r') // carriage return
|
||||
.replace(/\n/g, '\\n') // line feed
|
||||
.replace(/[\x80-\uFFFF]/g, utils.escape) + '"'; // non-ASCII characters
|
||||
};
|
||||
|
||||
})();
|
||||
|
Loading…
Reference in New Issue
Block a user