From 72feb3356aac253f0935d8b4828ccc98af6bb088 Mon Sep 17 00:00:00 2001 From: hns Date: Tue, 17 Apr 2007 13:43:59 +0000 Subject: [PATCH] * Add new Filters.js module contributed by Michael Platzer * Add Filters.js and JSON.js to all.js --- core/Filters.js | 206 ++++++++++++++++++++++++++++++++++++++++++++++++ core/all.js | 8 +- 2 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 core/Filters.js diff --git a/core/Filters.js b/core/Filters.js new file mode 100644 index 00000000..94111f81 --- /dev/null +++ b/core/Filters.js @@ -0,0 +1,206 @@ +/* + * Helma License Notice + * + * The contents of this file are subject to the Helma License + * Version 2.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://adele.helma.org/download/helma/license.txt + * + * Copyright 1998-2007 Helma Software. All Rights Reserved. + * + * $RCSfile$ + * $Author$ + * $Revision$ + * $Date$ + */ + +/** + * @fileoverview Implements some useful macro filters. + */ + +app.addRepository('modules/core/String.js'); + +/** + * Transforms a string to lowercase. + * + * @see String.prototype.toLowerCase + */ +function lowercase_filter(input) { + return (input || "").toString().toLowerCase(); +} + + +/** + * Transforms a string to uppercase. + * + * @see String.prototype.toUpperCase + */ +function uppercase_filter(input) { + return (input || "").toString().toUpperCase(); +} + + +/** + * Transforms the first Character of a string to uppercase. + * + * @see String.prototype.capitalize + */ +function capitalize_filter(input) { + return (input || "").toString().capitalize(); +} + + +/** + * Transforms the first Character of each word in a string + * to uppercase. + * + * @see String.prototype.titleize + */ +function titleize_filter(input) { + return (input || "").toString().titleize(); +} + + +/** + * Cuts a String at a certain position, and + * optionally appends a suffix, if truncation + * has occurred. + * + * @see String.prototype.head + * @param limit Maximum length + * @param clipping Appended String, default is the empty String + */ +function truncate_filter(input, param, limit, clipping) { + var limit = param.limit != null ? param.limit : limit; + var clipping = param.clipping || clipping || ""; + return (input || "").toString().head(limit, clipping); +} + + +/** + * Removes leading and trailing whitespaces. + * + * @see String.prototype.trim + */ +function trim_filter(input) { + return (input || "").toString().trim(); +} + + +/** + * Removes all tags from a String. + * Currently simply wraps Helma's stripTags-method. + * + * @see stripTags + */ +function stripTags_filter(input) { + return stripTags((input || "").toString()); +}; + + +/** + * Escapes the characters in a String using XML entities. + * Currently simply wraps Helma's encodeXml-method. + * + * @see encodeXml + */ +function escapeXml_filter(input) { + return encodeXml((input || "").toString()); +} + + +/** + * Escapes the characters in a String using HTML entities. + * + * @see http://www.google.com/codesearch?q=escapeHtml + */ +function escapeHtml_filter(input) { + var replace = Packages.org.mortbay.util.StringUtil.replace; + var str = (input || "").toString(); + return replace(replace(replace(replace(str, '&', '&'), '"', '"'), '>', '>'), '<', '<'); +} + +var h_filter = escapeHtml_filter; + + +/** + * Escapes the characters in a String to be suitable + * to use as an HTTP parameter value. + * + * @see http://www.google.com/codesearch?q=escapeUrl + * @param charset Optional String. The name of a supported + * character encoding. + */ +function escapeUrl_filter(input, param, charset) { + var charset = param.charset || charset || app.getCharset(); + return java.net.URLEncoder.encode(input || "", charset); +} + + +/** + * Escapes a string so it may be used in JavaScript String + * definitions. + */ +function escapeJavaScript_filter(input) { + var replace = Packages.org.mortbay.util.StringUtil.replace; + var str = (input || "").toString(); + return replace(replace(replace(replace(replace(str, '"', '\\"'), "'", "\\'"), '\n', '\\n'), '\r', '\\r'), '\t', '\\t'); +} + + +/** + * Replaces linebreaks with HTML linebreaks. + */ +function linebreakToHtml_filter(input) { + var replace = Packages.org.mortbay.util.StringUtil.replace; + var str = (input || "").toString(); + return replace(str, '\n', '
'); +} + + +/** + * Performs a string replacement. + * + * @param old + * @param new + */ +function replace_filter(input, param, oldString, newString) { + var str = (input || "").toString(); + var oldString = param["old"] != null ? param["old"] : oldString; + var newString = param["new"] != null ? param["new"] : newString; + var replace = Packages.org.mortbay.util.StringUtil.replace; + return replace(str, oldString, newString); +} + + +/** + * Returns a substring. Simply wraps the javascript + * method 'substring'. + * + * @see String.prototype.substring + * @param from + * @param to + */ +function substring_filter(input, param, from, to) { + var from = param.from != null ? param.from : from; + var to = param.to != null ? param.to : to; + var str = (input || "").toString(); + return str.substring(from, to); +} + + +/** + * Returns a formatted string representation of a Date. + * Simply wraps javascripts Date.format-method. + * + * @see Date.prototype.format + * @param format + */ +function dateFormat_filter(input, param, format) { + var format = param.format || format; + if (!input) { + return; + } else { + return input.format(format); + } +} diff --git a/core/all.js b/core/all.js index 8f107375..71474844 100644 --- a/core/all.js +++ b/core/all.js @@ -9,9 +9,9 @@ * Copyright 1998-2006 Helma Software. All Rights Reserved. * * $RCSfile: all.js,v $ - * $Author: czv $ - * $Revision: 1.4 $ - * $Date: 2006/05/30 18:34:31 $ + * $Author: tobi $ + * $Revision: 1.5 $ + * $Date: 2006/08/06 11:27:56 $ */ // convenience SingleFileRepository to load all the @@ -24,3 +24,5 @@ app.addRepository('modules/core/Object.js'); app.addRepository('modules/core/String.js'); app.addRepository('modules/core/HopObject.js'); app.addRepository('modules/core/Global.js'); +app.addRepository('modules/core/JSON.js'); +app.addRepository('modules/core/Filters.js');