1 | <?php |
||
25 | class SafeString extends Encoder |
||
26 | { |
||
27 | const EXTENDED_COMMENT_SEARCH = '/{{!--.*?--}}/s'; |
||
28 | const IS_SUBEXP_SEARCH = '/^\(.+\)$/s'; |
||
29 | const IS_BLOCKPARAM_SEARCH = '/^ +\|(.+)\|$/s'; |
||
30 | |||
31 | private $string; |
||
32 | |||
33 | public static $jsContext = array( |
||
34 | 'flags' => array( |
||
35 | 'jstrue' => 1, |
||
36 | 'jsobj' => 1, |
||
37 | ) |
||
38 | ); |
||
39 | |||
40 | /** |
||
41 | * Constructor |
||
42 | * |
||
43 | * @param string $str input string |
||
44 | * @param bool|string $escape false to not escape, true to escape, 'encq' to escape as handlebars.js |
||
45 | */ |
||
46 | 4 | public function __construct($str, $escape = false) |
|
47 | { |
||
48 | 4 | $this->string = $escape ? (($escape === 'encq') ? static::encq(static::$jsContext, $str) : static::enc(static::$jsContext, $str)) : $str; |
|
49 | 4 | } |
|
50 | |||
51 | 4 | public function __toString() |
|
55 | |||
56 | /** |
||
57 | * Strip extended comments {{!-- .... --}} |
||
58 | * |
||
59 | * @param string $template handlebars template string |
||
60 | * |
||
61 | * @return string Stripped template |
||
62 | * |
||
63 | * @expect 'abc' when input 'abc' |
||
64 | * @expect 'abc{{!}}cde' when input 'abc{{!}}cde' |
||
65 | * @expect 'abc{{! }}cde' when input 'abc{{!----}}cde' |
||
66 | */ |
||
67 | 796 | public static function stripExtendedComments($template) |
|
71 | |||
72 | /** |
||
73 | * Escape template |
||
74 | * |
||
75 | * @param string $template handlebars template string |
||
76 | * |
||
77 | * @return string Escaped template |
||
78 | * |
||
79 | * @expect 'abc' when input 'abc' |
||
80 | * @expect 'a\\\\bc' when input 'a\bc' |
||
81 | * @expect 'a\\\'bc' when input 'a\'bc' |
||
82 | */ |
||
83 | 796 | public static function escapeTemplate($template) |
|
87 | } |
||
88 |