Make WordPress Core


Ignore:
Timestamp:
12/17/2018 04:50:48 AM (6 years ago)
Author:
desrosj
Message:

Block Editor: Update @wordpress dependencies.

Changes of note:

  • Includes the new Annotations API package.
  • wp-polyfill-ecmascript.js is renamed to wp-polyfill.js.
  • strip_dynamic_blocks() has been removed in favor of excerpt_remove_blocks().
  • The PHP block parser is now syncing from the block-serialization-default-parser package.
  • do_blocks() uses the new parser.
  • The do_block filter has been removed from do_blocks(), in favor of a render_block filter in render_block().

Also, a little cleanup to render_block(). Always normalize $block['attrs’] to array in ’render_block’ filter.
Props pento, azaozz.

Merges [43884] and [43888] to trunk.

See #45145, #45190, #45264, #45282.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/class-wp-block-parser.php

    r44118 r44261  
    4949    public $innerHTML;
    5050
    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;
    5671    }
    5772}
     
    160175
    161176    /**
     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    /**
    162185     * Parses a document and returns a list of block structures
    163186     *
     
    172195     */
    173196    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 );
    178202
    179203        do {
     
    255279                    }
    256280
    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() );
    258282                    $this->offset   = $start_offset + $token_length;
    259283                    return true;
     
    262286                // otherwise we found an inner block
    263287                $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() ),
    265289                    $start_offset,
    266290                    $token_length
     
    274298                    $this->stack,
    275299                    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() ),
    277301                        $start_offset,
    278302                        $token_length,
     
    311335                 * block and add it as a new innerBlock to the parent
    312336                 */
    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;
    323342
    324343                $this->add_inner_block(
     
    360379         */
    361380        $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',
    363382            $this->document,
    364383            $matches,
     
    388407        $attrs = $has_attrs
    389408            ? json_decode( $matches['attrs'][0], /* as-associative */ true )
    390             : json_decode( '{}', /* don't ask why, just verify in PHP */ false );
     409            : $this->empty_attrs;
    391410
    392411        /*
     
    418437     * @return WP_Block_Parser_Block freeform block object
    419438     */
    420     static function 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 ) );
    422441    }
    423442
     
    453472    function add_inner_block( WP_Block_Parser_Block $block, $token_start, $token_length, $last_offset = null ) {
    454473        $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 ) );
    456474        $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;
    458484    }
    459485
     
    473499            : substr( $this->document, $prev_offset );
    474500
    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;
    479504        }
    480505
Note: See TracChangeset for help on using the changeset viewer.