1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of Aura for PHP. |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
namespace Aura\Html; |
10
|
|
|
|
11
|
|
|
use Aura\Html\Escaper\HtmlEscaper; |
12
|
|
|
use Aura\Html\Escaper\AttrEscaper; |
13
|
|
|
use Aura\Html\Escaper\CssEscaper; |
14
|
|
|
use Aura\Html\Escaper\JsEscaper; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
* A proxy for the various escapers. |
19
|
|
|
* |
20
|
|
|
* @package Aura.Html |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
class Escaper |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* A singleton instance of an Escaper, typically for view scripts. |
28
|
|
|
* |
29
|
|
|
* @var Escaper |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
static protected $escaper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
* An HtmlEscaper instance. |
37
|
|
|
* |
38
|
|
|
* @var HtmlEscaper |
39
|
|
|
* |
40
|
|
|
*/ |
41
|
|
|
protected $html; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* |
45
|
|
|
* An AttrEscaper instance. |
46
|
|
|
* |
47
|
|
|
* @var AttrEscaper |
48
|
|
|
* |
49
|
|
|
*/ |
50
|
|
|
protected $attr; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
* A CssEscaper instance. |
55
|
|
|
* |
56
|
|
|
* @var CssEscaper |
57
|
|
|
* |
58
|
|
|
*/ |
59
|
|
|
protected $css; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
* A JsEscaper instance. |
64
|
|
|
* |
65
|
|
|
* @var JsEscaper |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
protected $js; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
* Constructor. |
73
|
|
|
* |
74
|
|
|
* @param HtmlEscaper $html An HtmlEscaper instance. |
75
|
|
|
* |
76
|
|
|
* @param AttrEscaper $attr An AttrEscaper instance. |
77
|
|
|
* |
78
|
|
|
* @param CssEscaper $css A CssEscaper instance. |
79
|
|
|
* |
80
|
|
|
* @param JsEscaper $js A JsEscaper instance. |
81
|
|
|
* |
82
|
|
|
*/ |
83
|
89 |
|
public function __construct( |
84
|
|
|
HtmlEscaper $html, |
85
|
|
|
AttrEscaper $attr, |
86
|
|
|
CssEscaper $css, |
87
|
|
|
JsEscaper $js |
88
|
|
|
) { |
89
|
89 |
|
$this->html = $html; |
90
|
89 |
|
$this->attr = $attr; |
91
|
89 |
|
$this->css = $css; |
92
|
89 |
|
$this->js = $js; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* |
97
|
|
|
* Read-only property access; mostly for testing. |
98
|
|
|
* |
99
|
|
|
* @param string $key The property name. |
100
|
|
|
* |
101
|
|
|
* @return mixed |
102
|
|
|
* |
103
|
|
|
*/ |
104
|
|
|
public function __get($key) |
105
|
|
|
{ |
106
|
|
|
return $this->$key; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* |
111
|
|
|
* Allows this object to be used as a helper. |
112
|
|
|
* |
113
|
|
|
* @return self |
114
|
|
|
* |
115
|
|
|
*/ |
116
|
1 |
|
public function __invoke() |
117
|
|
|
{ |
118
|
1 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* |
123
|
|
|
* Sets the encoding on all escapers. |
124
|
|
|
* |
125
|
|
|
* @param string $encoding The encoding to use. |
126
|
|
|
* |
127
|
|
|
* @return null |
128
|
|
|
* |
129
|
|
|
*/ |
130
|
1 |
|
public function setEncoding($encoding) |
131
|
|
|
{ |
132
|
|
|
$this->html->setEncoding($encoding); |
133
|
|
|
$this->attr->setEncoding($encoding); |
134
|
|
|
$this->css->setEncoding($encoding); |
135
|
|
|
$this->js->setEncoding($encoding); |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
* Sets the flags for `htmlspecialchars()` on the Html and Attr escapers. |
141
|
|
|
* |
142
|
|
|
* @param int $flags The `htmlspecialchars()` flags. |
143
|
|
|
* |
144
|
|
|
* @return null |
145
|
|
|
* |
146
|
|
|
*/ |
147
|
1 |
|
public function setFlags($flags) |
148
|
|
|
{ |
149
|
|
|
$this->html->setFlags($flags); |
150
|
|
|
$this->attr->getHtml()->setFlags($flags); |
151
|
1 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* |
155
|
|
|
* Escapes for HTML body and quoted HTML attribute context. |
156
|
|
|
* |
157
|
|
|
* @param string $raw The raw string. |
158
|
|
|
* |
159
|
|
|
* @return string The escaped string. |
160
|
|
|
* |
161
|
|
|
*/ |
162
|
|
|
public function html($raw) |
163
|
|
|
{ |
164
|
|
|
return $this->html->__invoke($raw); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* |
169
|
|
|
* Escapes for unquoted HTML attribute context. |
170
|
|
|
* |
171
|
|
|
* @param string|array $raw The raw attribute (or array of attributes). |
172
|
|
|
* |
173
|
|
|
* @return string The escaped string. |
174
|
|
|
* |
175
|
|
|
*/ |
176
|
|
|
public function attr($raw) |
177
|
|
|
{ |
178
|
|
|
return $this->attr->__invoke($raw); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* |
183
|
|
|
* Escapes for CSS context. |
184
|
|
|
* |
185
|
|
|
* @param string $raw The raw string. |
186
|
|
|
* |
187
|
|
|
* @return string The escaped string. |
188
|
|
|
* |
189
|
|
|
*/ |
190
|
|
|
public function css($raw) |
191
|
|
|
{ |
192
|
|
|
return $this->css->__invoke($raw); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* |
197
|
|
|
* Escapes for JavaScript context. |
198
|
|
|
* |
199
|
|
|
* @param string $raw The raw string. |
200
|
|
|
* |
201
|
|
|
* @return string The escaped string. |
202
|
|
|
* |
203
|
|
|
*/ |
204
|
|
|
public function js($raw) |
205
|
|
|
{ |
206
|
|
|
return $this->js->__invoke($raw); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* |
211
|
|
|
* Sets the static singleton escaper instance. |
212
|
|
|
* |
213
|
|
|
* @param Escaper $escaper The Escaper to use as the singleton. |
214
|
|
|
* |
215
|
|
|
* @return null |
216
|
|
|
* |
217
|
|
|
*/ |
218
|
14 |
|
public static function setStatic(Escaper $escaper) |
219
|
|
|
{ |
220
|
14 |
|
static::$escaper = $escaper; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* |
225
|
|
|
* Gets the static singleton escaper instance. |
226
|
|
|
* |
227
|
|
|
* @return Escaper |
228
|
|
|
* |
229
|
|
|
*/ |
230
|
1 |
|
public static function getStatic() |
231
|
|
|
{ |
232
|
1 |
|
return static::$escaper; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* |
237
|
|
|
* Static escaping for HTML body and quoted HTML attribute context. |
238
|
|
|
* |
239
|
|
|
* @param string $raw The raw string. |
240
|
|
|
* |
241
|
|
|
* @return string The escaped string. |
242
|
|
|
* |
243
|
|
|
*/ |
244
|
|
|
public static function h($raw) |
245
|
|
|
{ |
246
|
|
|
return static::$escaper->html($raw); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* |
251
|
|
|
* Static escaping for unquoted HTML attribute context. |
252
|
|
|
* |
253
|
|
|
* @param string $raw The raw string. |
254
|
|
|
* |
255
|
|
|
* @return string The escaped string. |
256
|
|
|
* |
257
|
|
|
*/ |
258
|
|
|
public static function a($raw) |
259
|
|
|
{ |
260
|
|
|
return static::$escaper->attr($raw); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* |
265
|
|
|
* Static escaping for CSS context. |
266
|
|
|
* |
267
|
|
|
* @param string $raw The raw string. |
268
|
|
|
* |
269
|
|
|
* @return string The escaped string. |
270
|
|
|
* |
271
|
|
|
*/ |
272
|
|
|
public static function c($raw) |
273
|
|
|
{ |
274
|
|
|
return static::$escaper->css($raw); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* |
279
|
|
|
* Static escaping for JavaScript context. |
280
|
|
|
* |
281
|
|
|
* @param string $raw The raw string. |
282
|
|
|
* |
283
|
|
|
* @return string The escaped string. |
284
|
|
|
* |
285
|
|
|
*/ |
286
|
|
|
public static function j($raw) |
287
|
|
|
{ |
288
|
|
|
return static::$escaper->js($raw); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|