From 1f434f652e1878f55e7bb24f746a4beca57689a4 Mon Sep 17 00:00:00 2001 From: webchick Date: Mon, 17 Oct 2011 08:28:42 -0700 Subject: =?UTF-8?q?Issue=20#1191614=20by=20G=C3=A1bor=20Hojtsy:=20Added=20?= =?UTF-8?q?Make=20t()=20formatter=20available=20as=20its=20own=20function.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- misc/drupal.js | 72 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 28 deletions(-) (limited to 'misc') diff --git a/misc/drupal.js b/misc/drupal.js index 3cebbd284..7e2cc4d7b 100644 --- a/misc/drupal.js +++ b/misc/drupal.js @@ -111,6 +111,8 @@ Drupal.detachBehaviors = function (context, settings, trigger) { /** * Encode special characters in a plain-text string for display as HTML. + * + * @ingroup sanitization */ Drupal.checkPlain = function (str) { var character, regex, @@ -125,6 +127,45 @@ Drupal.checkPlain = function (str) { return str; }; +/** + * Replace placeholders with sanitized values in a string. + * + * @param str + * A string with placeholders. + * @param args + * An object of replacements pairs to make. Incidences of any key in this + * array are replaced with the corresponding value. Based on the first + * character of the key, the value is escaped and/or themed: + * - !variable: inserted as is + * - @variable: escape plain text to HTML (Drupal.checkPlain) + * - %variable: escape text and theme as a placeholder for user-submitted + * content (checkPlain + Drupal.theme('placeholder')) + * + * @see Drupal.t() + * @ingroup sanitization + */ +Drupal.formatString = function(str, args) { + // Transform arguments before inserting them. + for (var key in args) { + switch (key.charAt(0)) { + // Escaped only. + case '@': + args[key] = Drupal.checkPlain(args[key]); + break; + // Pass-through. + case '!': + break; + // Escaped and placeholder. + case '%': + default: + args[key] = Drupal.theme('placeholder', args[key]); + break; + } + str = str.replace(key, args[key]); + } + return str; +} + /** * Translate strings to the page language or a given language. * @@ -135,11 +176,7 @@ Drupal.checkPlain = function (str) { * @param args * An object of replacements pairs to make after translation. Incidences * of any key in this array are replaced with the corresponding value. - * Based on the first character of the key, the value is escaped and/or themed: - * - !variable: inserted as is - * - @variable: escape plain text to HTML (Drupal.checkPlain) - * - %variable: escape text and theme as a placeholder for user-submitted - * content (checkPlain + Drupal.theme('placeholder')) + * See Drupal.formatString(). * @return * The translated string. */ @@ -150,24 +187,7 @@ Drupal.t = function (str, args) { } if (args) { - // Transform arguments before inserting them. - for (var key in args) { - switch (key.charAt(0)) { - // Escaped only. - case '@': - args[key] = Drupal.checkPlain(args[key]); - break; - // Pass-through. - case '!': - break; - // Escaped and placeholder. - case '%': - default: - args[key] = Drupal.theme('placeholder', args[key]); - break; - } - str = str.replace(key, args[key]); - } + str = Drupal.formatString(str, args); } return str; }; @@ -193,11 +213,7 @@ Drupal.t = function (str, args) { * @param args * An object of replacements pairs to make after translation. Incidences * of any key in this array are replaced with the corresponding value. - * Based on the first character of the key, the value is escaped and/or themed: - * - !variable: inserted as is - * - @variable: escape plain text to HTML (Drupal.checkPlain) - * - %variable: escape text and theme as a placeholder for user-submitted - * content (checkPlain + Drupal.theme('placeholder')) + * See Drupal.formatString(). * Note that you do not need to include @count in this array. * This replacement is done automatically for the plural case. * @return -- cgit v1.2.3