<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[44116] trunk: Blocks: Introduce the block parser.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/44116">44116</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/44116","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>desrosj</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-12-13 17:39:59 +0000 (Thu, 13 Dec 2018)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Blocks: Introduce the block parser.
The `WP_Block_Parser` class, and the accompanying `parse_blocks()` helper function, can be used to parse an array of blocks out of a content string.
`WP_Block_Parser` is copied from the `@wordpress/block-serialization-default-parser` package. To ensure it stays in sync with the JavaScript parser, changes should be implemented in the package first, then the package version should be upgraded to include the changes.
Props pento.
Merges <a href="https://core.trac.wordpress.org/changeset/43751">[43751]</a> to trunk.
See <a href="https://core.trac.wordpress.org/ticket/45109">#45109</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
<li><a href="#trunktoolswebpackpackagesjs">trunk/tools/webpack/packages.js</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpblockparserphp">trunk/src/wp-includes/class-wp-block-parser.php</a></li>
<li>trunk/tests/phpunit/data/blocks/fixtures/</li>
<li><a href="#trunktestsphpunittestsblocksblockparserphp">trunk/tests/phpunit/tests/blocks/block-parser.php</a></li>
</ul>
<h3>Property Changed</h3>
<ul>
<li><a href="#trunk">trunk/</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: trunk
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- trunk 2018-12-13 17:26:09 UTC (rev 44115)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ trunk 2018-12-13 17:39:59 UTC (rev 44116)
</ins><a id="trunk"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: trunk</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/3.3:20543
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/3.4:21757
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/4.9:43557
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/branches/5.0:43681-43682,43684-43688,43719-43720,43723,43726-43727,43729-43731,43734-43744
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/branches/5.0:43681-43682,43684-43688,43719-43720,43723,43726-43727,43729-43731,43734-43744,43751
</ins><span class="cx" style="display: block; padding: 0 10px"> /trunk:18512
</span><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesblocksphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks.php 2018-12-13 17:26:09 UTC (rev 44115)
+++ trunk/src/wp-includes/blocks.php 2018-12-13 17:39:59 UTC (rev 44116)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -112,3 +112,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $dynamic_block_names;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Parses blocks out of a content string.
+ *
+ * @since 5.0.0
+ *
+ * @param string $content Post content.
+ * @return array Array of parsed block objects.
+ */
+function parse_blocks( $content ) {
+ /*
+ * If there are no blocks in the content, return a single block, rather
+ * than wasting time trying to parse the string.
+ */
+ if ( ! has_blocks( $content ) ) {
+ return array(
+ array(
+ 'blockName' => null,
+ 'attrs' => array(),
+ 'innerBlocks' => array(),
+ 'innerHTML' => $content,
+ ),
+ );
+ }
+
+ /**
+ * Filter to allow plugins to replace the server-side block parser
+ *
+ * @since 5.0.0
+ *
+ * @param string $parser_class Name of block parser class
+ */
+ $parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
+
+ $parser = new $parser_class();
+ return $parser->parse( $content );
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesclasswpblockparserphpfromrev43751branches50srcwpincludesclasswpblockparserphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/src/wp-includes/class-wp-block-parser.php (from rev 43751, branches/5.0/src/wp-includes/class-wp-block-parser.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-block-parser.php (rev 0)
+++ trunk/src/wp-includes/class-wp-block-parser.php 2018-12-13 17:39:59 UTC (rev 44116)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,473 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Class WP_Block_Parser_Block
+ *
+ * Holds the block structure in memory
+ *
+ * @since 3.8.0
+ */
+class WP_Block_Parser_Block {
+ /**
+ * Name of block
+ *
+ * @example "core/paragraph"
+ *
+ * @since 3.8.0
+ * @var string
+ */
+ public $blockName;
+
+ /**
+ * Optional set of attributes from block comment delimiters
+ *
+ * @example null
+ * @example array( 'columns' => 3 )
+ *
+ * @since 3.8.0
+ * @var array|null
+ */
+ public $attrs;
+
+ /**
+ * List of inner blocks (of this same class)
+ *
+ * @since 3.8.0
+ * @var WP_Block_Parser_Block[]
+ */
+ public $innerBlocks;
+
+ /**
+ * Resultant HTML from inside block comment delimiters
+ * after removing inner blocks
+ *
+ * @example "...Just <!-- wp:test /--> testing..." -> "Just testing..."
+ *
+ * @since 3.8.0
+ * @var string
+ */
+ public $innerHTML;
+
+ function __construct( $name, $attrs, $innerBlocks, $innerHTML ) {
+ $this->blockName = $name;
+ $this->attrs = $attrs;
+ $this->innerBlocks = $innerBlocks;
+ $this->innerHTML = $innerHTML;
+ }
+}
+
+/**
+ * Class WP_Block_Parser_Frame
+ *
+ * Holds partial blocks in memory while parsing
+ *
+ * @internal
+ * @since 3.8.0
+ */
+class WP_Block_Parser_Frame {
+ /**
+ * Full or partial block
+ *
+ * @since 3.8.0
+ * @var WP_Block_Parser_Block
+ */
+ public $block;
+
+ /**
+ * Byte offset into document for start of parse token
+ *
+ * @since 3.8.0
+ * @var int
+ */
+ public $token_start;
+
+ /**
+ * Byte length of entire parse token string
+ *
+ * @since 3.8.0
+ * @var int
+ */
+ public $token_length;
+
+ /**
+ * Byte offset into document for after parse token ends
+ * (used during reconstruction of stack into parse production)
+ *
+ * @since 3.8.0
+ * @var int
+ */
+ public $prev_offset;
+
+ /**
+ * Byte offset into document where leading HTML before token starts
+ *
+ * @since 3.8.0
+ * @var int
+ */
+ public $leading_html_start;
+
+ function __construct( $block, $token_start, $token_length, $prev_offset = null, $leading_html_start = null ) {
+ $this->block = $block;
+ $this->token_start = $token_start;
+ $this->token_length = $token_length;
+ $this->prev_offset = isset( $prev_offset ) ? $prev_offset : $token_start + $token_length;
+ $this->leading_html_start = $leading_html_start;
+ }
+}
+
+/**
+ * Class WP_Block_Parser
+ *
+ * Parses a document and constructs a list of parsed block objects
+ *
+ * @since 3.8.0
+ * @since 4.0.0 returns arrays not objects, all attributes are arrays
+ */
+class WP_Block_Parser {
+ /**
+ * Input document being parsed
+ *
+ * @example "Pre-text\n<!-- wp:paragraph -->This is inside a block!<!-- /wp:paragraph -->"
+ *
+ * @since 3.8.0
+ * @var string
+ */
+ public $document;
+
+ /**
+ * Tracks parsing progress through document
+ *
+ * @since 3.8.0
+ * @var int
+ */
+ public $offset;
+
+ /**
+ * List of parsed blocks
+ *
+ * @since 3.8.0
+ * @var WP_Block_Parser_Block[]
+ */
+ public $output;
+
+ /**
+ * Stack of partially-parsed structures in memory during parse
+ *
+ * @since 3.8.0
+ * @var WP_Block_Parser_Frame[]
+ */
+ public $stack;
+
+ /**
+ * Parses a document and returns a list of block structures
+ *
+ * When encountering an invalid parse will return a best-effort
+ * parse. In contrast to the specification parser this does not
+ * return an error on invalid inputs.
+ *
+ * @since 3.8.0
+ *
+ * @param string $document
+ * @return WP_Block_Parser_Block[]
+ */
+ function parse( $document ) {
+ $this->document = $document;
+ $this->offset = 0;
+ $this->output = array();
+ $this->stack = array();
+
+ do {
+ // twiddle our thumbs
+ } while ( $this->proceed() );
+
+ return $this->output;
+ }
+
+ /**
+ * Processes the next token from the input document
+ * and returns whether to proceed eating more tokens
+ *
+ * This is the "next step" function that essentially
+ * takes a token as its input and decides what to do
+ * with that token before descending deeper into a
+ * nested block tree or continuing along the document
+ * or breaking out of a level of nesting.
+ *
+ * @internal
+ * @since 3.8.0
+ * @return bool
+ */
+ function proceed() {
+ $next_token = $this->next_token();
+ list( $token_type, $block_name, $attrs, $start_offset, $token_length ) = $next_token;
+ $stack_depth = count( $this->stack );
+
+ // we may have some HTML soup before the next block
+ $leading_html_start = $start_offset > $this->offset ? $this->offset : null;
+
+ switch ( $token_type ) {
+ case 'no-more-tokens':
+ // if not in a block then flush output
+ if ( 0 === $stack_depth ) {
+ $this->add_freeform();
+ return false;
+ }
+
+ /*
+ * Otherwise we have a problem
+ * This is an error
+ *
+ * we have options
+ * - treat it all as freeform text
+ * - assume an implicit closer (easiest when not nesting)
+ */
+
+ // for the easy case we'll assume an implicit closer
+ if ( 1 === $stack_depth ) {
+ $this->add_block_from_stack();
+ return false;
+ }
+
+ /*
+ * for the nested case where it's more difficult we'll
+ * have to assume that multiple closers are missing
+ * and so we'll collapse the whole stack piecewise
+ */
+ while ( 0 < count( $this->stack ) ) {
+ $this->add_block_from_stack();
+ }
+ return false;
+
+ case 'void-block':
+ /*
+ * easy case is if we stumbled upon a void block
+ * in the top-level of the document
+ */
+ if ( 0 === $stack_depth ) {
+ if ( isset( $leading_html_start ) ) {
+ $this->output[] = (array) self::freeform( substr(
+ $this->document,
+ $leading_html_start,
+ $start_offset - $leading_html_start
+ ) );
+ }
+
+ $this->output[] = (array) new WP_Block_Parser_Block( $block_name, $attrs, array(), '' );
+ $this->offset = $start_offset + $token_length;
+ return true;
+ }
+
+ // otherwise we found an inner block
+ $this->add_inner_block(
+ new WP_Block_Parser_Block( $block_name, $attrs, array(), '' ),
+ $start_offset,
+ $token_length
+ );
+ $this->offset = $start_offset + $token_length;
+ return true;
+
+ case 'block-opener':
+ // track all newly-opened blocks on the stack
+ array_push( $this->stack, new WP_Block_Parser_Frame(
+ new WP_Block_Parser_Block( $block_name, $attrs, array(), '' ),
+ $start_offset,
+ $token_length,
+ $start_offset + $token_length,
+ $leading_html_start
+ ) );
+ $this->offset = $start_offset + $token_length;
+ return true;
+
+ case 'block-closer':
+ /*
+ * if we're missing an opener we're in trouble
+ * This is an error
+ */
+ if ( 0 === $stack_depth ) {
+ /*
+ * we have options
+ * - assume an implicit opener
+ * - assume _this_ is the opener
+ * - give up and close out the document
+ */
+ $this->add_freeform();
+ return false;
+ }
+
+ // if we're not nesting then this is easy - close the block
+ if ( 1 === $stack_depth ) {
+ $this->add_block_from_stack( $start_offset );
+ $this->offset = $start_offset + $token_length;
+ return true;
+ }
+
+ /*
+ * otherwise we're nested and we have to close out the current
+ * block and add it as a new innerBlock to the parent
+ */
+ $stack_top = array_pop( $this->stack );
+ $stack_top->block->innerHTML .= substr( $this->document, $stack_top->prev_offset, $start_offset - $stack_top->prev_offset );
+ $stack_top->prev_offset = $start_offset + $token_length;
+
+ $this->add_inner_block(
+ $stack_top->block,
+ $stack_top->token_start,
+ $stack_top->token_length,
+ $start_offset + $token_length
+ );
+ $this->offset = $start_offset + $token_length;
+ return true;
+
+ default:
+ // This is an error
+ $this->add_freeform();
+ return false;
+ }
+ }
+
+ /**
+ * Scans the document from where we last left off
+ * and finds the next valid token to parse if it exists
+ *
+ * Returns the type of the find: kind of find, block information, attributes
+ *
+ * @internal
+ * @since 3.8.0
+ * @return array
+ */
+ function next_token() {
+ $matches = null;
+
+ /*
+ * aye the magic
+ * we're using a single RegExp to tokenize the block comment delimiters
+ * we're also using a trick here because the only difference between a
+ * block opener and a block closer is the leading `/` before `wp:` (and
+ * a closer has no attributes). we can trap them both and process the
+ * match back in PHP to see which one it was.
+ */
+ $has_match = preg_match(
+ '/<!--\s+(?<closer>\/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{(?:(?!}\s+-->).)+?}\s+)?(?<void>\/)?-->/s',
+ $this->document,
+ $matches,
+ PREG_OFFSET_CAPTURE,
+ $this->offset
+ );
+
+ // we have no more tokens
+ if ( 0 === $has_match ) {
+ return array( 'no-more-tokens', null, null, null, null );
+ }
+
+ list( $match, $started_at ) = $matches[ 0 ];
+
+ $length = strlen( $match );
+ $is_closer = isset( $matches[ 'closer' ] ) && -1 !== $matches[ 'closer' ][ 1 ];
+ $is_void = isset( $matches[ 'void' ] ) && -1 !== $matches[ 'void' ][ 1 ];
+ $namespace = $matches[ 'namespace' ];
+ $namespace = ( isset( $namespace ) && -1 !== $namespace[ 1 ] ) ? $namespace[ 0 ] : 'core/';
+ $name = $namespace . $matches[ 'name' ][ 0 ];
+ $has_attrs = isset( $matches[ 'attrs' ] ) && -1 !== $matches[ 'attrs' ][ 1 ];
+
+ /*
+ * Fun fact! It's not trivial in PHP to create "an empty associative array" since all arrays
+ * are associative arrays. If we use `array()` we get a JSON `[]`
+ */
+ $attrs = $has_attrs
+ ? json_decode( $matches[ 'attrs' ][ 0 ], /* as-associative */ true )
+ : json_decode( '{}', /* don't ask why, just verify in PHP */ false );
+
+ /*
+ * This state isn't allowed
+ * This is an error
+ */
+ if ( $is_closer && ( $is_void || $has_attrs ) ) {
+ // we can ignore them since they don't hurt anything
+ }
+
+ if ( $is_void ) {
+ return array( 'void-block', $name, $attrs, $started_at, $length );
+ }
+
+ if ( $is_closer ) {
+ return array( 'block-closer', $name, null, $started_at, $length );
+ }
+
+ return array( 'block-opener', $name, $attrs, $started_at, $length );
+ }
+
+ /**
+ * Returns a new block object for freeform HTML
+ *
+ * @internal
+ * @since 3.9.0
+ *
+ * @param string $innerHTML HTML content of block
+ * @return WP_Block_Parser_Block freeform block object
+ */
+ static function freeform( $innerHTML ) {
+ return new WP_Block_Parser_Block( null, array(), array(), $innerHTML );
+ }
+
+ /**
+ * Pushes a length of text from the input document
+ * to the output list as a freeform block
+ *
+ * @internal
+ * @since 3.8.0
+ * @param null $length how many bytes of document text to output
+ */
+ function add_freeform( $length = null ) {
+ $length = $length ? $length : strlen( $this->document ) - $this->offset;
+
+ if ( 0 === $length ) {
+ return;
+ }
+
+ $this->output[] = (array) self::freeform( substr( $this->document, $this->offset, $length ) );
+ }
+
+ /**
+ * Given a block structure from memory pushes
+ * a new block to the output list
+ *
+ * @internal
+ * @since 3.8.0
+ * @param WP_Block_Parser_Block $block the block to add to the output
+ * @param int $token_start byte offset into the document where the first token for the block starts
+ * @param int $token_length byte length of entire block from start of opening token to end of closing token
+ * @param int|null $last_offset last byte offset into document if continuing form earlier output
+ */
+ function add_inner_block( WP_Block_Parser_Block $block, $token_start, $token_length, $last_offset = null ) {
+ $parent = $this->stack[ count( $this->stack ) - 1 ];
+ $parent->block->innerBlocks[] = $block;
+ $parent->block->innerHTML .= substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset );
+ $parent->prev_offset = $last_offset ? $last_offset : $token_start + $token_length;
+ }
+
+ /**
+ * Pushes the top block from the parsing stack to the output list
+ *
+ * @internal
+ * @since 3.8.0
+ * @param int|null $end_offset byte offset into document for where we should stop sending text output as HTML
+ */
+ function add_block_from_stack( $end_offset = null ) {
+ $stack_top = array_pop( $this->stack );
+ $prev_offset = $stack_top->prev_offset;
+
+ $stack_top->block->innerHTML .= isset( $end_offset )
+ ? substr( $this->document, $prev_offset, $end_offset - $prev_offset )
+ : substr( $this->document, $prev_offset );
+
+ if ( isset( $stack_top->leading_html_start ) ) {
+ $this->output[] = (array) self::freeform( substr(
+ $this->document,
+ $stack_top->leading_html_start,
+ $stack_top->token_start - $stack_top->leading_html_start
+ ) );
+ }
+
+ $this->output[] = (array) $stack_top->block;
+ }
+}
</ins></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-settings.php 2018-12-13 17:26:09 UTC (rev 44115)
+++ trunk/src/wp-settings.php 2018-12-13 17:39:59 UTC (rev 44116)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -246,6 +246,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/search/class-wp-rest-post-search-handler.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/class-wp-block-type.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/class-wp-block-type-registry.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require( ABSPATH . WPINC . '/class-wp-block-parser.php' );
</ins><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/blocks.php' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $GLOBALS['wp_embed'] = new WP_Embed();
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksblockparserphpfromrev43751branches50testsphpunittestsblocksblockparserphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/tests/blocks/block-parser.php (from rev 43751, branches/5.0/tests/phpunit/tests/blocks/block-parser.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/block-parser.php (rev 0)
+++ trunk/tests/phpunit/tests/blocks/block-parser.php 2018-12-13 17:39:59 UTC (rev 44116)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,120 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * WP_Block_Parser tests.
+ *
+ * @package WordPress
+ * @subpackage Blocks
+ * @since 5.0.0
+ */
+
+/**
+ * Tests for WP_Block_Parser
+ *
+ * @since 5.0.0
+ *
+ * @group blocks
+ */
+class WP_Test_Block_Parser extends WP_UnitTestCase {
+ /**
+ * The location of the fixtures to test with.
+ *
+ * @since 5.0.0
+ * @var string
+ */
+ protected static $fixtures_dir;
+
+ /**
+ * @ticket 45109
+ */
+ public function data_parsing_test_filenames() {
+ self::$fixtures_dir = DIR_TESTDATA . '/blocks/fixtures';
+
+ $fixture_filenames = array_merge(
+ glob( self::$fixtures_dir . '/*.json' ),
+ glob( self::$fixtures_dir . '/*.html' )
+ );
+
+ $fixture_filenames = array_values(
+ array_unique(
+ array_map(
+ array( $this, 'clean_fixture_filename' ),
+ $fixture_filenames
+ )
+ )
+ );
+
+ return array_map(
+ array( $this, 'pass_parser_fixture_filenames' ),
+ $fixture_filenames
+ );
+ }
+
+ /**
+ * @dataProvider data_parsing_test_filenames
+ * @ticket 45109
+ */
+ public function test_default_parser_output( $html_filename, $parsed_json_filename ) {
+ $html_path = self::$fixtures_dir . '/' . $html_filename;
+ $parsed_json_path = self::$fixtures_dir . '/' . $parsed_json_filename;
+
+ foreach ( array( $html_path, $parsed_json_path ) as $filename ) {
+ if ( ! file_exists( $filename ) ) {
+ //throw new Exception( "Missing fixture file: '$filename'" );
+ }
+ }
+
+ $html = self::strip_r( file_get_contents( $html_path ) );
+ $expected_parsed = json_decode( self::strip_r( file_get_contents( $parsed_json_path ) ), true );
+
+ $parser = new WP_Block_Parser();
+ $result = json_decode( json_encode( $parser->parse( $html ) ), true );
+
+ $this->assertEquals(
+ $expected_parsed,
+ $result,
+ "File '$parsed_json_filename' does not match expected value"
+ );
+ }
+
+ /**
+ * Helper function to remove relative paths and extension from a filename, leaving just the fixture name.
+ *
+ * @since 5.0.0
+ *
+ * @param string $filename The filename to clean.
+ * @return string The cleaned fixture name.
+ */
+ protected function clean_fixture_filename( $filename ) {
+ $filename = basename( $filename );
+ $filename = preg_replace( '/\..+$/', '', $filename );
+ return $filename;
+ }
+
+ /**
+ * Helper function to return the filenames needed to test the parser output.
+ *
+ * @since 5.0.0
+ *
+ * @param string $filename The cleaned fixture name.
+ * @return array The input and expected output filenames for that fixture.
+ */
+ protected function pass_parser_fixture_filenames( $filename ) {
+ return array(
+ "$filename.html",
+ "$filename.parsed.json",
+ );
+ }
+
+ /**
+ * Helper function to remove '\r' characters from a string.
+ *
+ * @since 5.0.0
+ *
+ * @param string $input The string to remove '\r' from.
+ * @return string The input string, with '\r' characters removed.
+ */
+ protected function strip_r( $input ) {
+ return str_replace( "\r", '', $input );
+ }
+
+}
</ins></span></pre></div>
<a id="trunktoolswebpackpackagesjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tools/webpack/packages.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tools/webpack/packages.js 2018-12-13 17:26:09 UTC (rev 44115)
+++ trunk/tools/webpack/packages.js 2018-12-13 17:39:59 UTC (rev 44116)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -116,6 +116,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'wp-polyfill-node-contains.min.js': 'polyfill-library/polyfills/Node/prototype/contains/polyfill.js',
</span><span class="cx" style="display: block; padding: 0 10px"> };
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ const phpFiles = {
+ 'block-serialization-default-parser/parser.php': 'wp-includes/class-wp-block-parser.php',
+ };
+
</ins><span class="cx" style="display: block; padding: 0 10px"> const externals = {
</span><span class="cx" style="display: block; padding: 0 10px"> react: 'React',
</span><span class="cx" style="display: block; padding: 0 10px"> 'react-dom': 'ReactDOM',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -164,6 +168,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> } ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ const phpCopies = Object.keys( phpFiles ).map( ( filename ) => ( {
+ from: join( baseDir, `node_modules/@wordpress/${ filename }` ),
+ to: join( baseDir, `src/${ phpFiles[ filename ] }` ),
+ } ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> const config = {
</span><span class="cx" style="display: block; padding: 0 10px"> mode,
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -232,6 +241,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> [
</span><span class="cx" style="display: block; padding: 0 10px"> ...vendorCopies,
</span><span class="cx" style="display: block; padding: 0 10px"> ...cssCopies,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ ...phpCopies,
</ins><span class="cx" style="display: block; padding: 0 10px"> ],
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> ],
</span></span></pre>
</div>
</div>
</body>
</html>