1 16 17 package org.springframework.web.util; 18 19 32 public class JavaScriptUtils { 33 34 40 public static String javaScriptEscape(String input) { 41 if (input == null) { 42 return input; 43 } 44 45 StringBuffer filtered = new StringBuffer (input.length()); 46 char prevChar = '\u0000'; 47 char c; 48 for (int i = 0; i < input.length(); i++) { 49 c = input.charAt(i); 50 if (c == '"') { 51 filtered.append("\\\""); 52 } 53 else if (c == '\'') { 54 filtered.append("\\'"); 55 } 56 else if (c == '\\') { 57 filtered.append("\\\\"); 58 } 59 else if (c == '/') { 60 filtered.append("\\/"); 61 } 62 else if (c == '\t') { 63 filtered.append("\\t"); 64 } 65 else if (c == '\n') { 66 if (prevChar != '\r') { 67 filtered.append("\\n"); 68 } 69 } 70 else if (c == '\r') { 71 filtered.append("\\n"); 72 } 73 else if (c == '\f') { 74 filtered.append("\\f"); 75 } 76 else { 77 filtered.append(c); 78 } 79 prevChar = c; 80 81 } 82 return filtered.toString(); 83 } 84 85 } 86 | Popular Tags |