Normalize tag instead of retreiving attributes manually
This commit is contained in:
parent
5a93393e04
commit
8b03f99fcb
|
@ -43,15 +43,16 @@ class BlockInserter {
|
||||||
* @param WP_Post $post Post.
|
* @param WP_Post $post Post.
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function get_content_with_block_insertions( $content, $post ) {
|
public function get_content_with_block_insertions( $content, $post = null ) {
|
||||||
$new_content = '';
|
$new_content = '';
|
||||||
$p = new \WP_HTML_Tag_Processor( $content );
|
$p = new \WP_HTML_Tag_Processor( $content );
|
||||||
|
|
||||||
while( $p->next_token() ) {
|
while( $p->next_token() ) {
|
||||||
switch ( $p->get_token_type() ) {
|
switch ( $p->get_token_type() ) {
|
||||||
case '#comment':
|
case '#comment':
|
||||||
$block_name = $this->get_block_name_from_comment( $p->get_modifiable_text() );
|
$text = $p->get_modifiable_text();
|
||||||
$is_closer = strpos( $p->get_modifiable_text(), ' /wp:' ) === 0;
|
$block_name = $this->get_block_name_from_comment( $text );
|
||||||
|
$is_closer = str_starts_with( $text, ' /wp:' );
|
||||||
|
|
||||||
if ( $block_name ) {
|
if ( $block_name ) {
|
||||||
$new_content .= $this->get_content_with_block_insertions(
|
$new_content .= $this->get_content_with_block_insertions(
|
||||||
|
@ -66,7 +67,7 @@ class BlockInserter {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$new_content .= '<!--' . $p->get_modifiable_text() . '-->';
|
$new_content .= '<!--' . $text . '-->';
|
||||||
|
|
||||||
if ( $block_name ) {
|
if ( $block_name ) {
|
||||||
$new_content .= $this->get_content_with_block_insertions(
|
$new_content .= $this->get_content_with_block_insertions(
|
||||||
|
@ -83,9 +84,7 @@ class BlockInserter {
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case '#tag':
|
case '#tag':
|
||||||
$new_content .= ! $p->is_tag_closer()
|
$new_content .= $this->normalize_tag( $p );
|
||||||
? '<' . strtolower( $p->get_tag() ) . $this->get_attributes_string( $p ) . '>'
|
|
||||||
: '</' . strtolower( $p->get_tag() ) . '>';
|
|
||||||
break;
|
break;
|
||||||
case '#text':
|
case '#text':
|
||||||
$new_content .= $p->get_modifiable_text();
|
$new_content .= $p->get_modifiable_text();
|
||||||
|
@ -96,6 +95,37 @@ class BlockInserter {
|
||||||
return $new_content;
|
return $new_content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize tag.
|
||||||
|
*
|
||||||
|
* @return string Normalized tag.
|
||||||
|
*/
|
||||||
|
private function normalize_tag( $p ) {
|
||||||
|
if ( null === $p->get_tag() ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tag_name = strtolower( $p->get_tag() );
|
||||||
|
|
||||||
|
if ( $p->is_tag_closer() ) {
|
||||||
|
return "</{$tag_name}>";
|
||||||
|
}
|
||||||
|
|
||||||
|
$attributes = $p->get_attribute_names_with_prefix( '' );
|
||||||
|
|
||||||
|
if ( null === $attributes ) {
|
||||||
|
return "<{$tag_name}>";
|
||||||
|
}
|
||||||
|
|
||||||
|
$builder = new \WP_HTML_Tag_Processor( "<{$tag_name}>" );
|
||||||
|
$builder->next_tag();
|
||||||
|
foreach ( $attributes as $name ) {
|
||||||
|
$builder->set_attribute( $name, $p->get_attribute( $name ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $builder->get_updated_html();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the attributes string used in tags.
|
* Get the attributes string used in tags.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue