Skip to content

Commit

Permalink
Prevent corrupted content if self-closing tags are used
Browse files Browse the repository at this point in the history
  • Loading branch information
ocean90 committed May 21, 2024
1 parent 1cce4aa commit cee32cb
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function enable_lightbox_for_images( string $content ): string {
$libxml_use_internal_errors = libxml_use_internal_errors( true );
$document_loaded = $document->loadHTML(
sprintf(
'<?xml encoding="utf-8"?>%s',
'<?xml encoding="utf-8"?><PreserveSelfClosingTags>%s</PreserveSelfClosingTags>',
$content
),
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
Expand Down Expand Up @@ -144,8 +144,18 @@ function enable_lightbox_for_images( string $content ): string {
$link->setAttribute( 'data-group', $image_id );
}

// Strip the XML tag as it's only used for the internal encoding.
$document_html = trim( str_replace( '<?xml encoding="utf-8"?>', '', (string) $document->saveHTML() ) );
// Strip the XML tag and placeholders as it's only used for the internal encoding.
$document_html = trim(
str_replace(
[
'<?xml encoding="utf-8"?>',
'<PreserveSelfClosingTags>',
'</PreserveSelfClosingTags>',
],
'',
(string) $document->saveHTML()
)
);
if ( $document_html ) {
$content = $document_html;
}
Expand Down

0 comments on commit cee32cb

Please sign in to comment.