-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ListItem parser rather than branching in RichText
- Loading branch information
1 parent
225718e
commit 406e4c2
Showing
3 changed files
with
46 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters