示例#1
0
        public bool SetStyleValue(Style style, CSSStyleItemReader reader, PDFContextBase context)
        {
            IParserStyleFactory found;

            if (string.IsNullOrEmpty(reader.CurrentAttribute) && reader.ReadNextAttributeName() == false)
            {
                return(false);
            }

            string variable;

            if (IsVariableName(reader, out variable))
            {
                if (style is StyleDefn defn)
                {
                    var id = reader.CurrentAttribute;
                    if (reader.ReadNextValue(';', ignoreWhiteSpace: true))
                    {
                        defn.AddVariable(variable, reader.CurrentTextValue);
                    }
                }
                else if (null != context && context.TraceLog.ShouldLog(TraceLevel.Warning))
                {
                    context.TraceLog.Add(TraceLevel.Warning, "CSS", "Can only declare variables within style definitions '" + reader.CurrentAttribute + "' is not beinf set on a definition.");
                }
            }
            if (_knownStyles.TryGetValue(reader.CurrentAttribute, out found))
            {
                return(found.SetStyleValue(style, reader, context));
            }
            else
            {
                if (null != context && context.TraceLog.ShouldLog(TraceLevel.Warning))
                {
                    context.TraceLog.Add(TraceLevel.Warning, "CSS", "Could not set the style value on attribute '" + reader.CurrentAttribute + "' as it is not a known style attribute.");
                }

                return(false);
            }
        }
示例#2
0
        public bool SetStyleValue(IHtmlContentParser parser, IPDFStyledComponent component, CSSStyleItemReader reader)
        {
            IParserStyleFactory found;

            if (string.IsNullOrEmpty(reader.CurrentAttribute) && reader.ReadNextAttributeName() == false)
            {
                return(false);
            }

            if (_knownStyles.TryGetValue(reader.CurrentAttribute, out found))
            {
                return(found.SetStyleValue(parser, component, reader));
            }
            else
            {
                if (null != parser && parser.IsLogging)
                {
                    parser.Context.TraceLog.Add(TraceLevel.Warning, "CSS", "Could not set the style value on attribute '" + reader.CurrentAttribute + "' as it is not a known style attribute.");
                }
            }
            return(false);
        }
示例#3
0
        public bool SetStyleValue(PDFTraceLog log, Style style, CSSStyleItemReader reader)
        {
            IParserStyleFactory found;

            if (string.IsNullOrEmpty(reader.CurrentAttribute) && reader.ReadNextAttributeName() == false)
            {
                return(false);
            }

            if (_knownStyles.TryGetValue(reader.CurrentAttribute, out found))
            {
                return(found.SetStyleValue(log, style, reader));
            }
            else
            {
                if (null != log && log.ShouldLog(TraceLevel.Warning))
                {
                    log.Add(TraceLevel.Warning, "CSS", "Could not set the style value on attribute '" + reader.CurrentAttribute + "' as it is not a known style attribute.");
                }

                return(false);
            }
        }
        private StyleBase ParseNextStyle()
        {
            if (!this._str.MoveNext())
            {
                return(null);
            }

            var start = this._str.Offset;

            if (start >= this._str.Length)
            {
                return(null);
            }

            StyleBase parsed   = null;
            string    selector = "";

            try
            {
                var next = MoveToNextStyleStart();
                if (next < 0)
                {
                    this._str.Offset = this._str.Length;
                    return(null);
                }

                selector = this._str.Substring(start, next - start).Trim();

                if (this.IsMediaQuery(ref selector))
                {
                    if (this._log.ShouldLog(TraceLevel.Verbose))
                    {
                        this._log.Add(TraceLevel.Verbose, "CSS", "Found media query at rule for " + selector + " parsing inner contents");
                    }

                    var             match    = Selectors.MediaMatcher.Parse(selector);
                    StyleMediaGroup media    = new StyleMediaGroup(match);
                    var             innerEnd = MoveToGroupEnd();
                    if (innerEnd <= next)
                    {
                        return(null);
                    }

                    this.ParseInnerStyles(media, next + 1, innerEnd - (next + 1));
                    parsed           = media;
                    this._str.Offset = innerEnd;
                }
                else if (this.IsPageQuery(ref selector))
                {
                    if (this._log.ShouldLog(TraceLevel.Verbose))
                    {
                        this._log.Add(TraceLevel.Verbose, "CSS", "Found page at rule for " + selector + " parsing inner contents");
                    }

                    var            match    = Selectors.PageMatcher.Parse(selector);
                    StylePageGroup pg       = new StylePageGroup(match);
                    var            innerEnd = MoveToNextStyleEnd();
                    if (innerEnd <= next)
                    {
                        return(null);
                    }

                    string style = this._str.Substring(next + 1, innerEnd - (next + 1));

                    CSSStyleItemReader    reader = new CSSStyleItemReader(style);
                    CSSStyleItemAllParser parser = new CSSStyleItemAllParser();

                    while (reader.ReadNextAttributeName())
                    {
                        parser.SetStyleValue(this._log, pg, reader);
                    }


                    parsed           = pg;
                    this._str.Offset = innerEnd;
                }
                else if (this.IsFontFace(ref selector))
                {
                    if (this._log.ShouldLog(TraceLevel.Verbose))
                    {
                        this._log.Add(TraceLevel.Verbose, "CSS", "Found font-face at rule for " + selector + " parsing inner contents");
                    }

                    StyleFontFace ff       = new StyleFontFace();
                    var           innerEnd = MoveToNextStyleEnd();
                    if (innerEnd <= next)
                    {
                        return(null);
                    }

                    string                style  = this._str.Substring(next + 1, innerEnd - (next + 1));
                    CSSStyleItemReader    reader = new CSSStyleItemReader(style);
                    CSSStyleItemAllParser parser = new CSSStyleItemAllParser();

                    while (reader.ReadNextAttributeName())
                    {
                        parser.SetStyleValue(this._log, ff, reader);
                    }


                    parsed           = ff;
                    this._str.Offset = innerEnd;
                }
                else
                {
                    var end = MoveToNextStyleEnd();
                    if (end < next)
                    {
                        this._str.Offset = this._str.Length;
                        return(null);
                    }

                    selector = this._str.Substring(start, next - start);
                    string style = this._str.Substring(next + 1, end - (next + 1));



                    StyleDefn defn = new StyleDefn();

                    defn.Match = selector;

                    if (this._log.ShouldLog(TraceLevel.Verbose))
                    {
                        this._log.Add(TraceLevel.Verbose, "CSS", "Found css selector " + defn.Match.ToString() + " parsing inner contents");
                    }

                    CSSStyleItemReader    reader = new CSSStyleItemReader(style);
                    CSSStyleItemAllParser parser = new CSSStyleItemAllParser();

                    while (reader.ReadNextAttributeName())
                    {
                        parser.SetStyleValue(this._log, defn, reader);
                    }

                    //success, so we can return
                    parsed           = defn;
                    this._str.Offset = end;
                }
            }
            catch (Exception ex)
            {
                this._log.Add(TraceLevel.Error, "CSS", "Parsing of css failed with message : " + ex.Message, ex);

                if (null != this._owner)
                {
                    this._owner.RegisterParsingError(start, selector, ex);
                }

                this._str.Offset = this._str.Length;
            }


            return(parsed);
        }