[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Interactivity API: WP_Interactivity_API_Directives_Processor class. 4 * 5 * @package WordPress 6 * @subpackage Interactivity API 7 * @since 6.5.0 8 */ 9 10 /** 11 * Class used to iterate over the tags of an HTML string and help process the 12 * directive attributes. 13 * 14 * @since 6.5.0 15 * 16 * @access private 17 */ 18 final class WP_Interactivity_API_Directives_Processor extends WP_HTML_Tag_Processor { 19 /** 20 * List of tags whose closer tag is not visited by the WP_HTML_Tag_Processor. 21 * 22 * @since 6.5.0 23 * @var string[] 24 */ 25 const TAGS_THAT_DONT_VISIT_CLOSER_TAG = array( 26 'SCRIPT', 27 'IFRAME', 28 'NOEMBED', 29 'NOFRAMES', 30 'STYLE', 31 'TEXTAREA', 32 'TITLE', 33 'XMP', 34 ); 35 36 /** 37 * Returns the content between two balanced template tags. 38 * 39 * It positions the cursor in the closer tag of the balanced template tag, 40 * if it exists. 41 * 42 * @since 6.5.0 43 * 44 * @access private 45 * 46 * @return string|null The content between the current opener template tag and its matching closer tag or null if it 47 * doesn't find the matching closing tag or the current tag is not a template opener tag. 48 */ 49 public function get_content_between_balanced_template_tags() { 50 if ( 'TEMPLATE' !== $this->get_tag() ) { 51 return null; 52 } 53 54 $positions = $this->get_after_opener_tag_and_before_closer_tag_positions(); 55 if ( ! $positions ) { 56 return null; 57 } 58 list( $after_opener_tag, $before_closer_tag ) = $positions; 59 60 return substr( $this->html, $after_opener_tag, $before_closer_tag - $after_opener_tag ); 61 } 62 63 /** 64 * Sets the content between two balanced tags. 65 * 66 * @since 6.5.0 67 * 68 * @access private 69 * 70 * @param string $new_content The string to replace the content between the matching tags. 71 * @return bool Whether the content was successfully replaced. 72 */ 73 public function set_content_between_balanced_tags( string $new_content ): bool { 74 $positions = $this->get_after_opener_tag_and_before_closer_tag_positions( true ); 75 if ( ! $positions ) { 76 return false; 77 } 78 list( $after_opener_tag, $before_closer_tag ) = $positions; 79 80 $this->lexical_updates[] = new WP_HTML_Text_Replacement( 81 $after_opener_tag, 82 $before_closer_tag - $after_opener_tag, 83 esc_html( $new_content ) 84 ); 85 86 return true; 87 } 88 89 /** 90 * Appends content after the closing tag of a template tag. 91 * 92 * It positions the cursor in the closer tag of the balanced template tag, 93 * if it exists. 94 * 95 * @access private 96 * 97 * @param string $new_content The string to append after the closing template tag. 98 * @return bool Whether the content was successfully appended. 99 */ 100 public function append_content_after_template_tag_closer( string $new_content ): bool { 101 if ( empty( $new_content ) || 'TEMPLATE' !== $this->get_tag() || ! $this->is_tag_closer() ) { 102 return false; 103 } 104 105 // Flushes any changes. 106 $this->get_updated_html(); 107 108 $bookmark = 'append_content_after_template_tag_closer'; 109 $this->set_bookmark( $bookmark ); 110 $after_closing_tag = $this->bookmarks[ $bookmark ]->start + $this->bookmarks[ $bookmark ]->length + 1; 111 $this->release_bookmark( $bookmark ); 112 113 // Appends the new content. 114 $this->lexical_updates[] = new WP_HTML_Text_Replacement( $after_closing_tag, 0, $new_content ); 115 116 return true; 117 } 118 119 /** 120 * Gets the positions right after the opener tag and right before the closer 121 * tag in a balanced tag. 122 * 123 * By default, it positions the cursor in the closer tag of the balanced tag. 124 * If $rewind is true, it seeks back to the opener tag. 125 * 126 * @since 6.5.0 127 * 128 * @access private 129 * 130 * @param bool $rewind Optional. Whether to seek back to the opener tag after finding the positions. Defaults to false. 131 * @return array|null Start and end byte position, or null when no balanced tag bookmarks. 132 */ 133 private function get_after_opener_tag_and_before_closer_tag_positions( bool $rewind = false ) { 134 // Flushes any changes. 135 $this->get_updated_html(); 136 137 $bookmarks = $this->get_balanced_tag_bookmarks(); 138 if ( ! $bookmarks ) { 139 return null; 140 } 141 list( $opener_tag, $closer_tag ) = $bookmarks; 142 143 $after_opener_tag = $this->bookmarks[ $opener_tag ]->start + $this->bookmarks[ $opener_tag ]->length + 1; 144 $before_closer_tag = $this->bookmarks[ $closer_tag ]->start; 145 146 if ( $rewind ) { 147 $this->seek( $opener_tag ); 148 } 149 150 $this->release_bookmark( $opener_tag ); 151 $this->release_bookmark( $closer_tag ); 152 153 return array( $after_opener_tag, $before_closer_tag ); 154 } 155 156 /** 157 * Returns a pair of bookmarks for the current opener tag and the matching 158 * closer tag. 159 * 160 * It positions the cursor in the closer tag of the balanced tag, if it 161 * exists. 162 * 163 * @since 6.5.0 164 * 165 * @return array|null A pair of bookmarks, or null if there's no matching closing tag. 166 */ 167 private function get_balanced_tag_bookmarks() { 168 static $i = 0; 169 $opener_tag = 'opener_tag_of_balanced_tag_' . ++$i; 170 171 $this->set_bookmark( $opener_tag ); 172 if ( ! $this->next_balanced_tag_closer_tag() ) { 173 $this->release_bookmark( $opener_tag ); 174 return null; 175 } 176 177 $closer_tag = 'closer_tag_of_balanced_tag_' . ++$i; 178 $this->set_bookmark( $closer_tag ); 179 180 return array( $opener_tag, $closer_tag ); 181 } 182 183 /** 184 * Skips processing the content between tags. 185 * 186 * It positions the cursor in the closer tag of the foreign element, if it 187 * exists. 188 * 189 * This function is intended to skip processing SVG and MathML inner content 190 * instead of bailing out the whole processing. 191 * 192 * @since 6.5.0 193 * 194 * @access private 195 * 196 * @return bool Whether the foreign content was successfully skipped. 197 */ 198 public function skip_to_tag_closer(): bool { 199 $depth = 1; 200 $tag_name = $this->get_tag(); 201 while ( $depth > 0 && $this->next_tag( 202 array( 203 'tag_name' => $tag_name, 204 'tag_closers' => 'visit', 205 ) 206 ) ) { 207 if ( $this->has_self_closing_flag() ) { 208 continue; 209 } 210 $depth += $this->is_tag_closer() ? -1 : 1; 211 } 212 213 return 0 === $depth; 214 } 215 216 /** 217 * Finds the matching closing tag for an opening tag. 218 * 219 * When called while the processor is on an open tag, it traverses the HTML 220 * until it finds the matching closer tag, respecting any in-between content, 221 * including nested tags of the same name. Returns false when called on a 222 * closer tag, a tag that doesn't have a closer tag (void), a tag that 223 * doesn't visit the closer tag, or if no matching closing tag was found. 224 * 225 * @since 6.5.0 226 * 227 * @access private 228 * 229 * @return bool Whether a matching closing tag was found. 230 */ 231 public function next_balanced_tag_closer_tag(): bool { 232 $depth = 0; 233 $tag_name = $this->get_tag(); 234 235 if ( ! $this->has_and_visits_its_closer_tag() ) { 236 return false; 237 } 238 239 while ( $this->next_tag( 240 array( 241 'tag_name' => $tag_name, 242 'tag_closers' => 'visit', 243 ) 244 ) ) { 245 if ( ! $this->is_tag_closer() ) { 246 ++$depth; 247 continue; 248 } 249 250 if ( 0 === $depth ) { 251 return true; 252 } 253 254 --$depth; 255 } 256 257 return false; 258 } 259 260 /** 261 * Checks whether the current tag has and will visit its matching closer tag. 262 * 263 * @since 6.5.0 264 * 265 * @access private 266 * 267 * @return bool Whether the current tag has a closer tag. 268 */ 269 public function has_and_visits_its_closer_tag(): bool { 270 $tag_name = $this->get_tag(); 271 272 return null !== $tag_name && ( 273 ! WP_HTML_Processor::is_void( $tag_name ) && 274 ! in_array( $tag_name, self::TAGS_THAT_DONT_VISIT_CLOSER_TAG, true ) 275 ); 276 } 277 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Apr 27 08:20:02 2024 | Cross-referenced by PHPXref |