From: Brion Vibber Date: Sun, 6 Feb 2005 13:44:01 +0000 (+0000) Subject: Run a check on output for well-formedness. X-Git-Tag: 1.5.0alpha1~770 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=3a8dbacf290756321b208e8a11bd0bf880901bc8;p=lhc%2Fweb%2Fwiklou.git Run a check on output for well-formedness. --- diff --git a/maintenance/parserTests.php b/maintenance/parserTests.php index d493dd60bc..9c089fbb46 100644 --- a/maintenance/parserTests.php +++ b/maintenance/parserTests.php @@ -256,7 +256,7 @@ class ParserTest { $this->teardownGlobals(); - if( $result === $out ) { + if( $result === $out && $this->wellFormed( $out ) ) { return $this->showSuccess( $desc ); } else { return $this->showFailure( $desc, $result, $out ); @@ -504,6 +504,9 @@ class ParserTest { if( $this->showDiffs ) { print $this->quickDiff( $result, $html ); } + if( !$this->wellFormed( $html ) ) { + print "XML error: $this->mXmlError\n"; + } return false; } @@ -629,6 +632,56 @@ class ParserTest { } return $text; } + + function wellFormed( $text ) { + $html = + '' . + '' . + $text . + ''; + + $parser = xml_parser_create( "UTF-8" ); + + # case folding violates XML standard, turn it off + xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false ); + + if( !xml_parse( $parser, $html, true ) ) { + $err = xml_error_string( xml_get_error_code( $parser ) ); + $position = xml_get_current_byte_index( $parser ); + $fragment = $this->extractFragment( $html, $position ); + $this->mXmlError = "$err at byte $position:\n$fragment"; + xml_parser_free( $parser ); + return false; + } + xml_parser_free( $parser ); + return true; + } + + function extractFragment( $text, $position ) { + $start = max( 0, $position - 10 ); + $before = $position - $start; + $fragment = '...' . + $this->termColor( 34 ) . + substr( $text, $start, $before ) . + $this->termColor( 0 ) . + $this->termColor( 31 ) . + $this->termColor( 1 ) . + substr( $text, $position, 1 ) . + $this->termColor( 0 ) . + $this->termColor( 34 ) . + substr( $text, $position + 1, 9 ) . + $this->termColor( 0 ) . + '...'; + $display = str_replace( "\n", ' ', $fragment ); + $caret = ' ' . + str_repeat( ' ', $before ) . + $this->termColor( 31 ) . + '^' . + $this->termColor( 0 ); + return "$display\n$caret"; + } + } if( isset( $options['help'] ) ) {