From 406e4c23153c2ce7adb6c3d9f6b4feccc4f77c6e Mon Sep 17 00:00:00 2001 From: Adam Wood Date: Fri, 27 Jan 2023 09:56:31 +1300 Subject: [PATCH] Add ListItem parser rather than branching in RichText --- env/export-content/includes/parser.php | 7 ++-- .../includes/parsers/ListItem.php | 40 +++++++++++++++++++ .../parsers/{RichText.php => Paragraph.php} | 16 +------- 3 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 env/export-content/includes/parsers/ListItem.php rename env/export-content/includes/parsers/{RichText.php => Paragraph.php} (69%) diff --git a/env/export-content/includes/parser.php b/env/export-content/includes/parser.php index 8ca45dd2..5b56ea61 100644 --- a/env/export-content/includes/parser.php +++ b/env/export-content/includes/parser.php @@ -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'; @@ -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(), diff --git a/env/export-content/includes/parsers/ListItem.php b/env/export-content/includes/parsers/ListItem.php new file mode 100644 index 00000000..15a55c04 --- /dev/null +++ b/env/export-content/includes/parsers/ListItem.php @@ -0,0 +1,40 @@ +get_attribute( 'placeholder', $block ); + + $matches = []; + + if ( preg_match( '/]*>(.+)<\/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 = '#(]*>)(' . preg_quote( $original, '/' ) . ')(<\/li>)#is'; + $html = preg_replace( $regex, '${1}' . addcslashes( $replacements[ $original ], '\\$' ) . '${3}', $html ); + } + } + + $block['innerHTML'] = $html; + $block['innerContent'] = [ $html ]; + + return $block; + } +} diff --git a/env/export-content/includes/parsers/RichText.php b/env/export-content/includes/parsers/Paragraph.php similarity index 69% rename from env/export-content/includes/parsers/RichText.php rename to env/export-content/includes/parsers/Paragraph.php index 6521c6f6..9f29a9bc 100644 --- a/env/export-content/includes/parsers/RichText.php +++ b/env/export-content/includes/parsers/Paragraph.php @@ -2,8 +2,7 @@ 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 { @@ -11,13 +10,7 @@ public function to_strings( array $block ) : array { $matches = []; - $regex = '/]*>(.+)<\/p>/is'; - - if ( $block['blockName'] === 'core/list-item' ) { - $regex = '/]*>(.+)<\/li>/is'; - } - - if ( preg_match( $regex, $block['innerHTML'], $matches ) ) { + if ( preg_match( '/]*>(.+)<\/p>/is', $block['innerHTML'], $matches ) ) { if ( ! empty( $matches[1] ) ) { $strings[] = $matches[1]; } @@ -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 = '#(]*>)(' . preg_quote( $original, '/' ) . ')(<\/p>)#is'; - - if ( $block['blockName'] === 'core/list-item' ) { - $regex = '#(]*>)(' . preg_quote( $original, '/' ) . ')(<\/li>)#is'; - } - $html = preg_replace( $regex, '${1}' . addcslashes( $replacements[ $original ], '\\$' ) . '${3}', $html ); } }