1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NatePage\EasyHtmlElement; |
4
|
|
|
|
5
|
|
|
use Zend\Escaper\Escaper as BaseEscaper; |
6
|
|
|
|
7
|
|
|
class Escaper extends BaseEscaper implements EscaperInterface |
8
|
|
|
{ |
9
|
|
|
/** @var array The special escaping types */ |
10
|
|
|
private $specialEscapingTypes = array('script', 'style'); |
11
|
|
|
|
12
|
|
|
/** @var array The urls attributes */ |
13
|
|
|
private $urlsAttributes = array('href', 'src'); |
14
|
|
|
|
15
|
|
|
/** @var bool Determine if html is escaped or not */ |
16
|
|
|
private $escapeHtml = true; |
17
|
|
|
|
18
|
|
|
/** @var bool Determine if html attributes are escaped or not */ |
19
|
|
|
private $escapeHtmlAttr = true; |
20
|
|
|
|
21
|
|
|
/** @var bool Determine if javascript is escaped or not */ |
22
|
|
|
private $escapeJs = true; |
23
|
|
|
|
24
|
|
|
/** @var bool Determine if css is escaped or not */ |
25
|
|
|
private $escapeCss = true; |
26
|
|
|
|
27
|
|
|
/** @var bool Determine if urls parameters are escaped or not */ |
28
|
|
|
private $escapeUrl = true; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function escapeAttributes(array $attributes) |
34
|
|
|
{ |
35
|
|
|
if ($this->escapeHtmlAttr || $this->escapeUrl) { |
36
|
|
|
foreach ($attributes as $attr => $value) { |
37
|
|
|
if (!in_array($attr, $this->urlsAttributes)) { |
38
|
|
|
if ($this->escapeHtmlAttr) { |
39
|
|
|
$value = $this->escapeHtmlAttr($value); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$attributes[$attr] = $value; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $attributes; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function escape(ElementInterface $element) |
54
|
|
|
{ |
55
|
|
|
if ($this->escapeHtml && !in_array($element->getType(), $this->specialEscapingTypes)) { |
56
|
|
|
$element->setText($this->escapeHtml($element->getText())); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$element->setAttributes($this->escapeAttributes($element->getAttributes())); |
60
|
|
|
|
61
|
|
|
if ($this->escapeJs && 'script' == $element->getType()) { |
62
|
|
|
$element->setText($this->escapeJs($element->getText())); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->escapeCss && 'style' == $element->getType()) { |
66
|
|
|
$element->setText($this->escapeCss($element->getText())); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $element; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function escapeUrlParameter($parameter) |
76
|
|
|
{ |
77
|
|
|
return $this->escapeUrl($parameter); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function isEscapeHtml() |
84
|
|
|
{ |
85
|
|
|
return $this->escapeHtml; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function setEscapeHtml($escapeHtml = true) |
92
|
|
|
{ |
93
|
|
|
$this->escapeHtml = $escapeHtml; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function isEscapeHtmlAttr() |
102
|
|
|
{ |
103
|
|
|
return $this->escapeHtmlAttr; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function setEscapeHtmlAttr($escapeHtmlAttr = true) |
110
|
|
|
{ |
111
|
|
|
$this->escapeHtmlAttr = $escapeHtmlAttr; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function isEscapeJs() |
120
|
|
|
{ |
121
|
|
|
return $this->escapeJs; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function setEscapeJs($escapeJs = true) |
128
|
|
|
{ |
129
|
|
|
$this->escapeJs = $escapeJs; |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function isEscapeCss() |
138
|
|
|
{ |
139
|
|
|
return $this->escapeCss; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function setEscapeCss($escapeCss = true) |
146
|
|
|
{ |
147
|
|
|
$this->escapeCss = $escapeCss; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
|
|
public function isEscapeUrl() |
156
|
|
|
{ |
157
|
|
|
return $this->escapeUrl; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
|
public function setEscapeUrl($escapeUrl = true) |
164
|
|
|
{ |
165
|
|
|
$this->escapeUrl = $escapeUrl; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function getUrlsAttributes() |
174
|
|
|
{ |
175
|
|
|
return $this->urlsAttributes; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|