Home | History | Annotate | Download | only in escape
      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 
     17 package com.google.clearsilver.jsilver.functions.escape;
     18 
     19 
     20 /**
     21  * This Javascript escapes the string so it will be valid data for placement into a Javascript
     22  * string. This converts characters such as ", ', and \ into their Javascript string safe
     23  * equivilants \", \', and \\.
     24  *
     25  * This behaves in the same way as the ClearSilver js_escape function.
     26  *
     27  * This implementation has been optimized for performance.
     28  */
     29 public class JsEscapeFunction extends SimpleEscapingFunction {
     30 
     31   private static final char[] DIGITS = "0123456789ABCDEF".toCharArray();
     32 
     33   private static final char[] ESCAPE_CHARS;
     34 
     35   private static final char[] UNQUOTED_ESCAPE_CHARS;
     36 
     37   static {
     38     char[] SPECIAL_CHARS = {'/', '"', '\'', '\\', '>', '<', '&', ';'};
     39     char[] UNQUOTED_SPECIAL_CHARS = {'/', '"', '\'', '\\', '>', '<', '&', ';', '=', ' '};
     40 
     41     ESCAPE_CHARS = new char[32 + SPECIAL_CHARS.length];
     42     UNQUOTED_ESCAPE_CHARS = new char[33 + UNQUOTED_SPECIAL_CHARS.length];
     43     for (int n = 0; n < 32; n++) {
     44       ESCAPE_CHARS[n] = (char) n;
     45       UNQUOTED_ESCAPE_CHARS[n] = (char) n;
     46     }
     47 
     48     System.arraycopy(SPECIAL_CHARS, 0, ESCAPE_CHARS, 32, SPECIAL_CHARS.length);
     49 
     50     UNQUOTED_ESCAPE_CHARS[32] = 0x7F;
     51     System.arraycopy(UNQUOTED_SPECIAL_CHARS, 0, UNQUOTED_ESCAPE_CHARS, 33,
     52         UNQUOTED_SPECIAL_CHARS.length);
     53   }
     54 
     55   /**
     56    * isUnquoted should be true if the function is escaping a string that will appear inside an
     57    * unquoted JS attribute (like onClick or onMouseover).
     58    *
     59    */
     60   public JsEscapeFunction(boolean isAttrUnquoted) {
     61     if (isAttrUnquoted) {
     62       super.setEscapeChars(UNQUOTED_ESCAPE_CHARS);
     63     } else {
     64       super.setEscapeChars(ESCAPE_CHARS);
     65     }
     66   }
     67 
     68   @Override
     69   protected String getEscapeString(char c) {
     70     return "\\x" + DIGITS[(c >> 4) & 0xF] + DIGITS[c & 0xF];
     71   }
     72 }
     73