--- /dev/null
+<?php
+namespace wcf\system\template\plugin;
+use wcf\system\template\TemplateEngine;
+use wcf\util\StringUtil;
+
+/**
+ * Template modifier plugin which formats a JSON string for usage in a single quoted
+ * javascript string by escapes single quotes and new lines.
+ *
+ * Usage:
+ * {$string|encodeJSON}
+ * {"bl''ah"|encodeJSON}
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2013 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package com.woltlab.wcf
+ * @subpackage system.template.plugin
+ * @category Community Framework
+ */
+class EncodeJSONModifierTemplatePlugin implements IModifierTemplatePlugin {
+ /**
+ * @see wcf\system\template\IModifierTemplatePlugin::execute()
+ */
+ public function execute($tagArgs, TemplateEngine $tplObj) {
+ return StringUtil::encodeJSON($tagArgs[0]);
+ }
+}
return $string;
}
+ /**
+ * Encodes JSON strings. This is not the same as PHP's json_encode()!
+ *
+ * @param string $string
+ * @return string
+ */
+ public static function encodeJSON($string) {
+ $string = self::encodeJS($string);
+
+ // single quotes must be encoded as HTML entity
+ $string = self::replace("\'", "'", $string);
+
+ return $string;
+ }
+
/**
* Decodes html entities.
*