Changeset 43884 for branches/5.0/src/wp-includes/class-wp-block-parser.php
- Timestamp:
- 11/12/2018 02:26:18 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/5.0/src/wp-includes/class-wp-block-parser.php
r43752 r43884 49 49 public $innerHTML; 50 50 51 function __construct( $name, $attrs, $innerBlocks, $innerHTML ) { 51 /** 52 * List of string fragments and null markers where inner blocks were found 53 * 54 * @example array( 55 * 'innerHTML' => 'BeforeInnerAfter', 56 * 'innerBlocks' => array( block, block ), 57 * 'innerContent' => array( 'Before', null, 'Inner', null, 'After' ), 58 * ) 59 * 60 * @since 4.2.0 61 * @var array 62 */ 63 public $innerContent; 64 65 function __construct( $name, $attrs, $innerBlocks, $innerHTML, $innerContent ) { 52 66 $this->blockName = $name; 53 67 $this->attrs = $attrs; 54 68 $this->innerBlocks = $innerBlocks; 55 69 $this->innerHTML = $innerHTML; 70 $this->innerContent = $innerContent; 56 71 } 57 72 } … … 160 175 161 176 /** 177 * Empty associative array, here due to PHP quirks 178 * 179 * @since 4.4.0 180 * @var array empty associative array 181 */ 182 public $empty_attrs; 183 184 /** 162 185 * Parses a document and returns a list of block structures 163 186 * … … 172 195 */ 173 196 function parse( $document ) { 174 $this->document = $document; 175 $this->offset = 0; 176 $this->output = array(); 177 $this->stack = array(); 197 $this->document = $document; 198 $this->offset = 0; 199 $this->output = array(); 200 $this->stack = array(); 201 $this->empty_attrs = json_decode( '{}', true ); 178 202 179 203 do { … … 253 277 } 254 278 255 $this->output[] = (array) new WP_Block_Parser_Block( $block_name, $attrs, array(), '' );279 $this->output[] = (array) new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ); 256 280 $this->offset = $start_offset + $token_length; 257 281 return true; … … 260 284 // otherwise we found an inner block 261 285 $this->add_inner_block( 262 new WP_Block_Parser_Block( $block_name, $attrs, array(), '' ),286 new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ), 263 287 $start_offset, 264 288 $token_length … … 270 294 // track all newly-opened blocks on the stack 271 295 array_push( $this->stack, new WP_Block_Parser_Frame( 272 new WP_Block_Parser_Block( $block_name, $attrs, array(), '' ),296 new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ), 273 297 $start_offset, 274 298 $token_length, … … 307 331 */ 308 332 $stack_top = array_pop( $this->stack ); 309 310 333 $html = substr( $this->document, $stack_top->prev_offset, $start_offset - $stack_top->prev_offset ); 311 if ( $stack_top->block->innerBlocks ) { 312 $stack_top->block->innerBlocks[] = (array) $this->freeform( $html ); 313 } else { 314 $stack_top->block->innerHTML = $html; 315 } 316 334 $stack_top->block->innerHTML .= $html; 335 $stack_top->block->innerContent[] = $html; 317 336 $stack_top->prev_offset = $start_offset + $token_length; 318 337 … … 355 374 */ 356 375 $has_match = preg_match( 357 '/<!--\s+(?<closer>\/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{(?: (?!}\s+-->).)+?}\s+)?(?<void>\/)?-->/s',376 '/<!--\s+(?<closer>\/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{(?:[^}]+|}+(?=})|(?!}\s+-->).)*?}\s+)?(?<void>\/)?-->/s', 358 377 $this->document, 359 378 $matches, … … 383 402 $attrs = $has_attrs 384 403 ? json_decode( $matches[ 'attrs' ][ 0 ], /* as-associative */ true ) 385 : json_decode( '{}', /* don't ask why, just verify in PHP */ false );404 : $this->empty_attrs; 386 405 387 406 /* … … 413 432 * @return WP_Block_Parser_Block freeform block object 414 433 */ 415 staticfunction freeform( $innerHTML ) {416 return new WP_Block_Parser_Block( null, array(), array(), $innerHTML);434 function freeform( $innerHTML ) { 435 return new WP_Block_Parser_Block( null, $this->empty_attrs, array(), $innerHTML, array( $innerHTML ) ); 417 436 } 418 437 … … 448 467 function add_inner_block( WP_Block_Parser_Block $block, $token_start, $token_length, $last_offset = null ) { 449 468 $parent = $this->stack[ count( $this->stack ) - 1 ]; 450 $parent->block->innerBlocks[] = (array) $this->freeform( substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset ) );451 469 $parent->block->innerBlocks[] = (array) $block; 470 $html = substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset ); 471 472 if ( ! empty( $html ) ) { 473 $parent->block->innerHTML .= $html; 474 $parent->block->innerContent[] = $html; 475 } 476 477 $parent->block->innerContent[] = null; 452 478 $parent->prev_offset = $last_offset ? $last_offset : $token_start + $token_length; 453 479 } … … 468 494 : substr( $this->document, $prev_offset ); 469 495 470 if ( $stack_top->block->innerBlocks ) { 471 $stack_top->block->innerBlocks[] = (array) $this->freeform( $html ); 472 } else { 473 $stack_top->block->innerHTML = $html; 496 if ( ! empty( $html ) ) { 497 $stack_top->block->innerHTML .= $html; 498 $stack_top->block->innerContent[] = $html; 474 499 } 475 500
Note: See TracChangeset
for help on using the changeset viewer.