Skip to content

Commit

Permalink
Add ListItem parser rather than branching in RichText
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoodnz committed Jan 31, 2023
1 parent 225718e commit 406e4c2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
7 changes: 4 additions & 3 deletions env/export-content/includes/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
require_once __DIR__ . '/parsers/BasicText.php';
require_once __DIR__ . '/parsers/Button.php';
require_once __DIR__ . '/parsers/Heading.php';
require_once __DIR__ . '/parsers/ListItem.php';
require_once __DIR__ . '/parsers/Noop.php';
require_once __DIR__ . '/parsers/RichText.php';
require_once __DIR__ . '/parsers/Paragraph.php';

// Unused.
require_once __DIR__ . '/parsers/ShortcodeBlock.php';
Expand All @@ -21,8 +22,8 @@ class BlockParser {
public function __construct() {
$this->parsers = [
// Core blocks that have custom parsers.
'core/paragraph' => new Parsers\RichText(),
'core/list-item' => new Parsers\RichText(),
'core/paragraph' => new Parsers\Paragraph(),
'core/list-item' => new Parsers\ListItem(),
'core/heading' => new Parsers\Heading(),
'core/button' => new Parsers\Button(),
'core/spacer' => new Parsers\Noop(),
Expand Down
40 changes: 40 additions & 0 deletions env/export-content/includes/parsers/ListItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace WordPress_org\Main_2022\ExportToPatterns\Parsers;

class ListItem implements BlockParser {
use GetSetAttribute;

public function to_strings( array $block ) : array {
$strings = $this->get_attribute( 'placeholder', $block );

$matches = [];

if ( preg_match( '/<li[^>]*>(.+)<\/li>/is', $block['innerHTML'], $matches ) ) {
if ( ! empty( $matches[1] ) ) {
$strings[] = $matches[1];
}
}

return $strings;
}

// todo: this needs a fix to properly rebuild innerContent - similar to ParagraphParserTest
public function replace_strings( array $block, array $replacements ) : array {
$this->set_attribute( 'placeholder', $block, $replacements );

$html = $block['innerHTML'];

foreach ( $this->to_strings( $block ) as $original ) {
if ( ! empty( $original ) && isset( $replacements[ $original ] ) ) {
$regex = '#(<li[^>]*>)(' . preg_quote( $original, '/' ) . ')(<\/li>)#is';
$html = preg_replace( $regex, '${1}' . addcslashes( $replacements[ $original ], '\\$' ) . '${3}', $html );
}
}

$block['innerHTML'] = $html;
$block['innerContent'] = [ $html ];

return $block;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@

namespace WordPress_org\Main_2022\ExportToPatterns\Parsers;

// Default block type is core/paragraph but also handles core/list-item
class RichText implements BlockParser {
class Paragraph implements BlockParser {
use GetSetAttribute;

public function to_strings( array $block ) : array {
$strings = $this->get_attribute( 'placeholder', $block );

$matches = [];

$regex = '/<p[^>]*>(.+)<\/p>/is';

if ( $block['blockName'] === 'core/list-item' ) {
$regex = '/<li[^>]*>(.+)<\/li>/is';
}

if ( preg_match( $regex, $block['innerHTML'], $matches ) ) {
if ( preg_match( '/<p[^>]*>(.+)<\/p>/is', $block['innerHTML'], $matches ) ) {
if ( ! empty( $matches[1] ) ) {
$strings[] = $matches[1];
}
Expand All @@ -35,11 +28,6 @@ public function replace_strings( array $block, array $replacements ) : array {
foreach ( $this->to_strings( $block ) as $original ) {
if ( ! empty( $original ) && isset( $replacements[ $original ] ) ) {
$regex = '#(<p[^>]*>)(' . preg_quote( $original, '/' ) . ')(<\/p>)#is';

if ( $block['blockName'] === 'core/list-item' ) {
$regex = '#(<li[^>]*>)(' . preg_quote( $original, '/' ) . ')(<\/li>)#is';
}

$html = preg_replace( $regex, '${1}' . addcslashes( $replacements[ $original ], '\\$' ) . '${3}', $html );
}
}
Expand Down

0 comments on commit 406e4c2

Please sign in to comment.