* Add new Filters.js module contributed by Michael Platzer

* Add Filters.js and JSON.js to all.js
This commit is contained in:
hns 2007-04-17 13:43:59 +00:00
parent ab188b2a70
commit 72feb3356a
2 changed files with 211 additions and 3 deletions

206
core/Filters.js Normal file
View file

@ -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, '&', '&amp;'), '"', '&quot;'), '>', '&gt;'), '<', '&lt;');
}
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', '<br />');
}
/**
* 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);
}
}

View file

@ -9,9 +9,9 @@
* Copyright 1998-2006 Helma Software. All Rights Reserved. * Copyright 1998-2006 Helma Software. All Rights Reserved.
* *
* $RCSfile: all.js,v $ * $RCSfile: all.js,v $
* $Author: czv $ * $Author: tobi $
* $Revision: 1.4 $ * $Revision: 1.5 $
* $Date: 2006/05/30 18:34:31 $ * $Date: 2006/08/06 11:27:56 $
*/ */
// convenience SingleFileRepository to load all the // 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/String.js');
app.addRepository('modules/core/HopObject.js'); app.addRepository('modules/core/HopObject.js');
app.addRepository('modules/core/Global.js'); app.addRepository('modules/core/Global.js');
app.addRepository('modules/core/JSON.js');
app.addRepository('modules/core/Filters.js');