Changeset 44390 for branches/5.0/src/wp-includes/class-wp-block-parser.php
- Timestamp:
- 01/04/2019 08:18:09 PM (4 years ago)
- Location:
- branches/5.0
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/5.0
-
branches/5.0/src/wp-includes/class-wp-block-parser.php
r43955 r44390 1 1 <?php 2 /** 3 * Block Serialization Parser 4 * 5 * @package WordPress 6 */ 2 7 3 8 /** … … 63 68 public $innerContent; 64 69 70 /** 71 * Constructor. 72 * 73 * Will populate object properties from the provided arguments. 74 * 75 * @since 3.8.0 76 * 77 * @param string $name Name of block. 78 * @param array $attrs Optional set of attributes from block comment delimiters. 79 * @param array $innerBlocks List of inner blocks (of this same class). 80 * @param string $innerHTML Resultant HTML from inside block comment delimiters after removing inner blocks. 81 * @param array $innerContent List of string fragments and null markers where inner blocks were found. 82 */ 65 83 function __construct( $name, $attrs, $innerBlocks, $innerHTML, $innerContent ) { 66 $this->blockName = $name;67 $this->attrs = $attrs;68 $this->innerBlocks = $innerBlocks;69 $this->innerHTML = $innerHTML;84 $this->blockName = $name; 85 $this->attrs = $attrs; 86 $this->innerBlocks = $innerBlocks; 87 $this->innerHTML = $innerHTML; 70 88 $this->innerContent = $innerContent; 71 89 } … … 122 140 public $leading_html_start; 123 141 142 /** 143 * Constructor 144 * 145 * Will populate object properties from the provided arguments. 146 * 147 * @since 3.8.0 148 * 149 * @param WP_Block_Parser_Block $block Full or partial block. 150 * @param int $token_start Byte offset into document for start of parse token. 151 * @param int $token_length Byte length of entire parse token string. 152 * @param int $prev_offset Byte offset into document for after parse token ends. 153 * @param int $leading_html_start Byte offset into document where leading HTML before token starts. 154 */ 124 155 function __construct( $block, $token_start, $token_length, $prev_offset = null, $leading_html_start = null ) { 125 156 $this->block = $block; … … 191 222 * @since 3.8.0 192 223 * 193 * @param string $document 224 * @param string $document Input document being parsed. 194 225 * @return WP_Block_Parser_Block[] 195 226 */ … … 202 233 203 234 do { 204 // twiddle our thumbs 235 // twiddle our thumbs. 205 236 } while ( $this->proceed() ); 206 237 … … 227 258 $stack_depth = count( $this->stack ); 228 259 229 // we may have some HTML soup before the next block 260 // we may have some HTML soup before the next block. 230 261 $leading_html_start = $start_offset > $this->offset ? $this->offset : null; 231 262 232 263 switch ( $token_type ) { 233 264 case 'no-more-tokens': 234 // if not in a block then flush output 265 // if not in a block then flush output. 235 266 if ( 0 === $stack_depth ) { 236 267 $this->add_freeform(); … … 247 278 */ 248 279 249 // for the easy case we'll assume an implicit closer 280 // for the easy case we'll assume an implicit closer. 250 281 if ( 1 === $stack_depth ) { 251 282 $this->add_block_from_stack(); … … 270 301 if ( 0 === $stack_depth ) { 271 302 if ( isset( $leading_html_start ) ) { 272 $this->output[] = (array) self::freeform( substr( 273 $this->document, 274 $leading_html_start, 275 $start_offset - $leading_html_start 276 ) ); 303 $this->output[] = (array) self::freeform( 304 substr( 305 $this->document, 306 $leading_html_start, 307 $start_offset - $leading_html_start 308 ) 309 ); 277 310 } 278 311 279 312 $this->output[] = (array) new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ); 280 $this->offset = $start_offset + $token_length;313 $this->offset = $start_offset + $token_length; 281 314 return true; 282 315 } 283 316 284 // otherwise we found an inner block 317 // otherwise we found an inner block. 285 318 $this->add_inner_block( 286 319 new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ), … … 292 325 293 326 case 'block-opener': 294 // track all newly-opened blocks on the stack 295 array_push( $this->stack, new WP_Block_Parser_Frame( 296 new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ), 297 $start_offset, 298 $token_length, 299 $start_offset + $token_length, 300 $leading_html_start 301 ) ); 327 // track all newly-opened blocks on the stack. 328 array_push( 329 $this->stack, 330 new WP_Block_Parser_Frame( 331 new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ), 332 $start_offset, 333 $token_length, 334 $start_offset + $token_length, 335 $leading_html_start 336 ) 337 ); 302 338 $this->offset = $start_offset + $token_length; 303 339 return true; … … 319 355 } 320 356 321 // if we're not nesting then this is easy - close the block 357 // if we're not nesting then this is easy - close the block. 322 358 if ( 1 === $stack_depth ) { 323 359 $this->add_block_from_stack( $start_offset ); … … 330 366 * block and add it as a new innerBlock to the parent 331 367 */ 332 $stack_top = array_pop( $this->stack );333 $html = substr( $this->document, $stack_top->prev_offset, $start_offset - $stack_top->prev_offset );334 $stack_top->block->innerHTML .= $html;368 $stack_top = array_pop( $this->stack ); 369 $html = substr( $this->document, $stack_top->prev_offset, $start_offset - $stack_top->prev_offset ); 370 $stack_top->block->innerHTML .= $html; 335 371 $stack_top->block->innerContent[] = $html; 336 $stack_top->prev_offset = $start_offset + $token_length;372 $stack_top->prev_offset = $start_offset + $token_length; 337 373 338 374 $this->add_inner_block( … … 346 382 347 383 default: 348 // This is an error 384 // This is an error. 349 385 $this->add_freeform(); 350 386 return false; … … 382 418 ); 383 419 384 // if we get here we probably have catastrophic backtracking or out-of-memory in the PCRE 420 // if we get here we probably have catastrophic backtracking or out-of-memory in the PCRE. 385 421 if ( false === $has_match ) { 386 422 return array( 'no-more-tokens', null, null, null, null ); 387 423 } 388 424 389 // we have no more tokens 425 // we have no more tokens. 390 426 if ( 0 === $has_match ) { 391 427 return array( 'no-more-tokens', null, null, null, null ); 392 428 } 393 429 394 list( $match, $started_at ) = $matches[ 0];430 list( $match, $started_at ) = $matches[0]; 395 431 396 432 $length = strlen( $match ); 397 $is_closer = isset( $matches[ 'closer' ] ) && -1 !== $matches[ 'closer' ][ 1];398 $is_void = isset( $matches[ 'void' ] ) && -1 !== $matches[ 'void' ][ 1];399 $namespace = $matches[ 'namespace'];400 $namespace = ( isset( $namespace ) && -1 !== $namespace[ 1 ] ) ? $namespace[ 0] : 'core/';401 $name = $namespace . $matches[ 'name' ][ 0];402 $has_attrs = isset( $matches[ 'attrs' ] ) && -1 !== $matches[ 'attrs' ][ 1];433 $is_closer = isset( $matches['closer'] ) && -1 !== $matches['closer'][1]; 434 $is_void = isset( $matches['void'] ) && -1 !== $matches['void'][1]; 435 $namespace = $matches['namespace']; 436 $namespace = ( isset( $namespace ) && -1 !== $namespace[1] ) ? $namespace[0] : 'core/'; 437 $name = $namespace . $matches['name'][0]; 438 $has_attrs = isset( $matches['attrs'] ) && -1 !== $matches['attrs'][1]; 403 439 404 440 /* … … 407 443 */ 408 444 $attrs = $has_attrs 409 ? json_decode( $matches[ 'attrs' ][ 0], /* as-associative */ true )445 ? json_decode( $matches['attrs'][0], /* as-associative */ true ) 410 446 : $this->empty_attrs; 411 447 … … 415 451 */ 416 452 if ( $is_closer && ( $is_void || $has_attrs ) ) { 417 // we can ignore them since they don't hurt anything 453 // we can ignore them since they don't hurt anything. 418 454 } 419 455 … … 435 471 * @since 3.9.0 436 472 * 437 * @param string $innerHTML HTML content of block 438 * @return WP_Block_Parser_Block freeform block object 473 * @param string $innerHTML HTML content of block. 474 * @return WP_Block_Parser_Block freeform block object. 439 475 */ 440 476 function freeform( $innerHTML ) { … … 444 480 /** 445 481 * Pushes a length of text from the input document 446 * to the output list as a freeform block 482 * to the output list as a freeform block. 447 483 * 448 484 * @internal 449 485 * @since 3.8.0 450 * @param null $length how many bytes of document text to output 486 * @param null $length how many bytes of document text to output. 451 487 */ 452 488 function add_freeform( $length = null ) { … … 462 498 /** 463 499 * Given a block structure from memory pushes 464 * a new block to the output list 500 * a new block to the output list. 465 501 * 466 502 * @internal 467 503 * @since 3.8.0 468 * @param WP_Block_Parser_Block $block the block to add to the output469 * @param int $token_start byte offset into the document where the first token for the block starts470 * @param int $token_length byte length of entire block from start of opening token to end of closing token471 * @param int|null $last_offset last byte offset into document if continuing form earlier output504 * @param WP_Block_Parser_Block $block The block to add to the output. 505 * @param int $token_start Byte offset into the document where the first token for the block starts. 506 * @param int $token_length Byte length of entire block from start of opening token to end of closing token. 507 * @param int|null $last_offset Last byte offset into document if continuing form earlier output. 472 508 */ 473 509 function add_inner_block( WP_Block_Parser_Block $block, $token_start, $token_length, $last_offset = null ) { 474 $parent = $this->stack[ count( $this->stack ) - 1 ];510 $parent = $this->stack[ count( $this->stack ) - 1 ]; 475 511 $parent->block->innerBlocks[] = (array) $block; 476 $html = substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset );512 $html = substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset ); 477 513 478 514 if ( ! empty( $html ) ) { 479 $parent->block->innerHTML .= $html;515 $parent->block->innerHTML .= $html; 480 516 $parent->block->innerContent[] = $html; 481 517 } 482 518 483 519 $parent->block->innerContent[] = null; 484 $parent->prev_offset = $last_offset ? $last_offset : $token_start + $token_length;485 } 486 487 /** 488 * Pushes the top block from the parsing stack to the output list 520 $parent->prev_offset = $last_offset ? $last_offset : $token_start + $token_length; 521 } 522 523 /** 524 * Pushes the top block from the parsing stack to the output list. 489 525 * 490 526 * @internal 491 527 * @since 3.8.0 492 * @param int|null $end_offset byte offset into document for where we should stop sending text output as HTML 528 * @param int|null $end_offset byte offset into document for where we should stop sending text output as HTML. 493 529 */ 494 530 function add_block_from_stack( $end_offset = null ) { … … 501 537 502 538 if ( ! empty( $html ) ) { 503 $stack_top->block->innerHTML .= $html;539 $stack_top->block->innerHTML .= $html; 504 540 $stack_top->block->innerContent[] = $html; 505 541 } 506 542 507 543 if ( isset( $stack_top->leading_html_start ) ) { 508 $this->output[] = (array) self::freeform( substr( 509 $this->document, 510 $stack_top->leading_html_start, 511 $stack_top->token_start - $stack_top->leading_html_start 512 ) ); 544 $this->output[] = (array) self::freeform( 545 substr( 546 $this->document, 547 $stack_top->leading_html_start, 548 $stack_top->token_start - $stack_top->leading_html_start 549 ) 550 ); 513 551 } 514 552
Note: See TracChangeset
for help on using the changeset viewer.