Changeset 44261 for trunk/src/wp-includes/class-wp-block-parser.php
- Timestamp:
- 12/17/2018 04:50:48 AM (6 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/5.0 merged: 43884,43888
- Property svn:mergeinfo changed
-
trunk/src/wp-includes/class-wp-block-parser.php
r44118 r44261 49 49 public $innerHTML; 50 50 51 function __construct( $name, $attrs, $innerBlocks, $innerHTML ) { 52 $this->blockName = $name; 53 $this->attrs = $attrs; 54 $this->innerBlocks = $innerBlocks; 55 $this->innerHTML = $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 ) { 66 $this->blockName = $name; 67 $this->attrs = $attrs; 68 $this->innerBlocks = $innerBlocks; 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 { … … 255 279 } 256 280 257 $this->output[] = (array) new WP_Block_Parser_Block( $block_name, $attrs, array(), '' );281 $this->output[] = (array) new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ); 258 282 $this->offset = $start_offset + $token_length; 259 283 return true; … … 262 286 // otherwise we found an inner block 263 287 $this->add_inner_block( 264 new WP_Block_Parser_Block( $block_name, $attrs, array(), '' ),288 new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ), 265 289 $start_offset, 266 290 $token_length … … 274 298 $this->stack, 275 299 new WP_Block_Parser_Frame( 276 new WP_Block_Parser_Block( $block_name, $attrs, array(), '' ),300 new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ), 277 301 $start_offset, 278 302 $token_length, … … 311 335 * block and add it as a new innerBlock to the parent 312 336 */ 313 $stack_top = array_pop( $this->stack ); 314 315 $html = substr( $this->document, $stack_top->prev_offset, $start_offset - $stack_top->prev_offset ); 316 if ( $stack_top->block->innerBlocks ) { 317 $stack_top->block->innerBlocks[] = (array) $this->freeform( $html ); 318 } else { 319 $stack_top->block->innerHTML = $html; 320 } 321 322 $stack_top->prev_offset = $start_offset + $token_length; 337 $stack_top = array_pop( $this->stack ); 338 $html = substr( $this->document, $stack_top->prev_offset, $start_offset - $stack_top->prev_offset ); 339 $stack_top->block->innerHTML .= $html; 340 $stack_top->block->innerContent[] = $html; 341 $stack_top->prev_offset = $start_offset + $token_length; 323 342 324 343 $this->add_inner_block( … … 360 379 */ 361 380 $has_match = preg_match( 362 '/<!--\s+(?<closer>\/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{(?: (?!}\s+-->).)+?}\s+)?(?<void>\/)?-->/s',381 '/<!--\s+(?<closer>\/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{(?:[^}]+|}+(?=})|(?!}\s+-->).)*?}\s+)?(?<void>\/)?-->/s', 363 382 $this->document, 364 383 $matches, … … 388 407 $attrs = $has_attrs 389 408 ? json_decode( $matches['attrs'][0], /* as-associative */ true ) 390 : json_decode( '{}', /* don't ask why, just verify in PHP */ false );409 : $this->empty_attrs; 391 410 392 411 /* … … 418 437 * @return WP_Block_Parser_Block freeform block object 419 438 */ 420 staticfunction freeform( $innerHTML ) {421 return new WP_Block_Parser_Block( null, array(), array(), $innerHTML);439 function freeform( $innerHTML ) { 440 return new WP_Block_Parser_Block( null, $this->empty_attrs, array(), $innerHTML, array( $innerHTML ) ); 422 441 } 423 442 … … 453 472 function add_inner_block( WP_Block_Parser_Block $block, $token_start, $token_length, $last_offset = null ) { 454 473 $parent = $this->stack[ count( $this->stack ) - 1 ]; 455 $parent->block->innerBlocks[] = (array) $this->freeform( substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset ) );456 474 $parent->block->innerBlocks[] = (array) $block; 457 $parent->prev_offset = $last_offset ? $last_offset : $token_start + $token_length; 475 $html = substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset ); 476 477 if ( ! empty( $html ) ) { 478 $parent->block->innerHTML .= $html; 479 $parent->block->innerContent[] = $html; 480 } 481 482 $parent->block->innerContent[] = null; 483 $parent->prev_offset = $last_offset ? $last_offset : $token_start + $token_length; 458 484 } 459 485 … … 473 499 : substr( $this->document, $prev_offset ); 474 500 475 if ( $stack_top->block->innerBlocks ) { 476 $stack_top->block->innerBlocks[] = (array) $this->freeform( $html ); 477 } else { 478 $stack_top->block->innerHTML = $html; 501 if ( ! empty( $html ) ) { 502 $stack_top->block->innerHTML .= $html; 503 $stack_top->block->innerContent[] = $html; 479 504 } 480 505
Note: See TracChangeset
for help on using the changeset viewer.