Skip to content

Commit 504f0bf

Browse files
committed
implement proper list handling/parsing
1 parent dbd33ae commit 504f0bf

File tree

4 files changed

+53
-68
lines changed

4 files changed

+53
-68
lines changed

ph-css/src/main/java/com/helger/css/decl/CSSSelectorMemberPseudoHas.java

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.helger.commons.annotation.ReturnsMutableCopy;
2727
import com.helger.commons.collection.impl.CommonsArrayList;
2828
import com.helger.commons.collection.impl.ICommonsList;
29-
import com.helger.commons.equals.EqualsHelper;
3029
import com.helger.commons.hashcode.HashCodeGenerator;
3130
import com.helger.commons.state.EChange;
3231
import com.helger.commons.string.ToStringGenerator;
@@ -46,40 +45,27 @@
4645
@NotThreadSafe
4746
public class CSSSelectorMemberPseudoHas implements ICSSSelectorMember, ICSSVersionAware, ICSSSourceLocationAware
4847
{
49-
private final ECSSSelectorCombinator m_eCombinator;
5048
private final ICommonsList <CSSSelector> m_aNestedSelectors;
5149
private CSSSourceLocation m_aSourceLocation;
5250

53-
public CSSSelectorMemberPseudoHas (@Nullable final ECSSSelectorCombinator eCombinator,
54-
@Nonnull final CSSSelector aNestedSelector)
51+
public CSSSelectorMemberPseudoHas (@Nonnull final CSSSelector aNestedSelector)
5552
{
5653
ValueEnforcer.notNull (aNestedSelector, "NestedSelector");
57-
m_eCombinator = eCombinator;
5854
m_aNestedSelectors = new CommonsArrayList <> (aNestedSelector);
5955
}
6056

61-
public CSSSelectorMemberPseudoHas (@Nullable final ECSSSelectorCombinator eCombinator,
62-
@Nonnull final CSSSelector... aNestedSelectors)
57+
public CSSSelectorMemberPseudoHas (@Nonnull final CSSSelector... aNestedSelectors)
6358
{
6459
ValueEnforcer.notNull (aNestedSelectors, "NestedSelectors");
65-
m_eCombinator = eCombinator;
6660
m_aNestedSelectors = new CommonsArrayList <> (aNestedSelectors);
6761
}
6862

69-
public CSSSelectorMemberPseudoHas (@Nullable final ECSSSelectorCombinator eCombinator,
70-
@Nonnull final Iterable <CSSSelector> aNestedSelectors)
63+
public CSSSelectorMemberPseudoHas (@Nonnull final Iterable <CSSSelector> aNestedSelectors)
7164
{
7265
ValueEnforcer.notNull (aNestedSelectors, "NestedSelectors");
73-
m_eCombinator = eCombinator;
7466
m_aNestedSelectors = new CommonsArrayList <> (aNestedSelectors);
7567
}
7668

77-
@Nullable
78-
public ECSSSelectorCombinator getCombinator ()
79-
{
80-
return m_eCombinator;
81-
}
82-
8369
public boolean hasSelectors ()
8470
{
8571
return m_aNestedSelectors.isNotEmpty ();
@@ -177,10 +163,6 @@ public String getAsCSSString (@Nonnull final ICSSWriterSettings aSettings, @Nonn
177163

178164
final boolean bOptimizedOutput = aSettings.isOptimizedOutput ();
179165
final StringBuilder aSB = new StringBuilder (":has(");
180-
181-
if (m_eCombinator != null)
182-
aSB.append (m_eCombinator.getAsCSSString (aSettings));
183-
184166
boolean bFirst = true;
185167
for (final CSSSelector aNestedSelector : m_aNestedSelectors)
186168
{
@@ -218,20 +200,19 @@ public boolean equals (final Object o)
218200
if (o == null || !getClass ().equals (o.getClass ()))
219201
return false;
220202
final CSSSelectorMemberPseudoHas rhs = (CSSSelectorMemberPseudoHas) o;
221-
return EqualsHelper.equals (m_eCombinator, rhs.m_eCombinator) && m_aNestedSelectors.equals (rhs.m_aNestedSelectors);
203+
return m_aNestedSelectors.equals (rhs.m_aNestedSelectors);
222204
}
223205

224206
@Override
225207
public int hashCode ()
226208
{
227-
return new HashCodeGenerator (this).append (m_eCombinator).append (m_aNestedSelectors).getHashCode ();
209+
return new HashCodeGenerator (this).append (m_aNestedSelectors).getHashCode ();
228210
}
229211

230212
@Override
231213
public String toString ()
232214
{
233-
return new ToStringGenerator (null).append ("Combinator", m_eCombinator)
234-
.append ("NestedSelectors", m_aNestedSelectors)
215+
return new ToStringGenerator (null).append ("NestedSelectors", m_aNestedSelectors)
235216
.appendIfNotNull ("SourceLocation", m_aSourceLocation)
236217
.getToString ();
237218
}

ph-css/src/main/java/com/helger/css/handler/CSSNodeToDomainObject.java

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -321,30 +321,12 @@ private ICSSSelectorMember _createSelectorMember (final CSSNode aNode)
321321

322322
if (ECSSNodeType.PSEUDO_HAS.isNode (aChildNode, m_eVersion))
323323
{
324-
ECSSSelectorCombinator eSelectorCombinator = null;
325-
326324
final int nChildChildCount = aChildNode.jjtGetNumChildren ();
327-
int i = 0;
328-
CSSNode aChildChildNode = aChildNode.jjtGetChild (i);
329-
if (ECSSNodeType.SELECTORCOMBINATOR.isNode (aChildChildNode, m_eVersion))
330-
{
331-
eSelectorCombinator = _createSelectorCombinator (aChildChildNode.getText ());
332-
if (eSelectorCombinator != null)
333-
{
334-
// Skip the first elements as selector
335-
i++;
336-
}
337-
}
338-
339325
final ICommonsList <CSSSelector> aNestedSelectors = new CommonsArrayList <> ();
340-
for (; i < nChildChildCount; ++i)
341-
{
342-
aChildChildNode = aChildNode.jjtGetChild (i);
343-
final CSSSelector aSelector = _createSelector (aChildChildNode);
344-
aNestedSelectors.add (aSelector);
345-
}
326+
for (int j = 0; j < nChildChildCount; ++j)
327+
aNestedSelectors.add (_createRelativeSelector(aChildNode.jjtGetChild (j)));
346328

347-
final CSSSelectorMemberPseudoHas ret = new CSSSelectorMemberPseudoHas (eSelectorCombinator, aNestedSelectors);
329+
final CSSSelectorMemberPseudoHas ret = new CSSSelectorMemberPseudoHas (aNestedSelectors);
348330
if (m_bUseSourceLocation)
349331
ret.setSourceLocation (aNode.getSourceLocation ());
350332
return ret;
@@ -355,11 +337,7 @@ private ICSSSelectorMember _createSelectorMember (final CSSNode aNode)
355337
final int nChildChildCount = aChildNode.jjtGetNumChildren ();
356338
final ICommonsList <CSSSelector> aNestedSelectors = new CommonsArrayList <> ();
357339
for (int j = 0; j < nChildChildCount; ++j)
358-
{
359-
final CSSSelector aSelector = _createSelector (aChildNode.jjtGetChild (j));
360-
aNestedSelectors.add (aSelector);
361-
}
362-
340+
aNestedSelectors.add (_createSelector (aChildNode.jjtGetChild (j)));
363341
final CSSSelectorMemberPseudoWhere ret = new CSSSelectorMemberPseudoWhere (aNestedSelectors);
364342
if (m_bUseSourceLocation)
365343
ret.setSourceLocation (aNode.getSourceLocation ());
@@ -371,11 +349,7 @@ private ICSSSelectorMember _createSelectorMember (final CSSNode aNode)
371349
final int nChildChildCount = aChildNode.jjtGetNumChildren ();
372350
final ICommonsList <CSSSelector> aNestedSelectors = new CommonsArrayList <> ();
373351
for (int j = 0; j < nChildChildCount; ++j)
374-
{
375-
final CSSSelector aSelector = _createSelector (aChildNode.jjtGetChild (j));
376-
aNestedSelectors.add (aSelector);
377-
}
378-
352+
aNestedSelectors.add (_createSelector (aChildNode.jjtGetChild (j)));
379353
final CSSSelectorMemberPseudoIs ret = new CSSSelectorMemberPseudoIs (aNestedSelectors);
380354
if (m_bUseSourceLocation)
381355
ret.setSourceLocation (aNode.getSourceLocation ());
@@ -423,6 +397,23 @@ private CSSSelector _createSelector (@Nonnull final CSSNode aNode)
423397
return ret;
424398
}
425399

400+
@Nonnull
401+
private CSSSelector _createRelativeSelector (@Nonnull final CSSNode aNode)
402+
{
403+
_expectNodeType (aNode, ECSSNodeType.RELATIVESELECTOR);
404+
405+
final CSSSelector ret = new CSSSelector ();
406+
if (m_bUseSourceLocation)
407+
ret.setSourceLocation (aNode.getSourceLocation ());
408+
for (final CSSNode aChildNode : aNode)
409+
{
410+
final ICSSSelectorMember aMember = _createSelectorMember (aChildNode);
411+
if (aMember != null)
412+
ret.addMember (aMember);
413+
}
414+
return ret;
415+
}
416+
426417
@Nonnull
427418
private CSSExpressionMemberMathProduct _createExpressionCalcProduct (@Nonnull final CSSNode aNode)
428419
{

ph-css/src/main/java/com/helger/css/handler/ECSSNodeType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public enum ECSSNodeType
4949
FONTFACERULE (ParserCSS30TreeConstants.JJTFONTFACERULE),
5050
// top level -- style rule
5151
SELECTOR (ParserCSS30TreeConstants.JJTSELECTOR),
52+
RELATIVESELECTOR (ParserCSS30TreeConstants.JJTRELATIVESELECTOR),
5253
STYLEDECLARATIONLIST (ParserCSS30TreeConstants.JJTSTYLEDECLARATIONLIST),
5354
STYLEDECLARATION (ParserCSS30TreeConstants.JJTSTYLEDECLARATION),
5455
// style rule -- selector

ph-css/src/main/jjtree/ParserCSS30.jjt

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,23 +1060,25 @@ void pseudoSlotted () #slotted : {}
10601060
)*
10611061
}
10621062

1063-
void relativeSelector() #void : {}
1063+
void relativeSelector() : {}
10641064
{
10651065
( selectorCombinator()
10661066
( <S> )*
10671067
)?
10681068
selector ()
1069-
( <S> )*
1070-
( <COMMA>
1071-
( <S> )*
1072-
selector()
1073-
( <S> )*
1074-
)*
10751069
}
10761070

10771071
void relativeSelectorList() #void : {}
10781072
{
1079-
( relativeSelector() )*
1073+
( <S> )*
1074+
( relativeSelector()
1075+
( <S> )*
1076+
( <COMMA>
1077+
( <S> )*
1078+
relativeSelector ()
1079+
( <S> )*
1080+
)*
1081+
)
10801082
}
10811083

10821084
void pseudoHas() #has : {}
@@ -1085,16 +1087,26 @@ void pseudoHas() #has : {}
10851087
relativeSelectorList()
10861088
}
10871089

1088-
void pseudoIs() #is : {}
1090+
void simpleSelectorList() #void : {}
10891091
{
10901092
( <S> )*
1091-
relativeSelectorList()
1093+
( selector ()
1094+
( <S> )*
1095+
( <COMMA>
1096+
( <S> )*
1097+
selector ()
1098+
( <S> )*
1099+
)*
1100+
)
1101+
}
1102+
void pseudoIs() #is : {}
1103+
{
1104+
simpleSelectorList ()
10921105
}
10931106

10941107
void pseudoWhere() #where : {}
10951108
{
1096-
( <S> )*
1097-
relativeSelectorList()
1109+
simpleSelectorList ()
10981110
}
10991111

11001112
void pseudoClassSelector() : {}

0 commit comments

Comments
 (0)