tests/ValidHtmlTest.php
/**
	 * Parse and re-serialize an HTML string, using the WHATWG
	 * standard parsing and fragment serialization algorithms.
	 * @param string $input An HTML string
	 * @param bool $noisyErrors True if you want HTML errors to be reported
	 *   on stderr
	 * @return string A 'cleaned up' HTML string
	 */
Parse and re-serialize an HTML string, using the WHATWG
standard parsing and fragment serialization algorithms.
@param string $input An HTML string
@param bool $noisyErrors True if you want HTML errors to be reported
on stderr
@return string A 'cleaned up' HTML string
Parse and re - serialize an HTML string using the WHATWG standard parsing and fragment serialization algorithms . @param string $input An HTML string @param bool $noisyErrors True if you want HTML errors to be reported on stderr @return string A ' cleaned up ' HTML string
public static function makeValidHtml( string $input, bool $noisyErrors = true ): string {
		$errorList = [];
		$errorCallback = function ( $text, $pos ) use ( &$errorList, $noisyErrors ) {
			if ( $noisyErrors ) {
				error_log( "$text\n" );
			}
		};
		$formatter = new class( [] ) extends HtmlFormatter {
			public function startDocument( $fragmentNamespace, $fragmentName ) {
			}
		};
		$serializer = new Serializer( $formatter, $errorCallback );
		$treeBuilder = new TreeBuilder( $serializer, [] );
		$dispatcher = new Dispatcher( $treeBuilder );
		$tokenizer = new Tokenizer( $dispatcher, $input, [] );
		$tokenizer->execute( [
			'fragmentNamespace' => HTMLData::NS_HTML,
			'fragmentName' => 'body',
		] );
		return $serializer->getResult() . implode( '', $errorList );
	}
public static function makeValidHtml ( string $ input , bool $ noisyErrors = true ) : string { $ errorList = [ ] ; $ errorCallback = function ( $ text , $ pos ) use ( & $ errorList , $ noisyErrors ) { if ( $ noisyErrors ) { error_log ( " $ text \n " ) ; } } ; $ formatter = new class ( [ ] ) extends HtmlFormatter { public function startDocument ( $ fragmentNamespace , $ fragmentName ) { } } ; $ serializer = new Serializer ( $ formatter , $ errorCallback ) ; $ treeBuilder = new TreeBuilder ( $ serializer , [ ] ) ; $ dispatcher = new Dispatcher ( $ treeBuilder ) ; $ tokenizer = new Tokenizer ( $ dispatcher , $ input , [ ] ) ; $ tokenizer -> execute ( [ 'fragmentNamespace' => HTMLData :: NS_HTML , 'fragmentName' => 'body' , ] ) ; return $ serializer -> getResult ( ) . implode ( '' , $ errorList ) ; }
Parse and re-serialize an HTML string, using the WHATWG
 standard parsing and fragment serialization algorithms.
Parse and re - serialize an HTML string using the WHATWG standard parsing and fragment serialization algorithms .
# If you want to see the errors inline in the HTML, uncomment this: # $errorList[] = "\n<!-- $text -->"; # But we found it more confusing than helpful for the first task, # so for now just spit out the HTML errors on stderr. /* Suppress doctype */