ENHANCEMENT Added CSSContentParser->getByXpath() (from r105126)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112440 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-10-15 01:26:24 +00:00
parent 8d07a8eccc
commit e44ba25714

View File

@ -53,16 +53,33 @@ class CSSContentParser extends Object {
/** /**
* Returns a number of SimpleXML elements that match the given CSS selector. * Returns a number of SimpleXML elements that match the given CSS selector.
* Currently the selector engine only supports querying by tag, id, and classs * Currently the selector engine only supports querying by tag, id, and class.
* See {@link getByXpath()} for a more direct selector syntax.
*
* @param String $selector
* @return SimpleXMLElement
*/ */
function getBySelector($selector) { function getBySelector($selector) {
$xpath = $this->selector2xpath($selector); $xpath = $this->selector2xpath($selector);
return $this->simpleXML->xpath($xpath); return $this->getByXpath($xpath);
} }
/**
* Allows querying the content through XPATH selectors.
*
* @param String $xpath SimpleXML compatible XPATH statement
* @return SimpleXMLElement|false
*/
function getByXpath($xpath) {
return $this->simpleXML->xpath($xpath);
}
/** /**
* Converts a CSS selector into an equivalent xpath expression. * Converts a CSS selector into an equivalent xpath expression.
* Currently the selector engine only supports querying by tag, id, and classs * Currently the selector engine only supports querying by tag, id, and class.
*
* @param String $selector See {@link getBySelector()}
* @return String XPath expression
*/ */
function selector2xpath($selector) { function selector2xpath($selector) {
$parts = preg_split('/\\s+/', $selector); $parts = preg_split('/\\s+/', $selector);
@ -85,4 +102,5 @@ class CSSContentParser extends Object {
return $xpath; return $xpath;
} }
} }