/**
* HTML validation and correction
*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
* @file
+ * @ingroup Parser
*/
/**
* we may create a real postprocessor or something that will replace this.
* It's called wrapper because for now it basically takes over MWTidy::tidy's task
* of wrapping the text in a xhtml block
- *
+ *
* This re-uses some of the parser's UNIQ tricks, though some of it is private so it's
* duplicated. Perhaps we should create an abstract marker hiding class.
+ *
+ * @ingroup Parser
*/
class MWTidyWrapper {
$this->mUniqPrefix = "\x7fUNIQ" .
dechex( mt_rand( 0, 0x7fffffff ) ) . dechex( mt_rand( 0, 0x7fffffff ) );
$this->mMarkerIndex = 0;
-
+
+ // Replace <mw:editsection> elements with placeholders
$wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
array( &$this, 'replaceEditSectionLinksCallback' ), $text );
+ // Modify inline Microdata <link> and <meta> elements so they say <html-link> and <html-meta> so
+ // we can trick Tidy into not stripping them out by including them in tidy's new-empty-tags config
+ $wrappedtext = preg_replace( '!<(link|meta)([^>]*?)(/{0,1}>)!', '<html-$1$2$3', $wrappedtext );
+
+ // Wrap the whole thing in a doctype and body for Tidy.
$wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
'<head><title>test</title></head><body>'.$wrappedtext.'</body></html>';
* @return string
*/
public function postprocess( $text ) {
- return $this->mTokens->replace( $text );
+ // Revert <html-{link,meta}> back to <{link,meta}>
+ $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
+
+ // Restore the contents of placeholder tokens
+ $text = $this->mTokens->replace( $text );
+
+ return $text;
}
}
*/
public static function checkErrors( $text, &$errorStr = null ) {
global $wgTidyInternal;
-
+
$retval = 0;
if( $wgTidyInternal ) {
$errorStr = self::execInternalTidy( $text, true, $retval );
*
* @param $text String: HTML to check
* @param $stderr Boolean: Whether to read result from STDERR rather than STDOUT
- * @param &$retval Exit code (-1 on internal error)
+ * @param &$retval int Exit code (-1 on internal error)
* @return mixed String or null
*/
private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
2 => array( 'file', wfGetNull(), 'a' )
);
}
-
+
$readpipe = $stderr ? 2 : 1;
$pipes = array();
*
* @param $text String: HTML to check
* @param $stderr Boolean: Whether to read result from error status instead of output
- * @param &$retval Exit code (-1 on internal error)
+ * @param &$retval int Exit code (-1 on internal error)
* @return mixed String or null
*/
private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
if ( !MWInit::classExists( 'tidy' ) ) {
wfWarn( "Unable to load internal tidy class." );
$retval = -1;
-
+
wfProfileOut( __METHOD__ );
return null;
}
wfProfileOut( __METHOD__ );
return $tidy->errorBuffer;
+ }
+
+ $tidy->cleanRepair();
+ $retval = $tidy->getStatus();
+ if ( $retval == 2 ) {
+ // 2 is magic number for fatal error
+ // http://www.php.net/manual/en/function.tidy-get-status.php
+ $cleansource = null;
} else {
- $tidy->cleanRepair();
- $retval = $tidy->getStatus();
- if ( $retval == 2 ) {
- // 2 is magic number for fatal error
- // http://www.php.net/manual/en/function.tidy-get-status.php
- $cleansource = null;
- } else {
- $cleansource = tidy_get_output( $tidy );
- if ( $wgDebugTidy && $retval > 0 ) {
- $cleansource .= "<!--\nTidy reports:\n" .
- str_replace( '-->', '-->', $tidy->errorBuffer ) .
- "\n-->";
- }
+ $cleansource = tidy_get_output( $tidy );
+ if ( $wgDebugTidy && $retval > 0 ) {
+ $cleansource .= "<!--\nTidy reports:\n" .
+ str_replace( '-->', '-->', $tidy->errorBuffer ) .
+ "\n-->";
}
-
- wfProfileOut( __METHOD__ );
- return $cleansource;
}
+
+ wfProfileOut( __METHOD__ );
+ return $cleansource;
}
}