diff --git a/js/Global/encode.js b/js/Global/encode.js deleted file mode 100644 index fef3dd14..00000000 --- a/js/Global/encode.js +++ /dev/null @@ -1,41 +0,0 @@ -var encode = function (str, buffer, encodeNewline) { - str = String(str); - - if (str === null || !str.length) return str; - if (!buffer) buffer = []; - if (typeof encodeNewline === 'undefined') encodeNewline = true; - - for (var i = 0, len = str.length; i < len; i += 1) { - var char = str.charAt(i); - - switch (char) { - case '<': - buffer.push('<'); - break; - - case '>': - buffer.push('>'); - break; - - case '&': - buffer.push('&'); - break; - - case '"': - buffer.push('"'); - break; - - case '\n': - if (encodeNewline) { - buffer.push("
"); - } - buffer.push('\n'); - break; - - default: - buffer.push(char); - } - } - - return buffer.join(''); -}; diff --git a/js/Global/encodeForm.js b/js/Global/encodeForm.js deleted file mode 100644 index dad26878..00000000 --- a/js/Global/encodeForm.js +++ /dev/null @@ -1,10 +0,0 @@ -// import 'encode'; - -var encodeForm = function(str, buffer) { - str = String(str); - - if (str === null || !str.length) return str; - if (!buffer) buffer = []; - - return encode(str, buffer, false); -}; diff --git a/js/Global/encodeXml.js b/js/Global/encodeXml.js deleted file mode 100644 index 858013bb..00000000 --- a/js/Global/encodeXml.js +++ /dev/null @@ -1,46 +0,0 @@ -var encodeXml = function(str, buffer) { - 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('<'); - break; - - case '>': - buffer.push('>'); - break; - - case '&': - buffer.push('&'); - break; - - case '"': - buffer.push('"'); - break; - - case '\'': - buffer.push('''); - break; - - default: - var charCode = str.charCodeAt(i); - if (charCode < 0x20) { - // sort out invalid XML characters below 0x20 - all but 0x9, 0xA and 0xD. - // The trick is an adaption of java.lang.Character.isSpace(). - if (((((1 << 0x9) | (1 << 0xA) | (1 << 0xD)) >> charCode) & 1) !== 0) { - buffer.push(char); - } - } else { - buffer.push(char); - } - } - } - - return buffer.join(''); -} diff --git a/js/Global/stripTags.js b/js/Global/stripTags.js deleted file mode 100644 index d4724d35..00000000 --- a/js/Global/stripTags.js +++ /dev/null @@ -1,31 +0,0 @@ -var stripTags = function (str) { - if (str === null) return str; - - var chars = String(str).split(''); - var charCounter = 0; - var inTag = false; - - for (var i = 0, len = str.length; i < len; i += 1) { - if (chars[i] === '<') inTag = true; - - if (!inTag) { - if (i > charCounter) { - chars[charCounter] = chars[i]; - } - - charCounter += 1; - } - - if (chars[i] === '>') { - inTag = false; - } - } - - if (i > charCounter) { - chars.length = charCounter; - return chars.join(''); - } - - return str; -}; - diff --git a/modules/core/Global.js b/modules/core/Global.js index 18dcd43a..b3c2abd8 100644 --- a/modules/core/Global.js +++ b/modules/core/Global.js @@ -17,7 +17,7 @@ /** * @fileoverview Adds useful global macros. *

- * To use this optional module, its repository needs to be added to the + * To use this optional module, its repository needs to be added to the * application, for example by calling app.addRepository('modules/core/Global.js') */ @@ -77,3 +77,180 @@ var skin_macro = function(param, name) { return; } +/** + * Encodes a string for HTML output and inserts linebreak tags. + * + * Performs the following string manipulations: + * All line breaks (i.e. line feeds) are replaced with
tags. + * All special characters are being replaced with their equivalent HTML entity. + * Existing markup tags are being encoded. + * + * @param {string} text + * The string to encode for HTML output. + * @param {boolean} [encodeNewLine = true] + * If or if not to encode line breaks (i.e. line feeds). + * @return {string} + * The encoded string. + */ +function encode(text, encodeNewLine) { + text = String(text); + + if (text === null || !text.length) return text; + var buffer = []; + if (typeof encodeNewline === 'undefined') encodeNewline = true; + + for (var i = 0, len = text.length; i < len; i += 1) { + var char = text.charAt(i); + + switch (char) { + case '<': + buffer.push('<'); + break; + + case '>': + buffer.push('>'); + break; + + case '&': + buffer.push('&'); + break; + + case '"': + buffer.push('"'); + break; + + case '\n': + if (encodeNewline) { + buffer.push("
"); + } + buffer.push('\n'); + break; + + default: + buffer.push(char); + } + } + + return buffer.join(''); +} + +/** + * Encodes a string for XML output. + * + * Performs the following string manipulations: + * All special characters are being replaced with their equivalent HTML entity. + * Existing tags, single and double quotes, as well as ampersands are being encoded. + * Some invalid XML characters below '0x20' are removed + * + * @param {string} text + * The string to encode for XML output. + * @return {string} + * The string encoded for XML output. + */ +function encodeXml(text) { + text = String(text); + + if (text === null || !text.length) return text; + var buffer = []; + + for (var i = 0, len = text.length; i < len; i += 1) { + var char = text.charAt(i); + + switch (char) { + case '<': + buffer.push('<'); + break; + + case '>': + buffer.push('>'); + break; + + case '&': + buffer.push('&'); + break; + + case '"': + buffer.push('"'); + break; + + case '\'': + buffer.push('''); + break; + + default: + var charCode = str.charCodeAt(i); + if (charCode < 0x20) { + // sort out invalid XML characters below 0x20 - all but 0x9, 0xA and 0xD. + // The trick is an adaption of java.lang.Character.isSpace(). + if (((((1 << 0x9) | (1 << 0xA) | (1 << 0xD)) >> charCode) & 1) !== 0) { + buffer.push(char); + } + } else { + buffer.push(char); + } + } + } + + return buffer.join(''); +} + +/** + * Encodes a string for HTML output, leaving linebreaks untouched. + * + * Performs the following string manipulations: + * Unlike encode, leaves linebreaks untouched. This is what you usually want to do for encoding form content (esp. + * with text input values). + * All special characters (i.e. non ASCII) are being replaced with their equivalent HTML entity. + * Existing markup tags are being encoded. + * + * @param {string} text + * The string to format for HTML output. + * @return {string} + * The string formatted for HTML output. + */ +var encodeForm = function(text) { + text = String(text); + + if (text === null || !text.length) return text; + + return encode(str, false); +}; + +/** + * Removes any markup tags contained in the passed string, and returns the modified string. + * + * @param {string} markup + * The text that is to be stripped of tags. + * @return {string} + * The text with the tags stripped out. + */ +var stripTags = function (markup) { + if (markup === null) return markup; + + var chars = String(markup).split(''); + var charCounter = 0; + var inTag = false; + + for (var i = 0, len = markup.length; i < len; i += 1) { + if (chars[i] === '<') inTag = true; + + if (!inTag) { + if (i > charCounter) { + chars[charCounter] = chars[i]; + } + + charCounter += 1; + } + + if (chars[i] === '>') { + inTag = false; + } + } + + if (i > charCounter) { + chars.length = charCounter; + return chars.join(''); + } + + return markup; +}; diff --git a/modules/core/Object.js b/modules/core/Object.js index 5c3f0a58..0bf53006 100644 --- a/modules/core/Object.js +++ b/modules/core/Object.js @@ -21,6 +21,8 @@ * application, for example by calling app.addRepository('modules/core/Object.js') */ +app.addRepository('modules/core/Global.js'); + /** * Copies the properties of this object into a clone. * @external diff --git a/modules/core/String.js b/modules/core/String.js index 79dd050c..d538d3d6 100644 --- a/modules/core/String.js +++ b/modules/core/String.js @@ -42,6 +42,8 @@ String.URLPATTERN = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\ * application, for example by calling app.addRepository('modules/core/String.js') */ +app.addRepository('modules/core/Global.js'); + /** * checks if a date format pattern is correct * @external