-
Notifications
You must be signed in to change notification settings - Fork 49
/
htmltokenset.php
333 lines (293 loc) · 7.89 KB
/
htmltokenset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/**
* @package Habari
*
*/
/**
* HTML Token Set (created by @see HTMLTokenizer)
*/
class HTMLTokenSet implements Iterator, ArrayAccess
{
protected $tokens = array();
protected $sliceOffsetBegin = null;
protected $sliceOffsetLength = null;
public $escape;
public function __construct( $escape = true )
{
$this->escape = $escape;
}
public function __tostring()
{
$out = '';
foreach ( $this->tokens as $token ) {
$out .= self::token_to_string( $token, $this->escape );
}
return $out;
}
/**
* Convert a token to a string
*
* @param array $token The token to convert
* @param bool $escape Whether to escape the string that is returned
* @return string The string representation of the token
*/
public static function token_to_string( array $token, $escape = true )
{
switch ( $token['type'] ) {
case HTMLTokenizer::NODE_TYPE_TEXT:
return $escape ? Utils::htmlspecialchars( html_entity_decode( $token['value'], ENT_QUOTES, 'UTF-8' ) ) : $token['value'];
break;
case HTMLTokenizer::NODE_TYPE_ELEMENT_OPEN:
case HTMLTokenizer::NODE_TYPE_ELEMENT_EMPTY:
$out = '<' . $token['name'];
if ( isset( $token['attrs'] ) && is_array( $token['attrs'] ) ) {
foreach ( $token['attrs'] as $attr => $attrval ) {
$out .= " {$attr}=\"";
if ( $escape ) {
$out .= Utils::htmlspecialchars( html_entity_decode( $attrval, ENT_QUOTES, 'UTF-8' ) );
}
else {
$out .= html_entity_decode( $attrval, ENT_QUOTES, 'UTF-8' );
}
$out .= '"';
}
}
$out .= '>';
break;
case HTMLTokenizer::NODE_TYPE_ELEMENT_CLOSE:
$out = "</{$token['name']}>";
break;
case HTMLTokenizer::NODE_TYPE_PI:
$out = "<?{$token['name']}{$token['value']}>";
break;
case HTMLTokenizer::NODE_TYPE_COMMENT:
$out = "<!--{$token['value']}-->";
break;
case HTMLTokenizer::NODE_TYPE_CDATA_SECTION:
$out = "<![CDATA[{$token['value']}]]>";
break;
case HTMLTokenizer::NODE_TYPE_STATEMENT:
$out = "<!{$token['name']}";
if ( !empty($token['value']) ) {
$out .= " {$token['value']}";
}
$out .= ">";
break;
}
return $out;
}
/**
* Get the offset of the last token in this HTMLTokenSet
*
* @return integer the offset of the last token in this HTMLTokenSet
*/
public function get_end_offset()
{
return $this->sliceOffsetBegin + $this->sliceOffsetLength;
}
/**
* Fetch a section of the tokens, based on passed criteria
*
* @param mixed $names The name of an html tag, or an array of names of html tags to get the tokens of
* @param array $attr Any attributes for the token that you want to filter by
* @return array An array of the HTMLTokenSets that were found
*/
public function slice( $names, array $attr = null )
{
$names = (array)$names;
$ret = array();
foreach ( $names as $name ) {
$offset = 0;
$slices = array();
while ( $slice = $this->find_slice( $offset, $name, $attr ) ) {
$slices[] = $slice;
$offset = $slice->get_end_offset();
}
// Need to reverse this because we need to splice the last chunks first
// if we splice the earlier chunks first, then the offsets get all
// messed up. Trust me.
$ret = array_merge( $ret, array_reverse( $slices ) );
}
return $ret;
}
/**
* This is a support function for HTMLTokenSet::slice(). It finds a specific slice
* @param integer $offset The offset at which to start looking for relevant tokens
* @param string $name The name of the html tag being searched for
* @param array $attr The attributes of the html tag being searched for
*
* @return HTMLTokenSet
*/
protected function find_slice( $offset, $name, array $attr )
{
// find start:
$foundStart = false;
for ( ; $offset < count( $this->tokens ); $offset++ ) {
// short circuit if possible
if ( $this->tokens[$offset]['type'] != HTMLTokenizer::NODE_TYPE_ELEMENT_OPEN ) {
continue;
}
if ( $this->tokens[$offset]['name'] != $name ) {
continue;
}
// check attributes
if ( !count( $attr ) ) {
$foundStart = true;
break; // To: FOUNDSTARTBREAKPOINT
}
foreach ( $attr as $compareName => $compareVal ) {
if ( isset( $this->tokens[$offset]['attrs'][$compareName] ) &&
stripos( $this->tokens[$offset]['attrs'][$compareName], $compareVal ) !== false ) {
$foundStart = true;
break 2; // To: FOUNDSTARTBREAKPOINT
}
}
}
// Fake label: FOUNDSTARTBREAKPOINT
// short circuit if possible:
if ( !$foundStart ) {
return false;
}
$startOffset = $offset;
// find the closing tag
// (keep a stack so we don't mistake a nested node for this closing node)
$stackDepth = 0;
$foundEnd = false;
for ( ; $offset < count( $this->tokens ); $offset++ ) {
switch ( $this->tokens[$offset]['type'] ) {
case HTMLTokenizer::NODE_TYPE_ELEMENT_OPEN:
if ( $this->tokens[$offset]['name'] == $name ) {
++$stackDepth;
}
break;
case HTMLTokenizer::NODE_TYPE_ELEMENT_CLOSE:
if ( $this->tokens[$offset]['name'] == $name ) {
--$stackDepth;
}
break;
// default: skip
}
if ( $stackDepth <= 0 ) {
$foundEnd = true;
break;
}
}
// short circuit if possible:
if ( !$foundEnd ) {
return false;
}
$offsetLength = $offset - $startOffset + 1;
// now, place the found set into a new HTMLTokenSet:
$slice = new HTMLTokenSet($this->escape);
$slice->sliceOffsetBegin = $startOffset;
$slice->sliceOffsetLength = $offsetLength;
$slice->tokens = array_slice( $this->tokens, $slice->sliceOffsetBegin, $slice->sliceOffsetLength );
return $slice;
}
/**
* Remove the first and last tokens from this HTMLTokenSet
*
* @return nothing
*/
public function trim_container()
{
$this->tokens = array_slice( $this->tokens, 1, -1 );
}
/**
* Replace a set of tokens in this HTMLTokenSet
*
* @param HTMLTokenSet $slice The tokens to insert in place of the old tokens
* @return nothing
*/
public function replace_slice( HTMLTokenSet $slice )
{
array_splice(
$this->tokens,
$slice->sliceOffsetBegin,
$slice->sliceOffsetLength,
$slice->tokens
);
}
/**
* Replace a full set of tokens with new tokens. The tokens are replaced
* in place as well as being returned
*
* @param string $source The text to create the new set of tokens from
* @return HTMLTokenSet The new set of tokens created
*/
public function tokenize_replace( $source )
{
$ht = new HTMLTokenizer( $source, $this->escape );
$this->tokens = $ht->parse()->tokens;
return $this->tokens;
}
/**
* Insert an HTMLTokenset before the given position
*
* @param HTMLTokenset $set. The HTMLTokenset to insert
* @param integer $pos. The position to insert the HTMLTokenset before
* @return Nothing
*/
public function insert( HTMLTokenset $set, $pos = 0 )
{
$set->end();
$length = $set->key() - 1;
$pre = array_slice( $this->tokens, 0, $pos );
$post = array_slice( $this->tokens, $pos );
$set->rewind();
while ( $set->valid() ) {
$pre[] = $set->current();
$set->next();
}
$this->tokens = array_merge( $pre, $post );
}
////////////////////////////////////////////////////
// Iterator implemetation:
public function rewind()
{
reset( $this->tokens );
}
public function current()
{
return current( $this->tokens );
}
public function key()
{
return key( $this->tokens );
}
public function next()
{
return next( $this->tokens );
}
public function valid()
{
return $this->current() !== false;
}
public function end()
{
return end( $this->tokens );
}
// ArrayAccess implementation
public function offsetExists( $offset )
{
return isset( $this->tokens[ $offset ] );
}
public function offsetGet( $offset )
{
return $this->tokens[ $offset ];
}
public function offsetSet( $offset, $value )
{
if ( $offset === null ) {
$this->tokens[] = $value;
}
else {
$this->tokens[ $offset ] = $value;
}
}
public function offsetUnset( $offset )
{
unset( $this->tokens[ $offset ] );
}
}
?>