Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly support selector lists #111

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ph-css/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.helger</groupId>
<groupId>unblu.patched.com.helger</groupId>
<artifactId>ph-css-parent-pom</artifactId>
<version>7.0.5-SNAPSHOT</version>
</parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.collection.impl.CommonsArrayList;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.equals.EqualsHelper;
import com.helger.commons.hashcode.HashCodeGenerator;
import com.helger.commons.state.EChange;
import com.helger.commons.string.ToStringGenerator;
Expand All @@ -46,40 +45,27 @@
@NotThreadSafe
public class CSSSelectorMemberPseudoHas implements ICSSSelectorMember, ICSSVersionAware, ICSSSourceLocationAware
{
private final ECSSSelectorCombinator m_eCombinator;
private final ICommonsList <CSSSelector> m_aNestedSelectors;
private CSSSourceLocation m_aSourceLocation;

public CSSSelectorMemberPseudoHas (@Nullable final ECSSSelectorCombinator eCombinator,
@Nonnull final CSSSelector aNestedSelector)
public CSSSelectorMemberPseudoHas (@Nonnull final CSSSelector aNestedSelector)
{
ValueEnforcer.notNull (aNestedSelector, "NestedSelector");
m_eCombinator = eCombinator;
m_aNestedSelectors = new CommonsArrayList <> (aNestedSelector);
}

public CSSSelectorMemberPseudoHas (@Nullable final ECSSSelectorCombinator eCombinator,
@Nonnull final CSSSelector... aNestedSelectors)
public CSSSelectorMemberPseudoHas (@Nonnull final CSSSelector... aNestedSelectors)
{
ValueEnforcer.notNull (aNestedSelectors, "NestedSelectors");
m_eCombinator = eCombinator;
m_aNestedSelectors = new CommonsArrayList <> (aNestedSelectors);
}

public CSSSelectorMemberPseudoHas (@Nullable final ECSSSelectorCombinator eCombinator,
@Nonnull final Iterable <CSSSelector> aNestedSelectors)
public CSSSelectorMemberPseudoHas (@Nonnull final Iterable <CSSSelector> aNestedSelectors)
{
ValueEnforcer.notNull (aNestedSelectors, "NestedSelectors");
m_eCombinator = eCombinator;
m_aNestedSelectors = new CommonsArrayList <> (aNestedSelectors);
}

@Nullable
public ECSSSelectorCombinator getCombinator ()
{
return m_eCombinator;
}

public boolean hasSelectors ()
{
return m_aNestedSelectors.isNotEmpty ();
Expand Down Expand Up @@ -177,10 +163,6 @@ public String getAsCSSString (@Nonnull final ICSSWriterSettings aSettings, @Nonn

final boolean bOptimizedOutput = aSettings.isOptimizedOutput ();
final StringBuilder aSB = new StringBuilder (":has(");

if (m_eCombinator != null)
aSB.append (m_eCombinator.getAsCSSString (aSettings));

boolean bFirst = true;
for (final CSSSelector aNestedSelector : m_aNestedSelectors)
{
Expand Down Expand Up @@ -218,20 +200,19 @@ public boolean equals (final Object o)
if (o == null || !getClass ().equals (o.getClass ()))
return false;
final CSSSelectorMemberPseudoHas rhs = (CSSSelectorMemberPseudoHas) o;
return EqualsHelper.equals (m_eCombinator, rhs.m_eCombinator) && m_aNestedSelectors.equals (rhs.m_aNestedSelectors);
return m_aNestedSelectors.equals (rhs.m_aNestedSelectors);
}

@Override
public int hashCode ()
{
return new HashCodeGenerator (this).append (m_eCombinator).append (m_aNestedSelectors).getHashCode ();
return new HashCodeGenerator (this).append (m_aNestedSelectors).getHashCode ();
}

@Override
public String toString ()
{
return new ToStringGenerator (null).append ("Combinator", m_eCombinator)
.append ("NestedSelectors", m_aNestedSelectors)
return new ToStringGenerator (null).append ("NestedSelectors", m_aNestedSelectors)
.appendIfNotNull ("SourceLocation", m_aSourceLocation)
.getToString ();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.collection.impl.CommonsArrayList;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.equals.EqualsHelper;
import com.helger.commons.hashcode.HashCodeGenerator;
import com.helger.commons.state.EChange;
import com.helger.commons.string.ToStringGenerator;
import com.helger.css.CSSSourceLocation;
import com.helger.css.ECSSVersion;
Expand All @@ -41,19 +46,112 @@
@NotThreadSafe
public class CSSSelectorMemberPseudoIs implements ICSSSelectorMember, ICSSVersionAware, ICSSSourceLocationAware
{
private final CSSSelector m_aSelector;
private final ICommonsList <CSSSelector> m_aNestedSelectors;
private CSSSourceLocation m_aSourceLocation;

public CSSSelectorMemberPseudoIs (@Nonnull final CSSSelector aSelector)
public CSSSelectorMemberPseudoIs (@Nonnull final CSSSelector aNestedSelector)
{
ValueEnforcer.notNull (aNestedSelector, "NestedSelector");
m_aNestedSelectors = new CommonsArrayList <> (aNestedSelector);
}

public CSSSelectorMemberPseudoIs (@Nonnull final CSSSelector... aNestedSelectors)
{
ValueEnforcer.notNull (aNestedSelectors, "NestedSelectors");
m_aNestedSelectors = new CommonsArrayList <> (aNestedSelectors);
}

public CSSSelectorMemberPseudoIs (@Nonnull final Iterable <CSSSelector> aNestedSelectors)
{
ValueEnforcer.notNull (aNestedSelectors, "NestedSelectors");
m_aNestedSelectors = new CommonsArrayList <> (aNestedSelectors);
}

public boolean hasSelectors ()
{
return m_aNestedSelectors.isNotEmpty ();
}

@Nonnegative
public int getSelectorCount ()
{
return m_aNestedSelectors.size ();
}

@Nonnull
public CSSSelectorMemberPseudoIs addSelector (@Nonnull final ICSSSelectorMember aSingleSelectorMember)
{
ValueEnforcer.notNull (aSingleSelectorMember, "SingleSelectorMember");

return addSelector (new CSSSelector ().addMember (aSingleSelectorMember));
}

@Nonnull
public CSSSelectorMemberPseudoIs addSelector (@Nonnull final CSSSelector aSelector)
{
ValueEnforcer.notNull (aSelector, "Selector");

m_aNestedSelectors.add (aSelector);
return this;
}

@Nonnull
public CSSSelectorMemberPseudoIs addSelector (@Nonnegative final int nIndex,
@Nonnull final ICSSSelectorMember aSingleSelectorMember)
{
ValueEnforcer.notNull (aSingleSelectorMember, "SingleSelectorMember");

return addSelector (nIndex, new CSSSelector ().addMember (aSingleSelectorMember));
}

@Nonnull
public CSSSelectorMemberPseudoIs addSelector (@Nonnegative final int nIndex, @Nonnull final CSSSelector aSelector)
{
ValueEnforcer.isGE0 (nIndex, "Index");
ValueEnforcer.notNull (aSelector, "Selector");
m_aSelector = aSelector;

if (nIndex >= getSelectorCount ())
m_aNestedSelectors.add (aSelector);
else
m_aNestedSelectors.add (nIndex, aSelector);
return this;
}

@Nonnull
public EChange removeSelector (@Nonnull final CSSSelector aSelector)
{
return m_aNestedSelectors.removeObject (aSelector);
}

@Nonnull
public EChange removeSelector (@Nonnegative final int nSelectorIndex)
{
return m_aNestedSelectors.removeAtIndex (nSelectorIndex);
}

/**
* Remove all selectors.
*
* @return {@link EChange#CHANGED} if any selector was removed,
* {@link EChange#UNCHANGED} otherwise. Never <code>null</code>.
*/
@Nonnull
public final CSSSelector getSelector ()
public EChange removeAllSelectors ()
{
return m_aSelector;
return m_aNestedSelectors.removeAll ();
}

@Nullable
public CSSSelector getSelectorAtIndex (@Nonnegative final int nSelectorIndex)
{
return m_aNestedSelectors.getAtIndex (nSelectorIndex);
}

@Nonnull
@ReturnsMutableCopy
public ICommonsList <CSSSelector> getAllSelectors ()
{
return m_aNestedSelectors.getClone ();
}

@Nonnull
Expand All @@ -62,8 +160,20 @@ public String getAsCSSString (@Nonnull final ICSSWriterSettings aSettings, @Nonn
{
aSettings.checkVersionRequirements (this);

aSettings.checkVersionRequirements (this);

final boolean bOptimizedOutput = aSettings.isOptimizedOutput ();
final StringBuilder aSB = new StringBuilder (":is(");
aSB.append (m_aSelector.getAsCSSString (aSettings, 0));

boolean bFirst = true;
for (final CSSSelector aNestedSelector : m_aNestedSelectors)
{
if (bFirst)
bFirst = false;
else
aSB.append (bOptimizedOutput ? "," : ", ");
aSB.append (aNestedSelector.getAsCSSString (aSettings, 0));
}
return aSB.append (')').toString ();
}

Expand Down Expand Up @@ -92,19 +202,19 @@ public boolean equals (final Object o)
if (o == null || !getClass ().equals (o.getClass ()))
return false;
final CSSSelectorMemberPseudoIs rhs = (CSSSelectorMemberPseudoIs) o;
return m_aSelector.equals (rhs.m_aSelector);
return m_aNestedSelectors.equals (rhs.m_aNestedSelectors);
}

@Override
public int hashCode ()
{
return new HashCodeGenerator (this).append (m_aSelector).getHashCode ();
return new HashCodeGenerator (this).append (m_aNestedSelectors).getHashCode ();
}

@Override
public String toString ()
{
return new ToStringGenerator (null).append ("Selector", m_aSelector)
return new ToStringGenerator (null).append ("NestedSelectors", m_aNestedSelectors)
.appendIfNotNull ("SourceLocation", m_aSourceLocation)
.getToString ();
}
Expand Down
Loading