From 7f40b2b9534b101e293dff67c0e22118470f4a85 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sun, 12 Oct 2008 16:15:24 +0000 Subject: [PATCH] BUGFIX Fixed CSSContentParser to only use tidy on CLI mode if its available, and first check for existence of PHP tidy extension. Fixes failing unit tests on standard WAMP windows installations. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64097 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- dev/CSSContentParser.php | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/dev/CSSContentParser.php b/dev/CSSContentParser.php index fc7dc7f8f..274dda197 100644 --- a/dev/CSSContentParser.php +++ b/dev/CSSContentParser.php @@ -9,6 +9,10 @@ * It was built to facilitate testing using PHPUnit and contains a number of assert methods that will throw PHPUnit * assertion exception when applicable. * + * Tries to use the PHP Tidy extension (http://php.net/tidy), + * and falls back to the "tidy" CLI tool. If none of those exists, + * the string is parsed directly without sanitization. + * * @package sapphire * @subpackage core */ @@ -16,10 +20,33 @@ class CSSContentParser extends Object { protected $simpleXML = null; function __construct($content) { - $CLI_content = escapeshellarg($content); - $tidy = `echo $CLI_content | tidy -n -q -utf8 -asxhtml 2> /dev/null`; - $tidy = str_replace('xmlns="http://www.w3.org/1999/xhtml"','',$tidy); - $tidy = str_replace(' ','',$tidy); + if(extension_loaded('tidy')) { + // using the tiny php extension + $tidy = new Tidy(); + $tidy->parseString( + $content, + array( + 'output-xhtml' => true, + 'numeric-entities' => true, + ), + 'utf8' + ); + $tidy->cleanRepair(); + $tidy = str_replace('xmlns="http://www.w3.org/1999/xhtml"','',$tidy); + $tidy = str_replace(' ','',$tidy); + } elseif(`which tidy`) { + // using tiny through cli + $CLI_content = escapeshellarg($content); + $tidy = `echo $CLI_content | tidy -n -q -utf8 -asxhtml 2> /dev/null`; + $tidy = str_replace('xmlns="http://www.w3.org/1999/xhtml"','',$tidy); + $tidy = str_replace(' ','',$tidy); + } else { + // no tidy library found, hence no sanitizing + $tidy = $content; + } + + + $this->simpleXML = new SimpleXMLElement($tidy); }