@@ -1414,30 +1414,61 @@ protected function evalSelectorPart($part)
14141414 * Collapse selectors
14151415 *
14161416 * @param array $selectors
1417+ * @param bool $selectorFormat
1418+ * if false return a collapsed string
1419+ * if true return an array description of a structured selector
14171420 *
14181421 * @return string
14191422 */
1420- protected function collapseSelectors ($ selectors )
1423+ protected function collapseSelectors ($ selectors, $ selectorFormat = false )
14211424 {
14221425 $ parts = [];
14231426
14241427 foreach ($ selectors as $ selector ) {
1425- $ output = '' ;
1428+ $ output = [];
1429+ $ glueNext = false ;
14261430 foreach ($ selector as $ node ) {
1427- $ output .= ( $ output ? ' ' : '' ) ;
1431+ $ compound = '' ;
14281432
14291433 array_walk_recursive (
14301434 $ node ,
1431- function ($ value , $ key ) use (&$ output ) {
1432- $ output .= $ value ;
1435+ function ($ value , $ key ) use (&$ compound ) {
1436+ $ compound .= $ value ;
14331437 }
14341438 );
1439+ if ($ selectorFormat && $ this ->isImmediateRelationshipCombinator ($ compound )) {
1440+ if (count ($ output )) {
1441+ $ output [count ($ output ) - 1 ] .= ' ' . $ compound ;
1442+ } else {
1443+ $ output [] = $ compound ;
1444+ }
1445+ $ glueNext = true ;
1446+ } elseif ($ glueNext ) {
1447+ $ output [count ($ output ) - 1 ] .= ' ' . $ compound ;
1448+ $ glueNext = false ;
1449+ } else {
1450+ $ output [] = $ compound ;
1451+ }
14351452 }
14361453
1454+ if ($ selectorFormat ) {
1455+ foreach ($ output as &$ o ) {
1456+ $ o = [Type::T_STRING , '' , [$ o ]];
1457+ }
1458+ $ output = [Type::T_LIST , ' ' , $ output ];
1459+ } else {
1460+ $ output = implode (' ' , $ output );
1461+ }
14371462 $ parts [] = $ output ;
14381463 }
14391464
1440- return implode (', ' , $ parts );
1465+ if ($ selectorFormat ) {
1466+ $ parts = [Type::T_LIST , ', ' , $ parts ];
1467+ } else {
1468+ $ parts = implode (', ' , $ parts );
1469+ }
1470+
1471+ return $ parts ;
14411472 }
14421473
14431474 /**
@@ -2638,9 +2669,8 @@ protected function reduce($value, $inExp = false)
26382669
26392670 case Type::T_SELF :
26402671 $ selfSelector = $ this ->multiplySelectors ($ this ->env );
2641- $ selfSelector = $ this ->collapseSelectors ($ selfSelector );
2642-
2643- return [Type::T_STRING , '' , [$ selfSelector ]];
2672+ $ selfSelector = $ this ->collapseSelectors ($ selfSelector , true );
2673+ return $ selfSelector ;
26442674
26452675 default :
26462676 return $ value ;
0 commit comments