-
- /**
- * Replace the output with an error if the HTML is not valid.
- *
- * @param string $s
- * @return string
- */
- private static function validateAllHtml( $s ) {
- $errors = '';
- if ( MWTidy::checkErrors( $s, $errors ) ) {
- return $s;
- }
-
- header( 'Cache-Control: no-cache' );
-
- $out = Html::element( 'h1', null, 'HTML validation error' );
- $out .= Html::openElement( 'ul' );
-
- $error = strtok( $errors, "\n" );
- $badLines = [];
- while ( $error !== false ) {
- if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
- $lineNum = intval( $m[1] );
- $badLines[$lineNum] = true;
- $out .= Html::rawElement( 'li', null,
- Html::element( 'a', [ 'href' => "#line-{$lineNum}" ], $error ) ) . "\n";
- }
- $error = strtok( "\n" );
- }
-
- $out .= Html::closeElement( 'ul' );
- $out .= Html::element( 'pre', null, $errors );
- $out .= Html::openElement( 'ol' ) . "\n";
- $line = strtok( $s, "\n" );
- $i = 1;
- while ( $line !== false ) {
- $attrs = [];
- if ( isset( $badLines[$i] ) ) {
- $attrs['class'] = 'highlight';
- $attrs['id'] = "line-$i";
- }
- $out .= Html::element( 'li', $attrs, $line ) . "\n";
- $line = strtok( "\n" );
- $i++;
- }
- $out .= Html::closeElement( 'ol' );
-
- $style = <<<CSS
- .highlight { background-color: #ffc }
- li { white-space: pre }
-CSS;
-
- $out = Html::htmlHeader( [ 'lang' => 'en', 'dir' => 'ltr' ] ) .
- Html::rawElement( 'head', null,
- Html::element( 'title', null, 'HTML validation error' ) .
- Html::inlineStyle( $style ) ) .
- Html::rawElement( 'body', null, $out ) .
- Html::closeElement( 'html' );
-
- return $out;
- }