2008-05-15 09:15:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CSSContentParser enables parsing & assertion running of HTML content via CSS selectors.
|
|
|
|
* It works by converting the content to XHTML using tidy, rewriting the CSS selectors as XPath queries, and executing
|
|
|
|
* those using SimpeXML.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-05-15 09:15:33 +02:00
|
|
|
* It was built to facilitate testing using PHPUnit and contains a number of assert methods that will throw PHPUnit
|
|
|
|
* assertion exception when applicable.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-08-21 03:56:05 +02:00
|
|
|
* Tries to use the PHP tidy extension (http://php.net/tidy),
|
2008-10-12 18:15:24 +02:00
|
|
|
* and falls back to the "tidy" CLI tool. If none of those exists,
|
|
|
|
* the string is parsed directly without sanitization.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-06-05 11:38:27 +02:00
|
|
|
* Caution: Doesn't fully support HTML elements like <header>
|
|
|
|
* due to them being declared illegal by the "tidy" preprocessing step.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-06-15 15:33:53 +02:00
|
|
|
* @subpackage core
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
|
|
|
class CSSContentParser extends Object {
|
|
|
|
protected $simpleXML = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($content) {
|
2008-10-12 18:15:24 +02:00
|
|
|
if(extension_loaded('tidy')) {
|
2014-08-21 03:56:05 +02:00
|
|
|
// using the tidy php extension
|
|
|
|
$tidy = new tidy();
|
2008-10-12 18:15:24 +02:00
|
|
|
$tidy->parseString(
|
2014-08-15 08:53:05 +02:00
|
|
|
$content,
|
2008-10-12 18:15:24 +02:00
|
|
|
array(
|
|
|
|
'output-xhtml' => true,
|
|
|
|
'numeric-entities' => true,
|
2014-08-19 16:36:44 +02:00
|
|
|
'wrap' => 0, // We need this to be consistent for functional test string comparisons
|
2014-08-15 08:53:05 +02:00
|
|
|
),
|
2008-10-12 18:15:24 +02:00
|
|
|
'utf8'
|
|
|
|
);
|
|
|
|
$tidy->cleanRepair();
|
|
|
|
$tidy = str_replace('xmlns="http://www.w3.org/1999/xhtml"','',$tidy);
|
|
|
|
$tidy = str_replace(' ','',$tidy);
|
2010-10-13 05:57:07 +02:00
|
|
|
|
2009-07-17 01:46:24 +02:00
|
|
|
} elseif(@shell_exec('which tidy')) {
|
2008-10-12 18:15:24 +02:00
|
|
|
// using tiny through cli
|
|
|
|
$CLI_content = escapeshellarg($content);
|
2014-09-03 13:02:04 +02:00
|
|
|
$tidy = `echo $CLI_content | tidy --force-output 1 -n -q -utf8 -asxhtml -w 0 2> /dev/null`;
|
2008-10-12 18:15:24 +02:00
|
|
|
$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;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 03:23:00 +02:00
|
|
|
$this->simpleXML = @simplexml_load_string($tidy, 'SimpleXMLElement', LIBXML_NOWARNING);
|
|
|
|
if(!$this->simpleXML) {
|
2012-09-26 23:34:00 +02:00
|
|
|
throw new Exception('CSSContentParser::__construct(): Could not parse content.'
|
|
|
|
. ' Please check the PHP extension tidy is installed.');
|
2010-10-19 03:23:00 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-09-18 05:02:19 +02:00
|
|
|
parent::__construct();
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
/**
|
|
|
|
* Returns a number of SimpleXML elements that match the given CSS selector.
|
2010-10-15 03:26:24 +02:00
|
|
|
* Currently the selector engine only supports querying by tag, id, and class.
|
|
|
|
* See {@link getByXpath()} for a more direct selector syntax.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 03:26:24 +02:00
|
|
|
* @param String $selector
|
|
|
|
* @return SimpleXMLElement
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getBySelector($selector) {
|
2008-05-15 09:15:33 +02:00
|
|
|
$xpath = $this->selector2xpath($selector);
|
2010-10-15 03:26:24 +02:00
|
|
|
return $this->getByXpath($xpath);
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 03:26:24 +02:00
|
|
|
/**
|
|
|
|
* Allows querying the content through XPATH selectors.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 03:26:24 +02:00
|
|
|
* @param String $xpath SimpleXML compatible XPATH statement
|
|
|
|
* @return SimpleXMLElement|false
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getByXpath($xpath) {
|
2010-10-15 03:26:24 +02:00
|
|
|
return $this->simpleXML->xpath($xpath);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
/**
|
|
|
|
* Converts a CSS selector into an equivalent xpath expression.
|
2010-10-15 03:26:24 +02:00
|
|
|
* Currently the selector engine only supports querying by tag, id, and class.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 03:26:24 +02:00
|
|
|
* @param String $selector See {@link getBySelector()}
|
|
|
|
* @return String XPath expression
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function selector2xpath($selector) {
|
2008-05-15 09:15:33 +02:00
|
|
|
$parts = preg_split('/\\s+/', $selector);
|
|
|
|
$xpath = "";
|
|
|
|
foreach($parts as $part) {
|
|
|
|
if(preg_match('/^([A-Za-z][A-Za-z0-9]*)/', $part, $matches)) {
|
|
|
|
$xpath .= "//$matches[1]";
|
|
|
|
} else {
|
|
|
|
$xpath .= "//*";
|
|
|
|
}
|
|
|
|
$xfilters = array();
|
|
|
|
if(preg_match('/#([^#.\[]+)/', $part, $matches)) {
|
|
|
|
$xfilters[] = "@id='$matches[1]'";
|
|
|
|
}
|
|
|
|
if(preg_match('/\.([^#.\[]+)/', $part, $matches)) {
|
|
|
|
$xfilters[] = "contains(@class,'$matches[1]')";
|
|
|
|
}
|
|
|
|
if($xfilters) $xpath .= '[' . implode(',', $xfilters) . ']';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
return $xpath;
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
|
|
|
|
2010-10-15 03:26:24 +02:00
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|