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\Helper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* Helper for a stack of <script> tags. |
14
|
|
|
* |
15
|
|
|
* @package Aura.Html |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class Scripts extends AbstractSeries |
19
|
|
|
{ |
20
|
|
|
private $capture; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* Adds a <script> tag to the stack. |
25
|
|
|
* |
26
|
|
|
* @param string $src The source href for the script. |
27
|
|
|
* |
28
|
|
|
* @param int $pos The script position in the stack. |
29
|
|
|
* |
30
|
|
|
* @param array $attr The additional attributes |
31
|
|
|
* |
32
|
|
|
* @return null |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
2 |
|
public function add($src, $pos = 100, $attr = array()) |
36
|
|
|
{ |
37
|
2 |
|
$attr = $this->attr($src, $attr); |
38
|
2 |
|
$tag = "<script $attr></script>"; |
39
|
2 |
|
$this->addElement($pos, $tag); |
40
|
|
|
|
41
|
2 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* Adds a conditional `<!--[if ...]><script><![endif] -->` tag to the |
47
|
|
|
* stack. |
48
|
|
|
* |
49
|
|
|
* @param string $cond The conditional expression for the script. |
50
|
|
|
* |
51
|
|
|
* @param string $src The source href for the script. |
52
|
|
|
* |
53
|
|
|
* @param string $pos The script position in the stack. |
54
|
|
|
* |
55
|
|
|
* @param array $attr The additional attributes |
56
|
|
|
* |
57
|
|
|
* @return null |
58
|
|
|
* |
59
|
|
|
*/ |
60
|
1 |
|
public function addCond($cond, $src, $pos = 100, array $attr = array()) |
61
|
|
|
{ |
62
|
1 |
|
$cond = $this->escaper->html($cond); |
63
|
1 |
|
$attr = $this->attr($src, $attr); |
64
|
1 |
|
$tag = "<!--[if $cond]><script $attr></script><![endif]-->"; |
65
|
1 |
|
$this->addElement($pos, $tag); |
66
|
|
|
|
67
|
1 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds internal script |
72
|
|
|
* |
73
|
|
|
* @param mixed $script The script |
74
|
|
|
* @param int $pos The script position in the stack. |
75
|
|
|
* @param array $attr The additional attributes |
76
|
|
|
* |
77
|
|
|
* @return Scripts |
78
|
|
|
* |
79
|
|
|
* @access public |
80
|
|
|
*/ |
81
|
1 |
|
public function addInternal($script, $pos = 100, array $attr = array()) |
82
|
|
|
{ |
83
|
1 |
|
$attr = $this->attr(null, $attr); |
84
|
1 |
|
$tag = "<script $attr>$script</script>"; |
85
|
1 |
|
$this->addElement($pos, $tag); |
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add Conditional internal script |
91
|
|
|
* |
92
|
|
|
* @param mixed $cond The conditional expression for the script. |
93
|
|
|
* @param mixed $script The script |
94
|
|
|
* @param int $pos The script position in the stack. |
95
|
|
|
* @param array $attr The additional attributes |
96
|
|
|
* |
97
|
|
|
* @return Scripts |
98
|
|
|
* |
99
|
|
|
* @access public |
100
|
|
|
*/ |
101
|
1 |
|
public function addCondInternal($cond, $script, $pos = 100, array $attr = array()) |
102
|
|
|
{ |
103
|
1 |
|
$cond = $this->escaper->html($cond); |
104
|
1 |
|
$attr = $this->attr(null, $attr); |
105
|
1 |
|
$tag = "<!--[if $cond]><script $attr>$script</script><![endif]-->"; |
106
|
1 |
|
$this->addElement($pos, $tag); |
107
|
|
|
|
108
|
1 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Adds internal script |
113
|
|
|
* |
114
|
|
|
* @param int $pos The script position in the stack. |
115
|
|
|
* @param array $attr The additional attributes |
116
|
|
|
* |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
* |
120
|
|
|
* @access public |
121
|
|
|
*/ |
122
|
1 |
|
public function beginInternal($pos = 100, array $attr = array()) |
123
|
|
|
{ |
124
|
1 |
|
$this->capture[] = array($pos, $attr); |
125
|
1 |
|
ob_start(); |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Begin Conditional Internal Capture |
130
|
|
|
* |
131
|
|
|
* @param mixed $cond condition |
132
|
|
|
* @param int $pos position |
133
|
|
|
* @param array $attr The additional attributes |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
* |
137
|
|
|
* @access public |
138
|
|
|
*/ |
139
|
1 |
|
public function beginCondInternal($cond, $pos = 100, array $attr = array()) |
140
|
|
|
{ |
141
|
1 |
|
$this->capture[] = array($cond, $pos, $attr); |
142
|
1 |
|
ob_start(); |
143
|
1 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* End internal script capture |
147
|
|
|
* |
148
|
|
|
* @return mixed |
149
|
|
|
* |
150
|
|
|
* @access public |
151
|
|
|
*/ |
152
|
1 |
|
public function endInternal() |
153
|
|
|
{ |
154
|
1 |
|
$script = ob_get_clean(); |
155
|
1 |
|
$params = array_pop($this->capture); |
156
|
1 |
|
if (count($params) > 2) { |
157
|
1 |
|
return $this->addCondInternal( |
158
|
1 |
|
$params[0], |
159
|
1 |
|
$script, |
160
|
1 |
|
$params[1], |
161
|
1 |
|
$params[2] |
162
|
1 |
|
); |
163
|
|
|
} |
164
|
1 |
|
return $this->addInternal($script, $params[0], $params[1]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Fix and escape script attributes |
169
|
|
|
* |
170
|
|
|
* @param mixed $src script source |
171
|
|
|
* @param array $attr additional attributes |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
* |
175
|
|
|
* @access protected |
176
|
|
|
*/ |
177
|
3 |
|
protected function attr($src = null, array $attr = array()) |
178
|
|
|
{ |
179
|
3 |
|
if (null !== $src) { |
180
|
2 |
|
$attr['src'] = $src; |
181
|
2 |
|
} |
182
|
3 |
|
$attr['type'] = 'text/javascript'; |
183
|
3 |
|
return $this->escaper->attr($attr); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|