Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid blindly re-encoding HTML files #373

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,26 @@ public static function extractErrorMessage( ?ResponseInterface $resp, RequestInt
*/
public static function buildDOMDocumentFromHtml( string $html ): DOMDocument {
$document = new DOMDocument( '1.0', 'UTF-8' );

libxml_use_internal_errors( true );
$document->loadHTML( mb_convert_encoding( str_replace( '<?xml version="1.0" encoding="UTF-8" ?>', '', $html ), 'HTML-ENTITIES', 'UTF-8' ) );
// enforce a UTF-8 encoding declaration
$document->loadHTML( '<?xml encoding="utf-8" ?>' . $html );
libxml_clear_errors();

$document->encoding = 'UTF-8';

// Dirty fix to strip out existing XML Processing Instruction nodes
// (we already have one from the creation of the DOMDocument)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to loadHTML() without adding the PI? II feels a bit inefficient to loop through the whole document (which could be quite large) only to find a dupe of something we've just added here. (Sorry if I'm missing the obvious!)

//
// This is better than re-encoding from UTF-8 to HTML-ENTITIES, because
// that will escape things even in CDATA blocks (e.g. T271390)
// https://www.php.net/manual/en/domdocument.loadhtml.php#95251
foreach ( $document->childNodes as $item ) {
if ( $item->nodeType === XML_PI_NODE ) {
$document->removeChild( $item );
}
}

return $document;
}

Expand Down