' . str_replace( "\n", "
\n", $part ) . "
\n";
}
}
return $out;
}
/**
* Parse HTML into a DOM, returning the wrapper element.
*
* @param string $html HTML.
* @return \DOMElement|null
*/
private static function load( $html ) {
if ( ! class_exists( '\DOMDocument' ) ) {
return null;
}
$doc = new \DOMDocument();
$prev = libxml_use_internal_errors( true );
$ok = $doc->loadHTML( '' . $html . '
', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
libxml_clear_errors();
libxml_use_internal_errors( $prev );
if ( ! $ok ) {
return null;
}
foreach ( $doc->childNodes as $node ) {
if ( $node instanceof \DOMElement && 'div' === strtolower( $node->nodeName ) ) {
return $node;
}
}
return null;
}
/**
* Blocks for the children of an element.
*
* @param \DOMNode $parent Parent.
* @return array[]
*/
private static function children_to_blocks( \DOMNode $parent ) {
$blocks = array();
$inline = '';
$flush = static function () use ( &$inline, &$blocks ) {
$text = trim( preg_replace( '#^(\s*
\s*)+|(\s*
\s*)+$#i', '', $inline ) );
if ( '' !== trim( wp_strip_all_tags_compat( $text ) ) || false !== stripos( $text, '
childNodes as $node ) {
if ( $node instanceof \DOMComment ) {
$flush();
$data = trim( $node->data );
if ( 0 === strpos( $data, 'more' ) ) {
$blocks[] = Blocks::more();
} elseif ( 'nextpage' === $data ) {
$blocks[] = Blocks::make( 'core/nextpage', array(), array( 'html' => '' ) );
}
continue;
}
if ( $node instanceof \DOMText ) {
$inline .= self::outer( $node );
continue;
}
if ( ! $node instanceof \DOMElement ) {
continue;
}
$name = strtolower( $node->nodeName );
if ( in_array( $name, self::INLINE, true ) ) {
$inline .= self::outer( $node );
continue;
}
$flush();
$block = self::element( $node, $name );
if ( $block ) {
$blocks = array_merge( $blocks, $block );
}
}
$flush();
return $blocks;
}
/**
* Blocks for one block-level element.
*
* @param \DOMElement $node Element.
* @param string $name Lower-case tag name.
* @return array[]
*/
private static function element( \DOMElement $node, $name ) {
switch ( $name ) {
case 'p':
return self::para( $node );
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
$inner = self::inner( $node );
if ( '' === trim( wp_strip_all_tags_compat( $inner ) ) ) {
return array();
}
return array( Blocks::heading( $inner, (int) substr( $name, 1 ), self::align( $node ), self::ident( $node ) ) );
case 'ul':
case 'ol':
$list = self::lst( $node );
return array( $list ? $list : Blocks::html( self::outer( $node ) ) );
case 'blockquote':
$inner = self::children_to_blocks( $node );
foreach ( $inner as $b ) {
if ( ! in_array( $b['name'], array( 'core/paragraph', 'core/heading', 'core/list' ), true ) ) {
return array( Blocks::html( self::outer( $node ) ) );
}
}
return $inner ? array( Blocks::quote( $inner ) ) : array();
case 'hr':
return array( Blocks::separator() );
case 'figure':
$img = self::image_from( $node );
return array( $img ? $img : Blocks::html( self::outer( $node ) ) );
default:
return array( Blocks::html( self::outer( $node ) ) );
}
}
/**
* A paragraph, or an image, or a shortcode block when that is all the paragraph holds.
*
* @param \DOMElement $node Paragraph.
* @return array[]
*/
private static function para( \DOMElement $node ) {
$inner = trim( preg_replace( '#^(\s*
\s*)+|(\s*
\s*)+$#i', '', self::inner( $node ) ) );
$text = trim( wp_strip_all_tags_compat( $inner ) );
if ( '' === $text && false === stripos( $inner, '
childNodes as $li ) {
if ( $li instanceof \DOMText && '' === trim( $li->data ) ) {
continue;
}
if ( ! $li instanceof \DOMElement || 'li' !== strtolower( $li->nodeName ) ) {
return null;
}
$html = '';
$sub = null;
foreach ( $li->childNodes as $child ) {
if ( $child instanceof \DOMElement && in_array( strtolower( $child->nodeName ), array( 'ul', 'ol' ), true ) ) {
if ( null !== $sub ) {
return null;
}
$sub = self::lst( $child );
if ( null === $sub ) {
return null;
}
continue;
}
if ( $child instanceof \DOMElement && ! in_array( strtolower( $child->nodeName ), self::INLINE, true ) ) {
return null;
}
if ( null !== $sub && ! ( $child instanceof \DOMText && '' === trim( $child->data ) ) ) {
return null; // Text after a nested list cannot be kept in a list item.
}
$html .= self::outer( $child );
}
$items[] = array(
'html' => trim( $html ),
'sub' => $sub,
);
}
return $items ? Blocks::lst( $items, 'ol' === strtolower( $node->nodeName ) ) : null;
}
/**
* An image block from an element holding one image (optionally linked, optionally with a caption).
*
* @param \DOMElement $node Element.
* @return array|null
*/
private static function image_from( \DOMElement $node ) {
$imgs = $node->getElementsByTagName( 'img' );
if ( 1 !== $imgs->length ) {
return null;
}
$img = $imgs->item( 0 );
$parent = $img->parentNode;
$link = '';
$target = '';
$rel = '';
$caption = '';
if ( $parent instanceof \DOMElement && 'a' === strtolower( $parent->nodeName ) ) {
$link = $parent->getAttribute( 'href' );
$target = $parent->getAttribute( 'target' );
$rel = $parent->getAttribute( 'rel' );
}
$fig = $node->getElementsByTagName( 'figcaption' );
if ( $fig->length ) {
$caption = self::inner( $fig->item( 0 ) );
}
$class = ' ' . $img->getAttribute( 'class' ) . ' ' . ( $node instanceof \DOMElement ? $node->getAttribute( 'class' ) : '' ) . ' ';
$id = preg_match( '/\swp-image-(\d+)\s/', $class, $m ) ? (int) $m[1] : 0;
$size = preg_match( '/\ssize-([a-z0-9_\-]+)\s/i', $class, $m ) ? $m[1] : ( $id ? 'full' : '' );
$align = preg_match( '/\salign(left|center|right)\s/', $class, $m ) ? $m[1] : '';
return Blocks::image(
$id,
$img->getAttribute( 'src' ),
array(
'alt' => $img->getAttribute( 'alt' ),
'size' => $size,
'link' => $link,
'target' => $target,
'rel' => $rel,
'caption' => $caption,
'align' => $align,
'title' => $img->getAttribute( 'title' ),
)
);
}
/**
* Text alignment from an inline style or a class.
*
* @param \DOMElement $node Element.
* @return string
*/
private static function align( \DOMElement $node ) {
if ( preg_match( '/text-align\s*:\s*(left|center|right)/i', $node->getAttribute( 'style' ), $m ) ) {
return strtolower( $m[1] );
}
if ( preg_match( '/\b(?:has-text-align-|align)(left|center|right)\b/', $node->getAttribute( 'class' ), $m ) ) {
return $m[1];
}
return '';
}
/**
* An element's id as the block anchor.
*
* @param \DOMElement $node Element.
* @return array
*/
private static function ident( \DOMElement $node ) {
$id = $node->getAttribute( 'id' );
return ( '' !== $id && preg_match( '/^[A-Za-z][\w\-:.]*$/', $id ) ) ? array( 'anchor' => $id ) : array();
}
/**
* Inner HTML.
*
* @param \DOMNode $node Node.
* @return string
*/
private static function inner( \DOMNode $node ) {
$html = '';
foreach ( $node->childNodes as $child ) {
$html .= self::outer( $child );
}
return $html;
}
/**
* Outer HTML, with non-ASCII text kept as characters.
*
* @param \DOMNode $node Node.
* @return string
*/
private static function outer( \DOMNode $node ) {
$html = (string) $node->ownerDocument->saveHTML( $node );
return preg_replace_callback(
'/(\d+);|([0-9a-f]+);/i',
static function ( $m ) {
$code = '' !== $m[1] ? (int) $m[1] : hexdec( $m[2] );
// Keep markup-significant characters escaped.
if ( in_array( $code, array( 34, 38, 39, 60, 62, 160 ), true ) || $code < 32 ) {
return $m[0];
}
return html_entity_decode( '' . $code . ';', ENT_QUOTES | ENT_HTML5, 'UTF-8' );
},
$html
);
}
}
if ( ! function_exists( __NAMESPACE__ . '\wp_strip_all_tags_compat' ) ) {
/**
* Strip tags (and ) for "is this empty?" checks.
*
* @param string $html HTML.
* @return string
*/
function wp_strip_all_tags_compat( $html ) {
$text = preg_replace( '@<(script|style)[^>]*?>.*?\\1>@si', '', (string) $html );
$text = strip_tags( $text ); // phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags -- emptiness check only.
return str_replace( array( ' ', "\xc2\xa0" ), ' ', $text );
}
}