Search in sources :

Example 1 with CSSExpressionMemberTermSimple

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

the class CSSReader30SpecialFuncTest method testReadExpressions.

@Test
public void testReadExpressions() {
    final ECSSVersion eVersion = ECSSVersion.CSS30;
    final CSSWriterSettings aCSSWS = new CSSWriterSettings(eVersion, false);
    final Charset aCharset = StandardCharsets.UTF_8;
    final File aFile = new File("src/test/resources/testfiles/css30/good/artificial/test-expression.css");
    final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, aCharset, eVersion);
    assertNotNull(aCSS);
    assertEquals(1, aCSS.getRuleCount());
    assertEquals(1, aCSS.getStyleRuleCount());
    final CSSStyleRule aSR = aCSS.getStyleRuleAtIndex(0);
    assertEquals("div", aSR.getSelectorsAsCSSString(aCSSWS, 0));
    assertEquals(23, aSR.getDeclarationCount());
    int i = 0;
    for (final CSSDeclaration aDecl : aSR.getAllDeclarations()) {
        final String sExpectedName = Character.toString((char) ('a' + i));
        assertEquals(sExpectedName, aDecl.getProperty());
        ++i;
    }
    CSSDeclaration aDecl;
    ICSSExpressionMember aMember;
    // a: -5
    aDecl = aSR.getDeclarationOfPropertyName("a");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5", aMember.getAsCSSString(aCSSWS));
    // b: +5
    aDecl = aSR.getDeclarationOfPropertyName("b");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5", aMember.getAsCSSString(aCSSWS));
    // c: 5
    aDecl = aSR.getDeclarationOfPropertyName("c");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5", aMember.getAsCSSString(aCSSWS));
    // d: -5.12
    aDecl = aSR.getDeclarationOfPropertyName("d");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5.12", aMember.getAsCSSString(aCSSWS));
    // e: +5.12
    aDecl = aSR.getDeclarationOfPropertyName("e");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5.12", aMember.getAsCSSString(aCSSWS));
    // f: 5.12
    aDecl = aSR.getDeclarationOfPropertyName("f");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5.12", aMember.getAsCSSString(aCSSWS));
    // g: -5.12%
    aDecl = aSR.getDeclarationOfPropertyName("g");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5.12%", aMember.getAsCSSString(aCSSWS));
    // h: +5.12%
    aDecl = aSR.getDeclarationOfPropertyName("h");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5.12%", aMember.getAsCSSString(aCSSWS));
    // i: 5.12%
    aDecl = aSR.getDeclarationOfPropertyName("i");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5.12%", aMember.getAsCSSString(aCSSWS));
    // j: -5px
    aDecl = aSR.getDeclarationOfPropertyName("j");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5px", aMember.getAsCSSString(aCSSWS));
    // k: +5px
    aDecl = aSR.getDeclarationOfPropertyName("k");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5px", aMember.getAsCSSString(aCSSWS));
    // l: 5px
    aDecl = aSR.getDeclarationOfPropertyName("l");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5px", aMember.getAsCSSString(aCSSWS));
    // m: 'string1'
    aDecl = aSR.getDeclarationOfPropertyName("m");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("'string1'", aMember.getAsCSSString(aCSSWS));
    // n: "string2"
    aDecl = aSR.getDeclarationOfPropertyName("n");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("\"string2\"", aMember.getAsCSSString(aCSSWS));
    // o: abc
    aDecl = aSR.getDeclarationOfPropertyName("o");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("abc", aMember.getAsCSSString(aCSSWS));
    // p: from
    aDecl = aSR.getDeclarationOfPropertyName("p");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("from", aMember.getAsCSSString(aCSSWS));
    // q: to
    aDecl = aSR.getDeclarationOfPropertyName("q");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("to", aMember.getAsCSSString(aCSSWS));
    // r: url(a.gif)
    aDecl = aSR.getDeclarationOfPropertyName("r");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermURI);
    assertEquals("url(a.gif)", aMember.getAsCSSString(aCSSWS));
    // s: #123
    aDecl = aSR.getDeclarationOfPropertyName("s");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("#123", aMember.getAsCSSString(aCSSWS));
    // t: function(5,6,abc)
    aDecl = aSR.getDeclarationOfPropertyName("t");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberFunction);
    assertEquals("function", ((CSSExpressionMemberFunction) aMember).getFunctionName());
    // 3 parameters and 2 commas
    assertEquals(5, ((CSSExpressionMemberFunction) aMember).getExpression().getMemberCount());
    assertEquals("function(5,6,abc)", aMember.getAsCSSString(aCSSWS));
    // u: calc(4 + 5)
    aDecl = aSR.getDeclarationOfPropertyName("u");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberMath);
    // 2 values and 1 operator
    assertEquals(3, ((CSSExpressionMemberMath) aMember).getMemberCount());
    assertEquals("calc(4 + 5)", aMember.getAsCSSString(aCSSWS));
    // v: inherit
    aDecl = aSR.getDeclarationOfPropertyName("v");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("inherit", aMember.getAsCSSString(aCSSWS));
    // w: u+1234
    aDecl = aSR.getDeclarationOfPropertyName("w");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("u+1234", aMember.getAsCSSString(aCSSWS));
    // Write result
    final String sCSS = new CSSWriter(aCSSWS).getCSSAsString(aCSS);
    assertNotNull(sCSS);
    if (false)
        s_aLogger.info(sCSS);
}
Also used : CSSExpressionMemberTermURI(com.helger.css.decl.CSSExpressionMemberTermURI) ECSSVersion(com.helger.css.ECSSVersion) Charset(java.nio.charset.Charset) CSSExpressionMemberTermSimple(com.helger.css.decl.CSSExpressionMemberTermSimple) CSSWriter(com.helger.css.writer.CSSWriter) ICSSExpressionMember(com.helger.css.decl.ICSSExpressionMember) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSStyleRule(com.helger.css.decl.CSSStyleRule) CSSExpressionMemberMath(com.helger.css.decl.CSSExpressionMemberMath) CSSDeclaration(com.helger.css.decl.CSSDeclaration) File(java.io.File) CSSExpressionMemberFunction(com.helger.css.decl.CSSExpressionMemberFunction) Test(org.junit.Test)

Example 2 with CSSExpressionMemberTermSimple

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

the class CSSReader30FuncTest method testReadExpressions.

@Test
public void testReadExpressions() {
    final ECSSVersion eVersion = ECSSVersion.CSS30;
    final CSSWriterSettings aCSSWS = new CSSWriterSettings(eVersion, false);
    final Charset aCharset = StandardCharsets.UTF_8;
    final File aFile = new File("src/test/resources/testfiles/css30/good/artificial/test-expression.css");
    final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, aCharset, eVersion);
    assertNotNull(aCSS);
    assertEquals(1, aCSS.getRuleCount());
    assertEquals(1, aCSS.getStyleRuleCount());
    final CSSStyleRule aSR = aCSS.getStyleRuleAtIndex(0);
    assertEquals("div", aSR.getSelectorsAsCSSString(aCSSWS, 0));
    assertEquals(23, aSR.getDeclarationCount());
    int i = 0;
    for (final CSSDeclaration aDecl : aSR.getAllDeclarations()) {
        final String sExpectedName = Character.toString((char) ('a' + i));
        assertEquals(sExpectedName, aDecl.getProperty());
        ++i;
    }
    CSSDeclaration aDecl;
    ICSSExpressionMember aMember;
    // a: -5
    aDecl = aSR.getDeclarationOfPropertyName("a");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5", aMember.getAsCSSString(aCSSWS));
    // b: +5
    aDecl = aSR.getDeclarationOfPropertyName("b");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5", aMember.getAsCSSString(aCSSWS));
    // c: 5
    aDecl = aSR.getDeclarationOfPropertyName("c");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5", aMember.getAsCSSString(aCSSWS));
    // d: -5.12
    aDecl = aSR.getDeclarationOfPropertyName("d");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5.12", aMember.getAsCSSString(aCSSWS));
    // e: +5.12
    aDecl = aSR.getDeclarationOfPropertyName("e");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5.12", aMember.getAsCSSString(aCSSWS));
    // f: 5.12
    aDecl = aSR.getDeclarationOfPropertyName("f");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5.12", aMember.getAsCSSString(aCSSWS));
    // g: -5.12%
    aDecl = aSR.getDeclarationOfPropertyName("g");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5.12%", aMember.getAsCSSString(aCSSWS));
    // h: +5.12%
    aDecl = aSR.getDeclarationOfPropertyName("h");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5.12%", aMember.getAsCSSString(aCSSWS));
    // i: 5.12%
    aDecl = aSR.getDeclarationOfPropertyName("i");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5.12%", aMember.getAsCSSString(aCSSWS));
    // j: -5px
    aDecl = aSR.getDeclarationOfPropertyName("j");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5px", aMember.getAsCSSString(aCSSWS));
    // k: +5px
    aDecl = aSR.getDeclarationOfPropertyName("k");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5px", aMember.getAsCSSString(aCSSWS));
    // l: 5px
    aDecl = aSR.getDeclarationOfPropertyName("l");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5px", aMember.getAsCSSString(aCSSWS));
    // m: 'string1'
    aDecl = aSR.getDeclarationOfPropertyName("m");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("'string1'", aMember.getAsCSSString(aCSSWS));
    // n: "string2"
    aDecl = aSR.getDeclarationOfPropertyName("n");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("\"string2\"", aMember.getAsCSSString(aCSSWS));
    // o: abc
    aDecl = aSR.getDeclarationOfPropertyName("o");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("abc", aMember.getAsCSSString(aCSSWS));
    // p: from
    aDecl = aSR.getDeclarationOfPropertyName("p");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("from", aMember.getAsCSSString(aCSSWS));
    // q: to
    aDecl = aSR.getDeclarationOfPropertyName("q");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("to", aMember.getAsCSSString(aCSSWS));
    // r: url(a.gif)
    aDecl = aSR.getDeclarationOfPropertyName("r");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermURI);
    assertEquals("url(a.gif)", aMember.getAsCSSString(aCSSWS));
    // s: #123
    aDecl = aSR.getDeclarationOfPropertyName("s");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("#123", aMember.getAsCSSString(aCSSWS));
    // t: function(5,6,abc)
    aDecl = aSR.getDeclarationOfPropertyName("t");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberFunction);
    assertEquals("function", ((CSSExpressionMemberFunction) aMember).getFunctionName());
    // 3 parameters and 2 commas
    assertEquals(5, ((CSSExpressionMemberFunction) aMember).getExpression().getMemberCount());
    assertEquals("function(5,6,abc)", aMember.getAsCSSString(aCSSWS));
    // u: calc(4 + 5)
    aDecl = aSR.getDeclarationOfPropertyName("u");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberMath);
    // 2 values and 1 operator
    assertEquals(3, ((CSSExpressionMemberMath) aMember).getMemberCount());
    assertEquals("calc(4 + 5)", aMember.getAsCSSString(aCSSWS));
    // v: inherit
    aDecl = aSR.getDeclarationOfPropertyName("v");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("inherit", aMember.getAsCSSString(aCSSWS));
    // w: u+1234
    aDecl = aSR.getDeclarationOfPropertyName("w");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("u+1234", aMember.getAsCSSString(aCSSWS));
    // Write result
    final String sCSS = new CSSWriter(aCSSWS).getCSSAsString(aCSS);
    assertNotNull(sCSS);
    if (false)
        m_aLogger.info(sCSS);
}
Also used : CSSExpressionMemberTermURI(com.helger.css.decl.CSSExpressionMemberTermURI) ECSSVersion(com.helger.css.ECSSVersion) Charset(java.nio.charset.Charset) CSSExpressionMemberTermSimple(com.helger.css.decl.CSSExpressionMemberTermSimple) CSSWriter(com.helger.css.writer.CSSWriter) ICSSExpressionMember(com.helger.css.decl.ICSSExpressionMember) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSStyleRule(com.helger.css.decl.CSSStyleRule) CSSExpressionMemberMath(com.helger.css.decl.CSSExpressionMemberMath) CSSDeclaration(com.helger.css.decl.CSSDeclaration) File(java.io.File) CSSExpressionMemberFunction(com.helger.css.decl.CSSExpressionMemberFunction) Test(org.junit.Test)

Example 3 with CSSExpressionMemberTermSimple

use of com.helger.css.decl.CSSExpressionMemberTermSimple 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)

Aggregations

CSSDeclaration (com.helger.css.decl.CSSDeclaration)3 CSSExpressionMemberTermSimple (com.helger.css.decl.CSSExpressionMemberTermSimple)3 ICSSExpressionMember (com.helger.css.decl.ICSSExpressionMember)3 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)3 ECSSVersion (com.helger.css.ECSSVersion)2 CSSExpressionMemberFunction (com.helger.css.decl.CSSExpressionMemberFunction)2 CSSExpressionMemberMath (com.helger.css.decl.CSSExpressionMemberMath)2 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)2 CSSStyleRule (com.helger.css.decl.CSSStyleRule)2 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)2 CSSWriter (com.helger.css.writer.CSSWriter)2 File (java.io.File)2 Charset (java.nio.charset.Charset)2 Test (org.junit.Test)2 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CSSExpression (com.helger.css.decl.CSSExpression)1 ICSSProperty (com.helger.css.property.ICSSProperty)1 Nonnull (javax.annotation.Nonnull)1