public static function PHPUnit_Util_XML::convertSelectToTag in Zircon Profile 8
Same name and namespace in other branches
- 8.0 vendor/phpunit/phpunit/src/Util/XML.php \PHPUnit_Util_XML::convertSelectToTag()
Parse a CSS selector into an associative array suitable for use with findNodes().
@since Method available since Release 3.3.0
Parameters
string $selector:
mixed $content:
Return value
array
22 calls to PHPUnit_Util_XML::convertSelectToTag()
- PHPUnit_Util_XML::cssSelect in vendor/
phpunit/ phpunit/ src/ Util/ XML.php - Parse an $actual document and return an array of DOMNodes matching the CSS $selector. If an error occurs, it will return false.
- Util_XMLTest::testConvertAssertAttribute in vendor/
phpunit/ phpunit/ tests/ Util/ XMLTest.php - Util_XMLTest::testConvertAssertAttributeMultipleSpaces in vendor/
phpunit/ phpunit/ tests/ Util/ XMLTest.php - Util_XMLTest::testConvertAssertAttributeSpaces in vendor/
phpunit/ phpunit/ tests/ Util/ XMLTest.php - Util_XMLTest::testConvertAssertClass in vendor/
phpunit/ phpunit/ tests/ Util/ XMLTest.php
File
- vendor/
phpunit/ phpunit/ src/ Util/ XML.php, line 309
Class
- PHPUnit_Util_XML
- XML helpers.
Code
public static function convertSelectToTag($selector, $content = true) {
$selector = trim(preg_replace("/\\s+/", ' ', $selector));
// substitute spaces within attribute value
while (preg_match('/\\[[^\\]]+"[^"]+\\s[^"]+"\\]/', $selector)) {
$selector = preg_replace('/(\\[[^\\]]+"[^"]+)\\s([^"]+"\\])/', '$1__SPACE__$2', $selector);
}
if (strstr($selector, ' ')) {
$elements = explode(' ', $selector);
}
else {
$elements = array(
$selector,
);
}
$previousTag = array();
foreach (array_reverse($elements) as $element) {
$element = str_replace('__SPACE__', ' ', $element);
// child selector
if ($element == '>') {
$previousTag = array(
'child' => $previousTag['descendant'],
);
continue;
}
// adjacent-sibling selector
if ($element == '+') {
$previousTag = array(
'adjacent-sibling' => $previousTag['descendant'],
);
continue;
}
$tag = array();
// match element tag
preg_match("/^([^\\.#\\[]*)/", $element, $eltMatches);
if (!empty($eltMatches[1])) {
$tag['tag'] = $eltMatches[1];
}
// match attributes (\[[^\]]*\]*), ids (#[^\.#\[]*),
// and classes (\.[^\.#\[]*))
preg_match_all("/(\\[[^\\]]*\\]*|#[^\\.#\\[]*|\\.[^\\.#\\[]*)/", $element, $matches);
if (!empty($matches[1])) {
$classes = array();
$attrs = array();
foreach ($matches[1] as $match) {
// id matched
if (substr($match, 0, 1) == '#') {
$tag['id'] = substr($match, 1);
}
elseif (substr($match, 0, 1) == '.') {
$classes[] = substr($match, 1);
}
elseif (substr($match, 0, 1) == '[' && substr($match, -1, 1) == ']') {
$attribute = substr($match, 1, strlen($match) - 2);
$attribute = str_replace('"', '', $attribute);
// match single word
if (strstr($attribute, '~=')) {
list($key, $value) = explode('~=', $attribute);
$value = "regexp:/.*\\b{$value}\\b.*/";
}
elseif (strstr($attribute, '*=')) {
list($key, $value) = explode('*=', $attribute);
$value = "regexp:/.*{$value}.*/";
}
else {
list($key, $value) = explode('=', $attribute);
}
$attrs[$key] = $value;
}
}
if (!empty($classes)) {
$tag['class'] = implode(' ', $classes);
}
if (!empty($attrs)) {
$tag['attributes'] = $attrs;
}
}
// tag content
if (is_string($content)) {
$tag['content'] = $content;
}
// determine previous child/descendants
if (!empty($previousTag['descendant'])) {
$tag['descendant'] = $previousTag['descendant'];
}
elseif (!empty($previousTag['child'])) {
$tag['child'] = $previousTag['child'];
}
elseif (!empty($previousTag['adjacent-sibling'])) {
$tag['adjacent-sibling'] = $previousTag['adjacent-sibling'];
unset($tag['content']);
}
$previousTag = array(
'descendant' => $tag,
);
}
return $tag;
}