Ejemplo n.º 1
0
        /// <summary><inheritDoc/></summary>
        public virtual bool IsValid(String objectString)
        {
            if (objectString == null)
            {
                return(false);
            }
            if (CssUtils.IsInitialOrInheritOrUnset(objectString))
            {
                return(true);
            }
            // Actually it's not shorthand but extractShorthandProperties method works exactly as needed in this case
            IList <IList <String> > extractedProperties = CssUtils.ExtractShorthandProperties(objectString);

            foreach (IList <String> propertyValues in extractedProperties)
            {
                if (propertyValues.IsEmpty() || propertyValues.Count > MAX_AMOUNT_OF_VALUES)
                {
                    return(false);
                }
                for (int i = 0; i < propertyValues.Count; i++)
                {
                    if (!IsValidProperty(propertyValues, i))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
        // With CSS3, you can apply multiple backgrounds to elements. These are layered atop one another
        // with the first background you provide on top and the last background listed in the back. Only
        // the last background can include a background color.

        /* (non-Javadoc)
         * @see com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver#resolveShorthand(java.lang.String)
         */
        public virtual IList <CssDeclaration> ResolveShorthand(String shorthandExpression)
        {
            if (CssUtils.IsInitialOrInheritOrUnset(shorthandExpression))
            {
                return(JavaUtil.ArraysAsList(new CssDeclaration(CommonCssConstants.BACKGROUND_COLOR, shorthandExpression),
                                             new CssDeclaration(CommonCssConstants.BACKGROUND_IMAGE, shorthandExpression), new CssDeclaration(CommonCssConstants
                                                                                                                                              .BACKGROUND_POSITION, shorthandExpression), new CssDeclaration(CommonCssConstants.BACKGROUND_SIZE, shorthandExpression
                                                                                                                                                                                                             ), new CssDeclaration(CommonCssConstants.BACKGROUND_REPEAT, shorthandExpression), new CssDeclaration(CommonCssConstants
                                                                                                                                                                                                                                                                                                                  .BACKGROUND_ORIGIN, shorthandExpression), new CssDeclaration(CommonCssConstants.BACKGROUND_CLIP, shorthandExpression
                                                                                                                                                                                                                                                                                                                                                                               ), new CssDeclaration(CommonCssConstants.BACKGROUND_ATTACHMENT, shorthandExpression)));
            }
            if (String.IsNullOrEmpty(shorthandExpression.Trim()))
            {
                LOGGER.Error(MessageFormatUtil.Format(iText.StyledXmlParser.LogMessageConstant.SHORTHAND_PROPERTY_CANNOT_BE_EMPTY
                                                      , CommonCssConstants.BACKGROUND));
                return(new List <CssDeclaration>());
            }
            IList <IList <String> > propsList = CssUtils.ExtractShorthandProperties(shorthandExpression);
            IDictionary <CssBackgroundUtils.BackgroundPropertyType, String> resolvedProps = new Dictionary <CssBackgroundUtils.BackgroundPropertyType
                                                                                                            , String>();

            FillMapWithPropertiesTypes(resolvedProps);
            foreach (IList <String> props in propsList)
            {
                if (!ProcessProperties(props, resolvedProps))
                {
                    return(new List <CssDeclaration>());
                }
            }
            if (resolvedProps.Get(CssBackgroundUtils.BackgroundPropertyType.BACKGROUND_COLOR) == null)
            {
                resolvedProps.Put(CssBackgroundUtils.BackgroundPropertyType.BACKGROUND_COLOR, CommonCssConstants.TRANSPARENT
                                  );
            }
            if (!CheckProperties(resolvedProps))
            {
                return(new List <CssDeclaration>());
            }
            return(JavaUtil.ArraysAsList(new CssDeclaration(CssBackgroundUtils.GetBackgroundPropertyNameFromType(CssBackgroundUtils.BackgroundPropertyType
                                                                                                                 .BACKGROUND_COLOR), resolvedProps.Get(CssBackgroundUtils.BackgroundPropertyType.BACKGROUND_COLOR)), new
                                         CssDeclaration(CssBackgroundUtils.GetBackgroundPropertyNameFromType(CssBackgroundUtils.BackgroundPropertyType
                                                                                                             .BACKGROUND_IMAGE), resolvedProps.Get(CssBackgroundUtils.BackgroundPropertyType.BACKGROUND_IMAGE)), new
                                         CssDeclaration(CssBackgroundUtils.GetBackgroundPropertyNameFromType(CssBackgroundUtils.BackgroundPropertyType
                                                                                                             .BACKGROUND_POSITION), resolvedProps.Get(CssBackgroundUtils.BackgroundPropertyType.BACKGROUND_POSITION
                                                                                                                                                      )), new CssDeclaration(CssBackgroundUtils.GetBackgroundPropertyNameFromType(CssBackgroundUtils.BackgroundPropertyType
                                                                                                                                                                                                                                  .BACKGROUND_SIZE), resolvedProps.Get(CssBackgroundUtils.BackgroundPropertyType.BACKGROUND_SIZE)), new
                                         CssDeclaration(CssBackgroundUtils.GetBackgroundPropertyNameFromType(CssBackgroundUtils.BackgroundPropertyType
                                                                                                             .BACKGROUND_REPEAT), resolvedProps.Get(CssBackgroundUtils.BackgroundPropertyType.BACKGROUND_REPEAT)),
                                         new CssDeclaration(CssBackgroundUtils.GetBackgroundPropertyNameFromType(CssBackgroundUtils.BackgroundPropertyType
                                                                                                                 .BACKGROUND_ORIGIN), resolvedProps.Get(CssBackgroundUtils.BackgroundPropertyType.BACKGROUND_ORIGIN)),
                                         new CssDeclaration(CssBackgroundUtils.GetBackgroundPropertyNameFromType(CssBackgroundUtils.BackgroundPropertyType
                                                                                                                 .BACKGROUND_CLIP), resolvedProps.Get(CssBackgroundUtils.BackgroundPropertyType.BACKGROUND_CLIP)), new
                                         CssDeclaration(CssBackgroundUtils.GetBackgroundPropertyNameFromType(CssBackgroundUtils.BackgroundPropertyType
                                                                                                             .BACKGROUND_ATTACHMENT), resolvedProps.Get(CssBackgroundUtils.BackgroundPropertyType.BACKGROUND_ATTACHMENT
                                                                                                                                                        ))));
        }
Ejemplo n.º 3
0
        // With CSS3, you can apply multiple backgrounds to elements. These are layered atop one another
        // with the first background you provide on top and the last background listed in the back. Only
        // the last background can include a background color.

        /* (non-Javadoc)
         * @see com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver#resolveShorthand(java.lang.String)
         */
        public virtual IList <CssDeclaration> ResolveShorthand(String shorthandExpression)
        {
            if (CommonCssConstants.INITIAL.Equals(shorthandExpression) || CommonCssConstants.INHERIT.Equals(shorthandExpression
                                                                                                            ))
            {
                return(JavaUtil.ArraysAsList(new CssDeclaration(CommonCssConstants.BACKGROUND_COLOR, shorthandExpression),
                                             new CssDeclaration(CommonCssConstants.BACKGROUND_IMAGE, shorthandExpression), new CssDeclaration(CommonCssConstants
                                                                                                                                              .BACKGROUND_POSITION, shorthandExpression), new CssDeclaration(CommonCssConstants.BACKGROUND_SIZE, shorthandExpression
                                                                                                                                                                                                             ), new CssDeclaration(CommonCssConstants.BACKGROUND_REPEAT, shorthandExpression), new CssDeclaration(CommonCssConstants
                                                                                                                                                                                                                                                                                                                  .BACKGROUND_ORIGIN, shorthandExpression), new CssDeclaration(CommonCssConstants.BACKGROUND_CLIP, shorthandExpression
                                                                                                                                                                                                                                                                                                                                                                               ), new CssDeclaration(CommonCssConstants.BACKGROUND_ATTACHMENT, shorthandExpression)));
            }
            // TODO: DEVSIX-2027 ignore multiple backgrounds at the moment (stop parsing after comma)
            IList <String> props = CssUtils.ExtractShorthandProperties(shorthandExpression)[0];

            String[] resolvedProps    = new String[8];
            bool     slashEncountered = false;

            foreach (String value in props)
            {
                int slashCharInd = value.IndexOf('/');
                if (slashCharInd > 0 && !value.Contains("url("))
                {
                    slashEncountered = true;
                    String value1 = value.JSubstring(0, slashCharInd);
                    String value2 = value.JSubstring(slashCharInd + 1, value.Length);
                    PutPropertyBasedOnType(ResolvePropertyType(value1), value1, resolvedProps, false);
                    PutPropertyBasedOnType(ResolvePropertyType(value2), value2, resolvedProps, true);
                }
                else
                {
                    PutPropertyBasedOnType(ResolvePropertyType(value), value, resolvedProps, slashEncountered);
                }
            }
            for (int i = 0; i < resolvedProps.Length; ++i)
            {
                if (resolvedProps[i] == null)
                {
                    resolvedProps[i] = CommonCssConstants.INITIAL;
                }
            }
            IList <CssDeclaration> cssDeclarations = JavaUtil.ArraysAsList(new CssDeclaration(CommonCssConstants.BACKGROUND_COLOR
                                                                                              , resolvedProps[BACKGROUND_COLOR_TYPE]), new CssDeclaration(CommonCssConstants.BACKGROUND_IMAGE, resolvedProps
                                                                                                                                                          [BACKGROUND_IMAGE_TYPE]), new CssDeclaration(CommonCssConstants.BACKGROUND_POSITION, resolvedProps[BACKGROUND_POSITION_TYPE
                                                                                                                                                                                                       ]), new CssDeclaration(CommonCssConstants.BACKGROUND_SIZE, resolvedProps[BACKGROUND_POSITION_OR_SIZE_TYPE
                                                                                                                                                                                                                              ]), new CssDeclaration(CommonCssConstants.BACKGROUND_REPEAT, resolvedProps[BACKGROUND_REPEAT_TYPE]), new
                                                                           CssDeclaration(CommonCssConstants.BACKGROUND_ORIGIN, resolvedProps[BACKGROUND_ORIGIN_OR_CLIP_TYPE]), new
                                                                           CssDeclaration(CommonCssConstants.BACKGROUND_CLIP, resolvedProps[BACKGROUND_CLIP_TYPE]), new CssDeclaration
                                                                               (CommonCssConstants.BACKGROUND_ATTACHMENT, resolvedProps[BACKGROUND_ATTACHMENT_TYPE]));

            return(cssDeclarations);
        }
        /* (non-Javadoc)
         * @see com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver#resolveShorthand(java.lang.String)
         */
        public virtual IList <CssDeclaration> ResolveShorthand(String shorthandExpression)
        {
            if (CommonCssConstants.INITIAL.Equals(shorthandExpression) || CommonCssConstants.INHERIT.Equals(shorthandExpression
                                                                                                            ))
            {
                return(JavaUtil.ArraysAsList(new CssDeclaration(CommonCssConstants.LIST_STYLE_TYPE, shorthandExpression),
                                             new CssDeclaration(CommonCssConstants.LIST_STYLE_POSITION, shorthandExpression), new CssDeclaration(CommonCssConstants
                                                                                                                                                 .LIST_STYLE_IMAGE, shorthandExpression)));
            }
            IList <String> props = CssUtils.ExtractShorthandProperties(shorthandExpression)[0];
            String         listStyleTypeValue     = null;
            String         listStylePositionValue = null;
            String         listStyleImageValue    = null;

            foreach (String value in props)
            {
                if (value.Contains("url(") || CssGradientUtil.IsCssLinearGradientValue(value) || (CommonCssConstants.NONE.
                                                                                                  Equals(value) && listStyleTypeValue != null))
                {
                    listStyleImageValue = value;
                }
                else
                {
                    if (LIST_STYLE_TYPE_VALUES.Contains(value))
                    {
                        listStyleTypeValue = value;
                    }
                    else
                    {
                        if (LIST_STYLE_POSITION_VALUES.Contains(value))
                        {
                            listStylePositionValue = value;
                        }
                    }
                }
            }
            IList <CssDeclaration> resolvedDecl = new List <CssDeclaration>();

            resolvedDecl.Add(new CssDeclaration(CommonCssConstants.LIST_STYLE_TYPE, listStyleTypeValue == null ? CommonCssConstants
                                                .INITIAL : listStyleTypeValue));
            resolvedDecl.Add(new CssDeclaration(CommonCssConstants.LIST_STYLE_POSITION, listStylePositionValue == null
                 ? CommonCssConstants.INITIAL : listStylePositionValue));
            resolvedDecl.Add(new CssDeclaration(CommonCssConstants.LIST_STYLE_IMAGE, listStyleImageValue == null ? CommonCssConstants
                                                .INITIAL : listStyleImageValue));
            return(resolvedDecl);
        }
Ejemplo n.º 5
0
        /// <summary><inheritDoc/></summary>
        public virtual IList <CssDeclaration> ResolveShorthand(String shorthandExpression)
        {
            if (CssUtils.IsInitialOrInheritOrUnset(shorthandExpression))
            {
                return(JavaUtil.ArraysAsList(new CssDeclaration(CommonCssConstants.BACKGROUND_POSITION_X, shorthandExpression
                                                                ), new CssDeclaration(CommonCssConstants.BACKGROUND_POSITION_Y, shorthandExpression)));
            }
            if (String.IsNullOrEmpty(shorthandExpression.Trim()))
            {
                LOGGER.Error(MessageFormatUtil.Format(iText.StyledXmlParser.LogMessageConstant.SHORTHAND_PROPERTY_CANNOT_BE_EMPTY
                                                      , CommonCssConstants.BACKGROUND_POSITION));
                return(new List <CssDeclaration>());
            }
            IList <IList <String> >      propsList     = CssUtils.ExtractShorthandProperties(shorthandExpression);
            IDictionary <String, String> resolvedProps = new Dictionary <String, String>();
            IDictionary <String, String> values        = new Dictionary <String, String>();

            foreach (IList <String> props in propsList)
            {
                if (props.IsEmpty())
                {
                    LOGGER.Error(MessageFormatUtil.Format(iText.StyledXmlParser.LogMessageConstant.SHORTHAND_PROPERTY_CANNOT_BE_EMPTY
                                                          , CommonCssConstants.BACKGROUND_POSITION));
                    return(new List <CssDeclaration>());
                }
                if (!ParsePositionShorthand(props, values))
                {
                    LOGGER.Error(MessageFormatUtil.Format(iText.StyledXmlParser.LogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION
                                                          , shorthandExpression));
                    return(new List <CssDeclaration>());
                }
                UpdateValue(resolvedProps, values, CommonCssConstants.BACKGROUND_POSITION_X);
                UpdateValue(resolvedProps, values, CommonCssConstants.BACKGROUND_POSITION_Y);
                values.Clear();
            }
            if (!CheckProperty(resolvedProps, CommonCssConstants.BACKGROUND_POSITION_X) || !CheckProperty(resolvedProps
                                                                                                          , CommonCssConstants.BACKGROUND_POSITION_Y))
            {
                return(new List <CssDeclaration>());
            }
            return(JavaUtil.ArraysAsList(new CssDeclaration(CommonCssConstants.BACKGROUND_POSITION_X, resolvedProps.Get
                                                                (CommonCssConstants.BACKGROUND_POSITION_X)), new CssDeclaration(CommonCssConstants.BACKGROUND_POSITION_Y
                                                                                                                                , resolvedProps.Get(CommonCssConstants.BACKGROUND_POSITION_Y))));
        }
        /// <summary>Applies background to an element.</summary>
        /// <param name="cssProps">the CSS properties</param>
        /// <param name="context">the processor context</param>
        /// <param name="element">the element</param>
        public static void ApplyBackground(IDictionary <String, String> cssProps, ProcessorContext context, IPropertyContainer
                                           element)
        {
            String                  backgroundColorStr     = cssProps.Get(CssConstants.BACKGROUND_COLOR);
            String                  backgroundImagesStr    = cssProps.Get(CssConstants.BACKGROUND_IMAGE);
            String                  backgroundRepeatStr    = cssProps.Get(CssConstants.BACKGROUND_REPEAT);
            String                  backgroundSizeStr      = cssProps.Get(CssConstants.BACKGROUND_SIZE);
            String                  backgroundPositionXStr = cssProps.Get(CssConstants.BACKGROUND_POSITION_X);
            String                  backgroundPositionYStr = cssProps.Get(CssConstants.BACKGROUND_POSITION_Y);
            String                  backgroundBlendModeStr = cssProps.Get(CssConstants.BACKGROUND_BLEND_MODE);
            String                  backgroundClipStr      = cssProps.Get(CssConstants.BACKGROUND_CLIP);
            String                  backgroundOriginStr    = cssProps.Get(CssConstants.BACKGROUND_ORIGIN);
            IList <String>          backgroundImagesArray  = CssUtils.SplitStringWithComma(backgroundImagesStr);
            IList <String>          backgroundRepeatArray  = CssUtils.SplitStringWithComma(backgroundRepeatStr);
            IList <IList <String> > backgroundSizeArray    = backgroundSizeStr == null ? null : CssUtils.ExtractShorthandProperties
                                                                 (backgroundSizeStr);
            IList <String> backgroundPositionXArray = CssUtils.SplitStringWithComma(backgroundPositionXStr);
            IList <String> backgroundPositionYArray = CssUtils.SplitStringWithComma(backgroundPositionYStr);
            IList <String> backgroundBlendModeArray = CssUtils.SplitStringWithComma(backgroundBlendModeStr);
            String         fontSize              = cssProps.Get(CssConstants.FONT_SIZE);
            float          em                    = fontSize == null ? 0 : CssDimensionParsingUtils.ParseAbsoluteLength(fontSize);
            float          rem                   = context.GetCssContext().GetRootFontSize();
            IList <String> backgroundClipArray   = CssUtils.SplitStringWithComma(backgroundClipStr);
            IList <String> backgroundOriginArray = CssUtils.SplitStringWithComma(backgroundOriginStr);
            BackgroundBox  clipForColor          = GetBackgroundBoxProperty(backgroundClipArray, backgroundImagesArray.IsEmpty()
                 ? 0 : (backgroundImagesArray.Count - 1), BackgroundBox.BORDER_BOX);

            ApplyBackgroundColor(backgroundColorStr, element, clipForColor);
            IList <BackgroundImage> backgroundImagesList = GetBackgroundImagesList(backgroundImagesArray, context, em,
                                                                                   rem, backgroundPositionXArray, backgroundPositionYArray, backgroundSizeArray, backgroundBlendModeArray
                                                                                   , backgroundRepeatArray, backgroundClipArray, backgroundOriginArray);

            if (!backgroundImagesList.IsEmpty())
            {
                element.SetProperty(Property.BACKGROUND_IMAGE, backgroundImagesList);
            }
        }