summaryrefslogtreecommitdiffstats
path: root/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php
blob: 590cf967cf47189c9ec6e4b3d170d0e32526615f (plain)
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
<?php
/**
 * Interactivity API: WP_Interactivity_API_Directives_Processor class.
 *
 * @package WordPress
 * @subpackage Interactivity API
 * @since 6.5.0
 */

/**
 * Class used to iterate over the tags of an HTML string and help process the
 * directive attributes.
 *
 * @since 6.5.0
 *
 * @access private
 */
final class WP_Interactivity_API_Directives_Processor extends WP_HTML_Tag_Processor {
	/**
	 * List of tags whose closer tag is not visited by the WP_HTML_Tag_Processor.
	 *
	 * @since 6.5.0
	 * @var string[]
	 */
	const TAGS_THAT_DONT_VISIT_CLOSER_TAG = array(
		'SCRIPT',
		'IFRAME',
		'NOEMBED',
		'NOFRAMES',
		'STYLE',
		'TEXTAREA',
		'TITLE',
		'XMP',
	);

	/**
	 * Returns the content between two balanced template tags.
	 *
	 * It positions the cursor in the closer tag of the balanced template tag,
	 * if it exists.
	 *
	 * @since 6.5.0
	 *
	 * @access private
	 *
	 * @return string|null The content between the current opener template tag and its matching closer tag or null if it
	 *                     doesn't find the matching closing tag or the current tag is not a template opener tag.
	 */
	public function get_content_between_balanced_template_tags() {
		if ( 'TEMPLATE' !== $this->get_tag() ) {
			return null;
		}

		$positions = $this->get_after_opener_tag_and_before_closer_tag_positions();
		if ( ! $positions ) {
			return null;
		}
		list( $after_opener_tag, $before_closer_tag ) = $positions;

		return substr( $this->html, $after_opener_tag, $before_closer_tag - $after_opener_tag );
	}

	/**
	 * Sets the content between two balanced tags.
	 *
	 * @since 6.5.0
	 *
	 * @access private
	 *
	 * @param string $new_content The string to replace the content between the matching tags.
	 * @return bool Whether the content was successfully replaced.
	 */
	public function set_content_between_balanced_tags( string $new_content ): bool {
		$positions = $this->get_after_opener_tag_and_before_closer_tag_positions( true );
		if ( ! $positions ) {
			return false;
		}
		list( $after_opener_tag, $before_closer_tag ) = $positions;

		$this->lexical_updates[] = new WP_HTML_Text_Replacement(
			$after_opener_tag,
			$before_closer_tag - $after_opener_tag,
			esc_html( $new_content )
		);

		return true;
	}

	/**
	 * Appends content after the closing tag of a template tag.
	 *
	 * It positions the cursor in the closer tag of the balanced template tag,
	 * if it exists.
	 *
	 * @access private
	 *
	 * @param string $new_content The string to append after the closing template tag.
	 * @return bool Whether the content was successfully appended.
	 */
	public function append_content_after_template_tag_closer( string $new_content ): bool {
		if ( empty( $new_content ) || 'TEMPLATE' !== $this->get_tag() || ! $this->is_tag_closer() ) {
			return false;
		}

		// Flushes any changes.
		$this->get_updated_html();

		$bookmark = 'append_content_after_template_tag_closer';
		$this->set_bookmark( $bookmark );
		$after_closing_tag = $this->bookmarks[ $bookmark ]->start + $this->bookmarks[ $bookmark ]->length;
		$this->release_bookmark( $bookmark );

		// Appends the new content.
		$this->lexical_updates[] = new WP_HTML_Text_Replacement( $after_closing_tag, 0, $new_content );

		return true;
	}

	/**
	 * Gets the positions right after the opener tag and right before the closer
	 * tag in a balanced tag.
	 *
	 * By default, it positions the cursor in the closer tag of the balanced tag.
	 * If $rewind is true, it seeks back to the opener tag.
	 *
	 * @since 6.5.0
	 *
	 * @access private
	 *
	 * @param bool $rewind Optional. Whether to seek back to the opener tag after finding the positions. Defaults to false.
	 * @return array|null Start and end byte position, or null when no balanced tag bookmarks.
	 */
	private function get_after_opener_tag_and_before_closer_tag_positions( bool $rewind = false ) {
		// Flushes any changes.
		$this->get_updated_html();

		$bookmarks = $this->get_balanced_tag_bookmarks();
		if ( ! $bookmarks ) {
			return null;
		}
		list( $opener_tag, $closer_tag ) = $bookmarks;

		$after_opener_tag  = $this->bookmarks[ $opener_tag ]->start + $this->bookmarks[ $opener_tag ]->length;
		$before_closer_tag = $this->bookmarks[ $closer_tag ]->start;

		if ( $rewind ) {
			$this->seek( $opener_tag );
		}

		$this->release_bookmark( $opener_tag );
		$this->release_bookmark( $closer_tag );

		return array( $after_opener_tag, $before_closer_tag );
	}

	/**
	 * Returns a pair of bookmarks for the current opener tag and the matching
	 * closer tag.
	 *
	 * It positions the cursor in the closer tag of the balanced tag, if it
	 * exists.
	 *
	 * @since 6.5.0
	 *
	 * @return array|null A pair of bookmarks, or null if there's no matching closing tag.
	 */
	private function get_balanced_tag_bookmarks() {
		static $i   = 0;
		$opener_tag = 'opener_tag_of_balanced_tag_' . ++$i;

		$this->set_bookmark( $opener_tag );
		if ( ! $this->next_balanced_tag_closer_tag() ) {
			$this->release_bookmark( $opener_tag );
			return null;
		}

		$closer_tag = 'closer_tag_of_balanced_tag_' . ++$i;
		$this->set_bookmark( $closer_tag );

		return array( $opener_tag, $closer_tag );
	}

	/**
	 * Skips processing the content between tags.
	 *
	 * It positions the cursor in the closer tag of the foreign element, if it
	 * exists.
	 *
	 * This function is intended to skip processing SVG and MathML inner content
	 * instead of bailing out the whole processing.
	 *
	 * @since 6.5.0
	 *
	 * @access private
	 *
	 * @return bool Whether the foreign content was successfully skipped.
	 */
	public function skip_to_tag_closer(): bool {
		$depth    = 1;
		$tag_name = $this->get_tag();

		while ( $depth > 0 && $this->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
			if ( ! $this->is_tag_closer() && $this->get_attribute_names_with_prefix( 'data-wp-' ) ) {
				/* translators: 1: SVG or MATH HTML tag. */
				$message = sprintf( __( 'Interactivity directives were detected inside an incompatible %1$s tag. These directives will be ignored in the server side render.' ), $tag_name );
				_doing_it_wrong( __METHOD__, $message, '6.6.0' );
			}
			if ( $this->get_tag() === $tag_name ) {
				if ( $this->has_self_closing_flag() ) {
					continue;
				}
				$depth += $this->is_tag_closer() ? -1 : 1;
			}
		}

		return 0 === $depth;
	}

	/**
	 * Finds the matching closing tag for an opening tag.
	 *
	 * When called while the processor is on an open tag, it traverses the HTML
	 * until it finds the matching closer tag, respecting any in-between content,
	 * including nested tags of the same name. Returns false when called on a
	 * closer tag, a tag that doesn't have a closer tag (void), a tag that
	 * doesn't visit the closer tag, or if no matching closing tag was found.
	 *
	 * @since 6.5.0
	 *
	 * @access private
	 *
	 * @return bool Whether a matching closing tag was found.
	 */
	public function next_balanced_tag_closer_tag(): bool {
		$depth    = 0;
		$tag_name = $this->get_tag();

		if ( ! $this->has_and_visits_its_closer_tag() ) {
			return false;
		}

		while ( $this->next_tag(
			array(
				'tag_name'    => $tag_name,
				'tag_closers' => 'visit',
			)
		) ) {
			if ( ! $this->is_tag_closer() ) {
				++$depth;
				continue;
			}

			if ( 0 === $depth ) {
				return true;
			}

			--$depth;
		}

		return false;
	}

	/**
	 * Checks whether the current tag has and will visit its matching closer tag.
	 *
	 * @since 6.5.0
	 *
	 * @access private
	 *
	 * @return bool Whether the current tag has a closer tag.
	 */
	public function has_and_visits_its_closer_tag(): bool {
		$tag_name = $this->get_tag();

		return null !== $tag_name && (
			! WP_HTML_Processor::is_void( $tag_name ) &&
			! in_array( $tag_name, self::TAGS_THAT_DONT_VISIT_CLOSER_TAG, true )
		);
	}
}