|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* DomTrait.php |
|
5
|
|
|
* |
|
6
|
|
|
* Provides DOM (HTML) related commands for the Response |
|
7
|
|
|
* |
|
8
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
9
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
10
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
11
|
|
|
*/ |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
namespace Jaxon\Response\Traits; |
|
14
|
|
|
|
|
15
|
|
|
use Jaxon\Response\ResponseInterface; |
|
16
|
|
|
use JsonSerializable; |
|
17
|
|
|
|
|
18
|
|
|
trait HtmlDomTrait |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $sName The command name |
|
|
|
|
|
|
24
|
|
|
* @param array|JsonSerializable $aOptions The command options |
|
|
|
|
|
|
25
|
|
|
* |
|
26
|
|
|
* @return ResponseInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract public function addCommand(string $sName, array|JsonSerializable $aOptions): ResponseInterface; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Add a command to assign the specified value to the given element's attribute |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $sTarget The id of the html element on the browser |
|
34
|
|
|
* @param string $sAttribute The attribute to be assigned |
|
|
|
|
|
|
35
|
|
|
* @param string $sValue The value to be assigned to the attribute |
|
|
|
|
|
|
36
|
|
|
* |
|
37
|
|
|
* @return ResponseInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
public function assign(string $sTarget, string $sAttribute, string $sValue): ResponseInterface |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->addCommand('as', [ |
|
42
|
|
|
'id' => $this->str($sTarget), |
|
|
|
|
|
|
43
|
|
|
'attr' => $this->str($sAttribute), |
|
44
|
|
|
'value' => $this->str($sValue), |
|
45
|
|
|
]); |
|
46
|
|
|
} |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add a command to assign the specified HTML content to the given element |
|
50
|
|
|
* |
|
51
|
|
|
* This is a shortcut for assign() on the innerHTML attribute. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $sTarget The id of the html element on the browser |
|
|
|
|
|
|
54
|
|
|
* @param string $sValue The value to be assigned to the attribute |
|
|
|
|
|
|
55
|
|
|
* |
|
56
|
|
|
* @return ResponseInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
public function html(string $sTarget, string $sValue): ResponseInterface |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->assign($sTarget, 'innerHTML', $sValue); |
|
61
|
|
|
} |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Add a command to append the specified data to the given element's attribute |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $sTarget The id of the element to be updated |
|
67
|
|
|
* @param string $sAttribute The name of the attribute to be appended to |
|
|
|
|
|
|
68
|
|
|
* @param string $sValue The data to be appended to the attribute |
|
|
|
|
|
|
69
|
|
|
* |
|
70
|
|
|
* @return ResponseInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
public function append(string $sTarget, string $sAttribute, string $sValue): ResponseInterface |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->addCommand('ap', [ |
|
75
|
|
|
'id' => $this->str($sTarget), |
|
76
|
|
|
'attr' => $this->str($sAttribute), |
|
77
|
|
|
'value' => $this->str($sValue), |
|
78
|
|
|
]); |
|
79
|
|
|
} |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Add a command to prepend the specified data to the given element's attribute |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $sTarget The id of the element to be updated |
|
85
|
|
|
* @param string $sAttribute The name of the attribute to be prepended to |
|
|
|
|
|
|
86
|
|
|
* @param string $sValue The value to be prepended to the attribute |
|
|
|
|
|
|
87
|
|
|
* |
|
88
|
|
|
* @return ResponseInterface |
|
89
|
|
|
*/ |
|
90
|
|
|
public function prepend(string $sTarget, string $sAttribute, string $sValue): ResponseInterface |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->addCommand('pp', [ |
|
93
|
|
|
'id' => $this->str($sTarget), |
|
94
|
|
|
'attr' => $this->str($sAttribute), |
|
95
|
|
|
'value' => $this->str($sValue), |
|
96
|
|
|
]); |
|
97
|
|
|
} |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Add a command to replace a specified value with another value within the given element's attribute |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $sTarget The id of the element to update |
|
103
|
|
|
* @param string $sAttribute The attribute to be updated |
|
|
|
|
|
|
104
|
|
|
* @param string $sSearch The needle to search for |
|
105
|
|
|
* @param string $sReplace The data to use in place of the needle |
|
|
|
|
|
|
106
|
|
|
* |
|
107
|
|
|
* @return ResponseInterface |
|
108
|
|
|
*/ |
|
109
|
|
|
public function replace(string $sTarget, string $sAttribute, |
|
110
|
|
|
string $sSearch, string $sReplace): ResponseInterface |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->addCommand('rp', [ |
|
113
|
|
|
'id' => $this->str($sTarget), |
|
114
|
|
|
'attr' => $this->str($sAttribute), |
|
115
|
|
|
'search' => $this->str($sSearch), |
|
116
|
|
|
'replace' => $this->str($sReplace), |
|
117
|
|
|
]); |
|
118
|
|
|
} |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Add a command to clear the specified attribute of the given element |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $sTarget The id of the element to be updated. |
|
124
|
|
|
* @param string $sAttribute The attribute to be cleared |
|
|
|
|
|
|
125
|
|
|
* |
|
126
|
|
|
* @return ResponseInterface |
|
127
|
|
|
*/ |
|
128
|
|
|
public function clear(string $sTarget, string $sAttribute = 'innerHTML'): ResponseInterface |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->assign($sTarget, $sAttribute, ''); |
|
131
|
|
|
} |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Add a command to remove an element from the document |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $sTarget The id of the element to be removed |
|
|
|
|
|
|
137
|
|
|
* |
|
138
|
|
|
* @return ResponseInterface |
|
139
|
|
|
*/ |
|
140
|
|
|
public function remove(string $sTarget): ResponseInterface |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->addCommand('rm', ['id' => $this->str($sTarget)]); |
|
143
|
|
|
} |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Add a command to create a new element on the browser |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $sParent The id of the parent element |
|
|
|
|
|
|
149
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
150
|
|
|
* @param string $sId The id to assign to the new element |
|
|
|
|
|
|
151
|
|
|
* |
|
152
|
|
|
* @return ResponseInterface |
|
153
|
|
|
*/ |
|
154
|
|
|
public function create(string $sParent, string $sTag, string $sId): ResponseInterface |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->addCommand('ce', [ |
|
157
|
|
|
'id' => $this->str($sParent), |
|
158
|
|
|
'tag' => [ |
|
159
|
|
|
'name' => $this->str($sTag), |
|
160
|
|
|
'id' => $this->str($sId), |
|
161
|
|
|
], |
|
162
|
|
|
]); |
|
163
|
|
|
} |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Add a command to insert a new element just prior to the specified element |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $sBefore The id of the element used as a reference point for the insertion |
|
|
|
|
|
|
169
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
170
|
|
|
* @param string $sId The id to assign to the new element |
|
|
|
|
|
|
171
|
|
|
* |
|
172
|
|
|
* @return ResponseInterface |
|
173
|
|
|
*/ |
|
174
|
|
|
public function insertBefore(string $sBefore, string $sTag, string $sId): ResponseInterface |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->addCommand('ie', [ |
|
177
|
|
|
'id' => $this->str($sBefore), |
|
178
|
|
|
'tag' => [ |
|
179
|
|
|
'name' => $this->str($sTag), |
|
180
|
|
|
'id' => $this->str($sId), |
|
181
|
|
|
], |
|
182
|
|
|
]); |
|
183
|
|
|
} |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Add a command to insert a new element just prior to the specified element |
|
187
|
|
|
* This is an alias for insertBefore. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $sBefore The id of the element used as a reference point for the insertion |
|
|
|
|
|
|
190
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
191
|
|
|
* @param string $sId The id to assign to the new element |
|
|
|
|
|
|
192
|
|
|
* |
|
193
|
|
|
* @return ResponseInterface |
|
194
|
|
|
*/ |
|
195
|
|
|
public function insert(string $sBefore, string $sTag, string $sId): ResponseInterface |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->insertBefore($sBefore, $sTag, $sId); |
|
198
|
|
|
} |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Add a command to insert a new element after the specified |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $sAfter The id of the element used as a reference point for the insertion |
|
|
|
|
|
|
204
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
|
|
|
|
|
205
|
|
|
* @param string $sId The id to assign to the new element |
|
206
|
|
|
* |
|
207
|
|
|
* @return ResponseInterface |
|
208
|
|
|
*/ |
|
209
|
|
|
public function insertAfter(string $sAfter, string $sTag, string $sId): ResponseInterface |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->addCommand('ia', [ |
|
212
|
|
|
'id' => $this->str($sAfter), |
|
213
|
|
|
'tag' => [ |
|
214
|
|
|
'name' => $this->str($sTag), |
|
215
|
|
|
'id' => $this->str($sId), |
|
216
|
|
|
], |
|
217
|
|
|
]); |
|
218
|
|
|
} |
|
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|