ClearSilverCompatibleFunctions.java revision 56ed4167b942ec265f9cee70ac4d71d10b3835ce
1/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.clearsilver.jsilver.functions.bundles;
18
19import com.google.clearsilver.jsilver.functions.escape.*;
20import com.google.clearsilver.jsilver.functions.html.CssUrlValidateFunction;
21import com.google.clearsilver.jsilver.functions.html.HtmlStripFunction;
22import com.google.clearsilver.jsilver.functions.html.HtmlUrlValidateFunction;
23import com.google.clearsilver.jsilver.functions.html.TextHtmlFunction;
24import com.google.clearsilver.jsilver.functions.numeric.AbsFunction;
25import com.google.clearsilver.jsilver.functions.numeric.MaxFunction;
26import com.google.clearsilver.jsilver.functions.numeric.MinFunction;
27import com.google.clearsilver.jsilver.functions.string.CrcFunction;
28import com.google.clearsilver.jsilver.functions.string.FindFunction;
29import com.google.clearsilver.jsilver.functions.string.LengthFunction;
30import com.google.clearsilver.jsilver.functions.string.SliceFunction;
31import com.google.clearsilver.jsilver.functions.structure.FirstFunction;
32import com.google.clearsilver.jsilver.functions.structure.LastFunction;
33import com.google.clearsilver.jsilver.functions.structure.SubcountFunction;
34
35/**
36 * Set of functions required to allow JSilver to be compatible with ClearSilver.
37 */
38public class ClearSilverCompatibleFunctions extends CoreOperators {
39
40  @Override
41  protected void setupDefaultFunctions() {
42    super.setupDefaultFunctions();
43
44    // Structure functions.
45    registerFunction("subcount", new SubcountFunction());
46    registerFunction("first", new FirstFunction());
47    registerFunction("last", new LastFunction());
48
49    // Deprecated - but here for ClearSilver compatibility.
50    registerFunction("len", new SubcountFunction());
51
52    // Numeric functions.
53    registerFunction("abs", new AbsFunction());
54    registerFunction("max", new MaxFunction());
55    registerFunction("min", new MinFunction());
56
57    // String functions.
58    registerFunction("string.slice", new SliceFunction());
59    registerFunction("string.find", new FindFunction());
60    registerFunction("string.length", new LengthFunction());
61    registerFunction("string.crc", new CrcFunction());
62
63    // Escaping functions.
64    registerFunction("url_escape", new UrlEscapeFunction("UTF-8"), true);
65    registerEscapeMode("url", new UrlEscapeFunction("UTF-8"));
66    registerFunction("html_escape", new HtmlEscapeFunction(false), true);
67    registerEscapeMode("html", new HtmlEscapeFunction(false));
68    registerFunction("js_escape", new JsEscapeFunction(false), true);
69    registerEscapeMode("js", new JsEscapeFunction(false));
70
71    // These functions are available as arguments to <?cs escape: ?>
72    // though they aren't in ClearSilver. This is so that auto escaping
73    // can automatically add <?cs escape ?> nodes with these modes
74    registerEscapeMode("html_unquoted", new HtmlEscapeFunction(true));
75    registerEscapeMode("js_attr_unquoted", new JsEscapeFunction(true));
76    registerEscapeMode("js_check_number", new JsValidateUnquotedLiteral());
77    registerEscapeMode("url_validate_unquoted", new HtmlUrlValidateFunction(true));
78
79    registerEscapeMode("css", new StyleEscapeFunction(false));
80    registerEscapeMode("css_unquoted", new StyleEscapeFunction(true));
81
82    // HTML functions.
83    registerFunction("html_strip", new HtmlStripFunction());
84    registerFunction("text_html", new TextHtmlFunction());
85
86    // url_validate is available as an argument to <?cs escape: ?>
87    // though it isn't in ClearSilver.
88    registerFunction("url_validate", new HtmlUrlValidateFunction(false), true);
89    registerEscapeMode("url_validate", new HtmlUrlValidateFunction(false));
90
91    registerFunction("css_url_validate", new CssUrlValidateFunction(), true);
92    // Register as an EscapingFunction so that autoescaping will be disabled
93    // for the output of this function.
94    registerFunction("null_escape", new NullEscapeFunction(), true);
95  }
96
97}
98