diff --git a/core/src/main/java/hudson/Functions.java b/core/src/main/java/hudson/Functions.java index b7f05208e8..9352944e3d 100644 --- a/core/src/main/java/hudson/Functions.java +++ b/core/src/main/java/hudson/Functions.java @@ -758,6 +758,19 @@ public class Functions { return s.indexOf('\r') >= 0 || s.indexOf('\n') >= 0; } + /** + * Percent-encodes space and non-ASCII UTF-8 characters for use in URLs. + *
+ * Input example 1: !"£$%^&*()_+}{:@~?><|¬`,./;'#[]- =
+ * Output example 1: !"%C2%A3$%^&*()_+}{:@~?><|%C2%AC`,./;'#[]-%20=
+ *
+ * Notes:
+ *
+ * Input example 1: & " ' < >
+ * Output example 1: %26+%22+%27+%3C+%3E
+ * Input example 2: !"£$%^&*()_+}{:@~?><|¬`,./;'#[]-=
+ * Output example 2: %21%22%C2%A3%24%25%5E%26*%28%29_%2B%7D%7B%3A%40%7E%3F%3E%3C%7C%C2%AC%60%2C.%2F%3B%27%23%5B%5D-%3D
+ *
+ * Note: A blank space will render as + (You can see this in above examples)
*
* @since 2.200
*/
@@ -776,10 +796,31 @@ public class Functions {
return URLEncoder.encode(s, StandardCharsets.UTF_8);
}
+ /**
+ * Transforms the input string so it renders as written in HTML output: newlines are converted to HTML line breaks, consecutive spaces are retained as {@code }, and HTML metacharacters are escaped.
+ *
+ * Input example 1: & " ' < >
+ * Output example 1: & " ' < >
+ * Input example 2: !"£$%^&*()_+}{:@~?><|¬`,./;'#[]-=
+ * Output example 2: !"£$%^&*()_+}{:@~?><|¬`,./;'#[]-=
+ *
+ * @see #xmlEscape
+ * @see hudson.Util#escape
+ */
public static String escape(String s) {
return Util.escape(s);
}
+ /**
+ * Escapes XML unsafe characters
+ *
+ * Input example 1: < > &
+ * Output example 1: < > &
+ * Input example 2: !"£$%^&*()_+}{:@~?><|¬`,./;'#[]-=
+ * Output example 2: !"£$%^&*()_+}{:@~?><|¬`,./;'#[]-=
+ *
+ * @see hudson.Util#xmlEscape
+ */
public static String xmlEscape(String s) {
return Util.xmlEscape(s);
}
@@ -788,6 +829,16 @@ public class Functions {
return s.replace("<", "<").replace(">", ">").replace("&", "&");
}
+ /**
+ * Escapes a string so it can be used in an HTML attribute value.
+ *
+ * Input example 1: & " ' < >
+ * Output example 1: & " ' < >
+ * Input example 2: !"£$%^&*()_+}{:@~?><|¬`,./;'#[]-=
+ * Output example 2: !"£$%^&*()_+}{:@~?><|¬`,./;'#[]-=
+ *
+ * Note: 2 consecutive blank spaces will not render any special chars.
+ */
public static String htmlAttributeEscape(String text) {
StringBuilder buf = new StringBuilder(text.length() + 64);
for (int i = 0; i < text.length(); i++) {
@@ -1569,6 +1620,15 @@ public class Functions {
return Collections.emptyList();
}
+ /**
+ * Escape a string so variable values can be used in inline JavaScript in views.
+ * Note that inline JavaScript and especially passing variables is discouraged, see the documentation for alternatives.
+ * + * Input example : \ \\ ' " + * Output example: \\ \\\\ \' \" + *+ * @see Passing values to JavaScript + */ public static String jsStringEscape(String s) { if (s == null) return null; StringBuilder buf = new StringBuilder();