Search in sources :

Example 6 with CSSDeclaration

use of com.helger.css.decl.CSSDeclaration in project ph-css by phax.

the class CSSShortHandDescriptor method getSplitIntoPieces.

@Nonnull
@ReturnsMutableCopy
public ICommonsList<CSSDeclaration> getSplitIntoPieces(@Nonnull final CSSDeclaration aDeclaration) {
    ValueEnforcer.notNull(aDeclaration, "Declaration");
    // Check that declaration matches this property
    if (!aDeclaration.hasProperty(m_eProperty))
        throw new IllegalArgumentException("Cannot split a '" + aDeclaration.getProperty() + "' as a '" + m_eProperty.getName() + "'");
    // global
    final int nSubProperties = m_aSubProperties.size();
    final ICommonsList<CSSDeclaration> ret = new CommonsArrayList<>();
    final ICommonsList<ICSSExpressionMember> aExpressionMembers = aDeclaration.getExpression().getAllMembers();
    // Modification for margin and padding
    modifyExpressionMembers(aExpressionMembers);
    final int nExpressionMembers = aExpressionMembers.size();
    final CSSWriterSettings aCWS = new CSSWriterSettings(ECSSVersion.CSS30, false);
    final boolean[] aHandledSubProperties = new boolean[nSubProperties];
    // For all expression members
    for (int nExprMemberIndex = 0; nExprMemberIndex < nExpressionMembers; ++nExprMemberIndex) {
        final ICSSExpressionMember aMember = aExpressionMembers.get(nExprMemberIndex);
        // For all unhandled sub-properties
        for (int nSubPropIndex = 0; nSubPropIndex < nSubProperties; ++nSubPropIndex) if (!aHandledSubProperties[nSubPropIndex]) {
            final CSSPropertyWithDefaultValue aSubProp = m_aSubProperties.get(nSubPropIndex);
            final ICSSProperty aProperty = aSubProp.getProperty();
            final int nMinArgs = aProperty.getMinimumArgumentCount();
            // Always use minimum number of arguments
            if (nExprMemberIndex + nMinArgs - 1 < nExpressionMembers) {
                // Build sum of all members
                final StringBuilder aSB = new StringBuilder();
                for (int k = 0; k < nMinArgs; ++k) {
                    final String sValue = aMember.getAsCSSString(aCWS);
                    if (aSB.length() > 0)
                        aSB.append(' ');
                    aSB.append(sValue);
                }
                if (aProperty.isValidValue(aSB.toString())) {
                    // We found a match
                    final CSSExpression aExpr = new CSSExpression();
                    for (int k = 0; k < nMinArgs; ++k) aExpr.addMember(aExpressionMembers.get(nExprMemberIndex + k));
                    ret.add(new CSSDeclaration(aSubProp.getProperty().getPropertyName(), aExpr));
                    nExprMemberIndex += nMinArgs - 1;
                    // Remember as handled
                    aHandledSubProperties[nSubPropIndex] = true;
                    // Next expression member
                    break;
                }
            }
        }
    }
    // Assign all default values that are not present
    for (int nSubPropIndex = 0; nSubPropIndex < nSubProperties; ++nSubPropIndex) if (!aHandledSubProperties[nSubPropIndex]) {
        final CSSPropertyWithDefaultValue aSubProp = m_aSubProperties.get(nSubPropIndex);
        // assign default value
        final CSSExpression aExpr = new CSSExpression();
        aExpr.addMember(new CSSExpressionMemberTermSimple(aSubProp.getDefaultValue()));
        ret.add(new CSSDeclaration(aSubProp.getProperty().getPropertyName(), aExpr));
    }
    return ret;
}
Also used : ICSSProperty(com.helger.css.property.ICSSProperty) CSSExpressionMemberTermSimple(com.helger.css.decl.CSSExpressionMemberTermSimple) ICSSExpressionMember(com.helger.css.decl.ICSSExpressionMember) CSSExpression(com.helger.css.decl.CSSExpression) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) CSSDeclaration(com.helger.css.decl.CSSDeclaration) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Example 7 with CSSDeclaration

use of com.helger.css.decl.CSSDeclaration in project ph-css by phax.

the class CSSShortHandDescriptorTest method testMargin1.

@Test
public void testMargin1() {
    final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor(ECSSProperty.MARGIN);
    assertNotNull(aSHD);
    final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString("margin:1px", ECSSVersion.CSS30).getDeclarationAtIndex(0);
    assertNotNull(aDecl);
    final List<CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces(aDecl);
    assertNotNull(aSplittedDecls);
    assertEquals(4, aSplittedDecls.size());
    assertEquals("margin-top:1px", aSplittedDecls.get(0).getAsCSSString(CWS));
    assertEquals("margin-right:1px", aSplittedDecls.get(1).getAsCSSString(CWS));
    assertEquals("margin-bottom:1px", aSplittedDecls.get(2).getAsCSSString(CWS));
    assertEquals("margin-left:1px", aSplittedDecls.get(3).getAsCSSString(CWS));
}
Also used : CSSDeclaration(com.helger.css.decl.CSSDeclaration) Test(org.junit.Test)

Example 8 with CSSDeclaration

use of com.helger.css.decl.CSSDeclaration in project ph-css by phax.

the class CSSShortHandDescriptorTest method testPadding1.

@Test
public void testPadding1() {
    final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor(ECSSProperty.PADDING);
    assertNotNull(aSHD);
    final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString("padding:1px", ECSSVersion.CSS30).getDeclarationAtIndex(0);
    assertNotNull(aDecl);
    final List<CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces(aDecl);
    assertNotNull(aSplittedDecls);
    assertEquals(4, aSplittedDecls.size());
    assertEquals("padding-top:1px", aSplittedDecls.get(0).getAsCSSString(CWS));
    assertEquals("padding-right:1px", aSplittedDecls.get(1).getAsCSSString(CWS));
    assertEquals("padding-bottom:1px", aSplittedDecls.get(2).getAsCSSString(CWS));
    assertEquals("padding-left:1px", aSplittedDecls.get(3).getAsCSSString(CWS));
}
Also used : CSSDeclaration(com.helger.css.decl.CSSDeclaration) Test(org.junit.Test)

Example 9 with CSSDeclaration

use of com.helger.css.decl.CSSDeclaration in project ph-css by phax.

the class CSSShortHandDescriptorTest method testPadding3.

@Test
public void testPadding3() {
    final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor(ECSSProperty.PADDING);
    assertNotNull(aSHD);
    final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString("padding:1px 3px 5px", ECSSVersion.CSS30).getDeclarationAtIndex(0);
    assertNotNull(aDecl);
    final List<CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces(aDecl);
    assertNotNull(aSplittedDecls);
    assertEquals(4, aSplittedDecls.size());
    assertEquals("padding-top:1px", aSplittedDecls.get(0).getAsCSSString(CWS));
    assertEquals("padding-right:3px", aSplittedDecls.get(1).getAsCSSString(CWS));
    assertEquals("padding-bottom:5px", aSplittedDecls.get(2).getAsCSSString(CWS));
    assertEquals("padding-left:3px", aSplittedDecls.get(3).getAsCSSString(CWS));
}
Also used : CSSDeclaration(com.helger.css.decl.CSSDeclaration) Test(org.junit.Test)

Example 10 with CSSDeclaration

use of com.helger.css.decl.CSSDeclaration in project ph-css by phax.

the class CSSShortHandDescriptorTest method testPadding4.

@Test
public void testPadding4() {
    final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor(ECSSProperty.PADDING);
    assertNotNull(aSHD);
    final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString("padding:1px 3px 5px 7px", ECSSVersion.CSS30).getDeclarationAtIndex(0);
    assertNotNull(aDecl);
    final List<CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces(aDecl);
    assertNotNull(aSplittedDecls);
    assertEquals(4, aSplittedDecls.size());
    assertEquals("padding-top:1px", aSplittedDecls.get(0).getAsCSSString(CWS));
    assertEquals("padding-right:3px", aSplittedDecls.get(1).getAsCSSString(CWS));
    assertEquals("padding-bottom:5px", aSplittedDecls.get(2).getAsCSSString(CWS));
    assertEquals("padding-left:7px", aSplittedDecls.get(3).getAsCSSString(CWS));
}
Also used : CSSDeclaration(com.helger.css.decl.CSSDeclaration) Test(org.junit.Test)

Aggregations

CSSDeclaration (com.helger.css.decl.CSSDeclaration)26 Test (org.junit.Test)19 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)6 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)6 CSSDeclarationList (com.helger.css.decl.CSSDeclarationList)5 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)5 CSSImportRule (com.helger.css.decl.CSSImportRule)4 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)4 CSSExpressionMemberTermSimple (com.helger.css.decl.CSSExpressionMemberTermSimple)3 ICSSExpressionMember (com.helger.css.decl.ICSSExpressionMember)3 DefaultCSSUrlVisitor (com.helger.css.decl.visit.DefaultCSSUrlVisitor)3 CSSWriter (com.helger.css.writer.CSSWriter)3 Charset (java.nio.charset.Charset)3 ECSSVersion (com.helger.css.ECSSVersion)2 CSSExpressionMemberFunction (com.helger.css.decl.CSSExpressionMemberFunction)2 CSSExpressionMemberMath (com.helger.css.decl.CSSExpressionMemberMath)2 CSSStyleRule (com.helger.css.decl.CSSStyleRule)2 File (java.io.File)2 Nonnull (javax.annotation.Nonnull)2 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1