Exemplo n.º 1
0
        protected bool IsVariableName(CSSStyleItemReader reader, out string name)
        {
            if (reader.CurrentAttribute.StartsWith(CSSVariableIdentifier))
            {
                var queue = new Queue <string>();

                name = reader.CurrentAttribute;
                return(true);
            }
            else
            {
                name = null;
                return(false);
            }
        }
Exemplo n.º 2
0
        public bool SetStyleValue(Style style, CSSStyleItemReader reader, PDFContextBase context)
        {
            var attr = reader.CurrentAttribute;
            //var val = reader.CurrentTextValue;


            bool success = this.DoSetStyleValue(style, reader);

            if (null != context && null != context.TraceLog)
            {
                if (success && context.ShouldLogDebug)
                {
                    context.TraceLog.Add(TraceLevel.Debug, "CSS", "The css style item " + (string.IsNullOrEmpty(attr) ? "[UNKNOWN]" : attr) + " set on style " + style.ToString());
                }
                else if (!success && context.TraceLog.ShouldLog(TraceLevel.Warning))
                {
                    context.TraceLog.Add(TraceLevel.Warning, "CSS", "The css style item " + (string.IsNullOrEmpty(attr) ? "[UNKNOWN]" : attr) + " could not be set on style " + style.ToString());
                }
            }
            return(success);
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
 protected abstract bool DoSetStyleValue(Style style, CSSStyleItemReader reader);
Exemplo n.º 8
0
 bool IParserStyleFactory.SetStyleValue(IHtmlContentParser parser, IPDFStyledComponent onComponent, CSSStyleItemReader reader)
 {
     return(this.SetStyleValue(onComponent.Style, reader, parser.Context));
 }