View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.renderkit.util;
21  
22  /**
23   * @deprecated since 2.0.0. JavaScript should not be used no server side (CSP)
24   */
25  @Deprecated
26  public class JQueryUtils {
27  
28    private JQueryUtils() {
29      // to prevent instantiation
30    }
31  
32    /**
33     * Puts two backslashes before : and . to escape them.
34     * Two are needed, because of JavaScript string literals. 
35     */
36    public static String escapeId(String id) {
37      return id.replaceAll("([\\:\\.])", "\\\\\\\\$1");
38    }
39  
40    /**
41     * Puts one backslashes before \ to escape it.
42     * It is needed, because of JavaScript string literals. 
43     */
44    public static String escapeValue(String value) {
45      return value.replaceAll("\\\\", "\\\\\\\\");
46    }
47  
48    /**
49     * Puts two backslashes before #;&,.+*~':"!^$[]()=>|/ to escape them.
50     * Two are needed, because of JavaScript string literals.
51     * Puts three backslashes before a \ itself, to escape it. 
52     */
53    public static String escapeSelector(String value) {
54      StringBuilder builder = new StringBuilder();
55      for (char c : value.toCharArray()) {
56        switch (c) {
57          case '\\':
58            builder.append("\\\\\\\\");
59            break;
60          case '#':
61          case ';':
62          case '&':
63          case ',':
64          case '.':
65          case '+':
66          case '*':
67          case '~':
68          case '\'':
69          case ':':
70          case '"':
71          case '!':
72          case '^':
73          case '$':
74          case '[':
75          case ']':
76          case '(':
77          case ')':
78          case '=':
79          case '>':
80          case '|':
81          case '/':
82            builder.append("\\\\");
83          default:
84            builder.append(c);
85            break;
86        }
87      }
88      return builder.toString();
89    }
90  
91    /**
92     * Creates a selector for an id like jQuery('#id').
93     * The id will be escaped if necessary.
94     */
95    public static String selectId(String id) {
96      StringBuilder builder = new StringBuilder();
97      builder.append("jQuery('#");
98      builder.append(escapeId(id));
99      builder.append("')");
100     return builder.toString();
101   }
102 }