Search in sources :

Example 1 with CSSExpression

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

the class CSSVisitorForUrl method onDeclaration.

public void onDeclaration(@Nonnull final CSSDeclaration aDeclaration) {
    final ICSSTopLevelRule aTopLevelRule = m_aTopLevelRule.isEmpty() ? null : m_aTopLevelRule.peek();
    final CSSExpression aExpr = aDeclaration.getExpression();
    for (final ICSSExpressionMember aMember : aExpr.getAllMembers()) if (aMember instanceof CSSExpressionMemberTermURI) {
        final CSSExpressionMemberTermURI aExprTerm = (CSSExpressionMemberTermURI) aMember;
        m_aVisitor.onUrlDeclaration(aTopLevelRule, aDeclaration, aExprTerm);
    }
}
Also used : ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSExpressionMemberTermURI(com.helger.css.decl.CSSExpressionMemberTermURI) CSSExpression(com.helger.css.decl.CSSExpression) ICSSExpressionMember(com.helger.css.decl.ICSSExpressionMember)

Example 2 with CSSExpression

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

the class WikiCreateFontFaceRule method createFontFace.

/**
 * Create a single font-face rule.
 *
 * <pre>
 * &#64;font-face {
 *   font-family: "Your typeface";
 *   src: url("path/basename.eot");
 *   src: local("local font name"),
 *        url("path/basename.woff") format("woff"),
 *        url("path/basename.otf") format("opentype"),
 *        url("path/basename.svg#filename") format("svg");
 * }
 * </pre>
 *
 * @param sTypefaceName
 *        The name of the font-face in CSS. May neither be <code>null</code>
 *        nor empty.
 * @param sLocalName
 *        The name of the local font to be used. May be <code>null</code>.
 * @param sPath
 *        The server-relative path, where the font files reside. May not be
 *        <code>null</code>.
 * @param sBasename
 *        the base name of the font-files (without extension). May neither be
 *        <code>null</code> nor empty
 * @return The created {@link CascadingStyleSheet}.
 */
@Nonnull
public static CascadingStyleSheet createFontFace(@Nonnull @Nonempty final String sTypefaceName, @Nullable final String sLocalName, @Nonnull final String sPath, @Nonnull final String sBasename) {
    final CascadingStyleSheet aCSS = new CascadingStyleSheet();
    final CSSFontFaceRule aFFR = new CSSFontFaceRule();
    // The font-family
    aFFR.addDeclaration("font-family", CSSExpression.createString(sTypefaceName), false);
    // The special EOT file
    aFFR.addDeclaration("src", CSSExpression.createURI(sPath + sBasename + ".eot"), false);
    // The generic rules
    final CSSExpression aExpr = new CSSExpression();
    if (StringHelper.hasText(sLocalName))
        aExpr.addMember(new CSSExpressionMemberFunction("local", CSSExpression.createString(sLocalName))).addMember(ECSSExpressionOperator.COMMA);
    aExpr.addURI(sPath + sBasename + ".woff").addMember(_createFormatFct("woff")).addMember(ECSSExpressionOperator.COMMA).addURI(sPath + sBasename + ".otf").addMember(_createFormatFct("opentype")).addMember(ECSSExpressionOperator.COMMA).addURI(sPath + sBasename + ".svg#" + sBasename).addMember(_createFormatFct("svg"));
    aFFR.addDeclaration("src", aExpr, false);
    // Add the font-face rule to the main CSS
    aCSS.addRule(aFFR);
    return aCSS;
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSFontFaceRule(com.helger.css.decl.CSSFontFaceRule) CSSExpression(com.helger.css.decl.CSSExpression) CSSExpressionMemberFunction(com.helger.css.decl.CSSExpressionMemberFunction) Nonnull(javax.annotation.Nonnull)

Example 3 with CSSExpression

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

CSSExpression (com.helger.css.decl.CSSExpression)3 ICSSExpressionMember (com.helger.css.decl.ICSSExpressionMember)2 Nonnull (javax.annotation.Nonnull)2 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CSSDeclaration (com.helger.css.decl.CSSDeclaration)1 CSSExpressionMemberFunction (com.helger.css.decl.CSSExpressionMemberFunction)1 CSSExpressionMemberTermSimple (com.helger.css.decl.CSSExpressionMemberTermSimple)1 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)1 CSSFontFaceRule (com.helger.css.decl.CSSFontFaceRule)1 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)1 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)1 ICSSProperty (com.helger.css.property.ICSSProperty)1 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)1