@@ -22,7 +22,22 @@ class BlockMarkupUrlProcessor extends BlockMarkupProcessor {
22
22
private $ base_url_object ;
23
23
private $ url_in_text_processor ;
24
24
private $ url_in_text_node_updated ;
25
- private $ inspected_url_attribute_idx = - 1 ;
25
+
26
+ /**
27
+ * The list of names of URL-related HTML attributes that may be available on
28
+ * the current token. They will be inspected by next_url_attribute().
29
+ *
30
+ * Possible values:
31
+ *
32
+ * - null: We haven't inspected any attribute yet.
33
+ * - array: The first element is the currently inspected attribute
34
+ * and the rest of the list are elements yet to be inspected on
35
+ * the upcoming next_url_attribute() call.
36
+ * - empty array: We've already inspected all the URL-related attributes.
37
+ *
38
+ * @var array<string>|null
39
+ */
40
+ private $ inspecting_html_attributes ;
26
41
27
42
public function __construct ( $ html , ?string $ base_url_string = null ) {
28
43
parent ::__construct ( $ html );
@@ -50,10 +65,10 @@ public function get_parsed_url() {
50
65
public function next_token (): bool {
51
66
$ this ->get_updated_html ();
52
67
53
- $ this ->raw_url = null ;
54
- $ this ->parsed_url = null ;
55
- $ this ->inspected_url_attribute_idx = - 1 ;
56
- $ this ->url_in_text_processor = null ;
68
+ $ this ->raw_url = null ;
69
+ $ this ->parsed_url = null ;
70
+ $ this ->inspecting_html_attributes = null ;
71
+ $ this ->url_in_text_processor = null ;
57
72
// Do not reset url_in_text_node_updated – it's reset in get_updated_html() which.
58
73
// is called in parent::next_token().
59
74
@@ -116,39 +131,51 @@ private function next_url_in_text_node() {
116
131
117
132
private function next_url_attribute () {
118
133
$ tag = $ this ->get_tag ();
119
- if (
120
- ! array_key_exists ( $ tag , self ::URL_ATTRIBUTES ) &&
121
- 'INPUT ' !== $ tag // type=image => src,.
122
- ) {
134
+
135
+ if ( ! array_key_exists ( $ tag , self ::URL_ATTRIBUTES ) ) {
123
136
return false ;
124
137
}
125
138
126
- while ( ++$ this ->inspected_url_attribute_idx < count ( self ::URL_ATTRIBUTES [ $ tag ] ) ) {
127
- $ attr = self ::URL_ATTRIBUTES [ $ tag ][ $ this ->inspected_url_attribute_idx ];
128
- if ( false === $ attr ) {
129
- return false ;
130
- }
139
+ if ( null === $ this ->inspecting_html_attributes ) {
140
+ /**
141
+ * Initialize the list on the first call to next_url_attribute()
142
+ * for the current token. The last element is the attribute we'll
143
+ * inspect in the while() loop below.
144
+ */
145
+ $ this ->inspecting_html_attributes = self ::URL_ATTRIBUTES [ $ tag ];
146
+ } else {
147
+ /**
148
+ * Forget the attribute we've inspected on the previous call to
149
+ * next_url_attribute().
150
+ */
151
+ array_pop ( $ this ->inspecting_html_attributes );
152
+ }
131
153
154
+ while ( count ( $ this ->inspecting_html_attributes ) > 0 ) {
155
+ $ attr = $ this ->inspecting_html_attributes [ count ( $ this ->inspecting_html_attributes ) - 1 ];
132
156
$ url_maybe = $ this ->get_attribute ( $ attr );
157
+ if ( ! is_string ( $ url_maybe ) ) {
158
+ array_pop ( $ this ->inspecting_html_attributes );
159
+ continue ;
160
+ }
161
+
133
162
/*
134
163
* Use base URL to resolve known URI attributes as we are certain we're
135
164
* dealing with URI values.
136
165
* With a base URL, the string "plugins.php" in <a href="plugins.php"> will
137
166
* be correctly recognized as a URL.
138
167
* Without a base URL, this Processor would incorrectly skip it.
139
168
*/
169
+ $ parsed_url = WPURL ::parse ( $ url_maybe , $ this ->base_url_string );
140
170
141
- if ( is_string ( $ url_maybe ) ) {
142
- $ parsed_url = WPURL ::parse ( $ url_maybe , $ this ->base_url_string );
143
-
144
- if ( false === $ parsed_url ) {
145
- return false ;
146
- }
147
- $ this ->raw_url = $ url_maybe ;
148
- $ this ->parsed_url = $ parsed_url ;
149
-
150
- return true ;
171
+ if ( false === $ parsed_url ) {
172
+ array_pop ( $ this ->inspecting_html_attributes );
173
+ continue ;
151
174
}
175
+ $ this ->raw_url = $ url_maybe ;
176
+ $ this ->parsed_url = $ parsed_url ;
177
+
178
+ return true ;
152
179
}
153
180
154
181
return false ;
@@ -324,19 +351,15 @@ public function get_inspected_attribute_name() {
324
351
return false ;
325
352
}
326
353
327
- $ tag = $ this ->get_tag ();
328
- if ( ! array_key_exists ( $ tag , self ::URL_ATTRIBUTES ) ) {
354
+ if ( null === $ this ->inspecting_html_attributes ) {
329
355
return false ;
330
356
}
331
357
332
- if (
333
- $ this ->inspected_url_attribute_idx < 0 ||
334
- $ this ->inspected_url_attribute_idx >= count ( self ::URL_ATTRIBUTES [ $ tag ] )
335
- ) {
358
+ if ( empty ( $ this ->inspecting_html_attributes ) ) {
336
359
return false ;
337
360
}
338
361
339
- return self :: URL_ATTRIBUTES [ $ tag ][ $ this ->inspected_url_attribute_idx ];
362
+ return $ this -> inspecting_html_attributes [ count ( $ this ->inspecting_html_attributes ) - 1 ];
340
363
}
341
364
342
365
0 commit comments