chg: refactored global encode() method to JS

This commit is contained in:
Tobi Schäfer 2017-03-19 17:13:20 +01:00
parent 655f965084
commit 12f9e1b1e3
2 changed files with 41 additions and 12 deletions

40
js/Global/encode.js Normal file
View file

@ -0,0 +1,40 @@
var encode = function (str, buffer, encodeNewline) {
str = String(str);
if (str === null || !str.length) return str;
if (!buffer) buffer = [];
for (var i = 0, len = str.length; i < len; i += 1) {
var char = str.charAt(i);
switch (char) {
case '<':
buffer.push('&lt;');
break;
case '>':
buffer.push('&gt;');
break;
case '&':
buffer.push('&amp;');
break;
case '"':
buffer.push('&quot;');
break;
case '\n':
if (encodeNewline) {
buffer.push("<br class='helma-format' />");
}
buffer.push('\n');
break;
default:
buffer.push(char);
}
}
return buffer.join('');
};

View file

@ -68,7 +68,7 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
public void init() { public void init() {
String[] globalFuncs = { String[] globalFuncs = {
"renderSkin", "renderSkinAsString", "getProperty", "renderSkin", "renderSkinAsString", "getProperty",
"authenticate", "createSkin", "format", "encode", "authenticate", "createSkin", "format",
"encodeXml", "encodeForm", "formatParagraphs", "encodeXml", "encodeForm", "formatParagraphs",
"getXmlDocument", "getHtmlDocument", "seal", "getXmlDocument", "getHtmlDocument", "seal",
"getDBConnection", "getURL", "write", "writeln", "getDBConnection", "getURL", "write", "writeln",
@ -453,17 +453,6 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
return new NativeJavaObject(this, obj, null); return new NativeJavaObject(this, obj, null);
} }
/**
*
*
* @param obj ...
*
* @return ...
*/
public String encode(Object obj) {
return HtmlEncoder.encodeAll(toString(obj));
}
/** /**
* *
* *