-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This includes the updates from WordPress/wordpress.org#90, a new AttributeParser for attribute-only blocks, and a fix for the ListItem block to allow child lists. Fixes #211
- Loading branch information
Showing
10 changed files
with
300 additions
and
144 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,31 @@ | ||
<?php | ||
|
||
namespace WordPress_org\Main_2022\ExportToPatterns\Parsers; | ||
|
||
class AttributeParser implements BlockParser { | ||
use GetSetAttribute; | ||
|
||
public $attributes = []; | ||
|
||
public function __construct( $attributes = array() ) { | ||
$this->attributes = (array) $attributes; | ||
} | ||
|
||
public function to_strings( array $block ) : array { | ||
$strings = []; | ||
foreach ( $this->attributes as $attr ) { | ||
$results = $this->get_attribute( $attr, $block ); | ||
$strings = array_merge( $strings, $results ); | ||
} | ||
|
||
return $strings; | ||
} | ||
|
||
public function replace_strings( array $block, array $replacements ) : array { | ||
foreach ( $this->attributes as $attr ) { | ||
$this->set_attribute( $attr, $block, $replacements ); | ||
} | ||
|
||
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
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,95 @@ | ||
<?php | ||
|
||
namespace WordPress_org\Main_2022\ExportToPatterns\Parsers; | ||
|
||
class HTMLParser implements BlockParser { | ||
use GetSetAttribute; | ||
|
||
public $tags = []; | ||
public $attributes = []; | ||
|
||
public function __construct( $tags = array(), $attributes = array() ) { | ||
$this->tags = (array) $tags; | ||
$this->attributes = (array) $attributes; | ||
} | ||
|
||
public function to_strings( array $block ) : array { | ||
$strings = $this->get_attribute( 'placeholder', $block ); | ||
|
||
foreach ( $this->tags as $tag ) { | ||
$tag = $this->escape_tag( $tag, '#' ); | ||
|
||
if ( preg_match_all( "#<{$tag}[^>]*>\s*(?P<string>.+?)\s*</{$tag}>#is", $block['innerHTML'], $matches ) ) { | ||
$strings = array_merge( $strings, $matches['string'] ); | ||
} | ||
} | ||
|
||
foreach ( $this->attributes as $attr ) { | ||
$attr = $this->escape_attr( $attr, '#' ); | ||
|
||
if ( | ||
str_contains( $block['innerHTML'], "='" ) && | ||
preg_match_all( "#{$attr}='(?P<string>[^']+?)'#is", $block['innerHTML'], $matches ) | ||
) { | ||
$strings = array_merge( $strings, $matches['string'] ); | ||
} | ||
|
||
if ( | ||
str_contains( $block['innerHTML'], '="' ) && | ||
preg_match_all( "#{$attr}=\"(?P<string>[^\"]+?)\"#is", $block['innerHTML'], $matches ) | ||
) { | ||
$strings = array_merge( $strings, $matches['string'] ); | ||
} | ||
} | ||
|
||
return $strings; | ||
} | ||
|
||
// todo: this needs a fix to properly rebuild innerContent - see 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 ] ) ) { | ||
continue; | ||
} | ||
|
||
// TODO: Potentially this should be more specific for tags/attribute replacements as needed. | ||
$regex = '#([>"\'])\s*' . preg_quote( $original, '#' ) . '\s*([\'"<])#s'; | ||
$html = preg_replace( $regex, '$1' . addcslashes( $replacements[ $original ], '\\$' ) . '$2', $html ); | ||
} | ||
|
||
$block['innerHTML'] = $html; | ||
$block['innerContent'] = [ $html ]; | ||
|
||
return $block; | ||
} | ||
|
||
/** | ||
* Escape a tag/attribute to use in a regex. | ||
*/ | ||
protected function escape_tag( $string, $delim ) { | ||
return $this->escape( $string, $delim ); | ||
} | ||
protected function escape_attr( $string, $delim ) { | ||
return $this->escape( $string, $delim ); | ||
} | ||
protected function escape( $string, $delim ) { | ||
return preg_quote( $string, $delim ); | ||
} | ||
} | ||
|
||
class HTMLRegexParser extends HTMLParser { | ||
/** | ||
* Maybe escape a string for a regex match, unless it looks like regex (ie. /..../) then use as-is. | ||
*/ | ||
protected function escape_tag( $string, $delim ) { | ||
if ( str_starts_with( $string, '/' ) && str_ends_with( $string, '/' ) ) { | ||
return trim( $string, '/' ); | ||
} | ||
|
||
return parent::escape_tag( $string, $delim ); | ||
} | ||
} |
Oops, something went wrong.