Example #1
0
        /// <summary>
        /// Computes the declarations for the given element in the context of
        /// the specified styling rules.
        /// </summary>
        /// <param name="rules">The styles to use.</param>
        /// <param name="element">The element that is questioned.</param>
        /// <param name="pseudoSelector">The optional pseudo selector to use.</param>
        /// <returns>The style declaration containing all the declarations.</returns>
        public static CssStyleDeclaration ComputeDeclarations(this StyleCollection rules, IElement element, String pseudoSelector = null)
        {
            var computedStyle = new CssStyleDeclaration();
            var pseudoElement = PseudoElement.Create(element, pseudoSelector);

            if (pseudoElement != null)
            {
                element = pseudoElement;
            }

            computedStyle.SetDeclarations(rules.ComputeCascadedStyle(element).Declarations);
            var htmlElement = element as IHtmlElement;

            if (htmlElement != null)
            {
                var declarations = htmlElement.Style.OfType <CssProperty>();
                computedStyle.SetDeclarations(declarations);
            }

            var nodes = element.GetAncestors().OfType <IElement>();

            foreach (var node in nodes)
            {
                var style = rules.ComputeCascadedStyle(node);
                computedStyle.UpdateDeclarations(style.Declarations);
            }

            return(computedStyle);
        }
Example #2
0
        private ElementRenderNode RenderElement(IElement reference, StyleCollection collection, ICssStyleDeclaration parent = null)
        {
            var style    = collection.ComputeCascadedStyle(reference, parent);
            var children = new List <IRenderNode>();

            foreach (var child in reference.ChildNodes)
            {
                if (child is IText text)
                {
                    children.Add(RenderText(text, collection));
                }
                else if (child is IElement element)
                {
                    children.Add(RenderElement(element, collection, style));
                }
            }

            return(new ElementRenderNode
            {
                Ref = reference,
                SpecifiedStyle = style,
                ComputedStyle = style.Compute(_device),
                Children = children,
            });
        }
Example #3
0
        public async Task NullSelectorStillWorks_Issue52()
        {
            var sheet    = ParseStyleSheet("a {}");
            var document = await sheet.Context.OpenAsync(res => res.Content("<body></body>"));

            sheet.Add(new CssStyleRule(sheet));
            var sc   = new StyleCollection(new[] { sheet }, new DefaultRenderDevice());
            var decl = sc.ComputeCascadedStyle(document.Body);

            Assert.IsNotNull(decl);
        }
Example #4
0
        /// <summary>
        /// Computes the declarations for the given element in the context of
        /// the specified styling rules.
        /// </summary>
        /// <param name="rules">The styles to use.</param>
        /// <param name="element">The element that is questioned.</param>
        /// <param name="pseudoSelector">The optional pseudo selector to use.</param>
        /// <returns>The style declaration containing all the declarations.</returns>
        public static ICssStyleDeclaration ComputeDeclarations(this StyleCollection rules, IElement element, String pseudoSelector = null)
        {
            var computedStyle = new CssStyleDeclaration(element.Owner?.Context);
            var nodes         = element.GetAncestors().OfType <IElement>();

            if (!String.IsNullOrEmpty(pseudoSelector))
            {
                var pseudoElement = element?.Pseudo(pseudoSelector.TrimStart(':'));

                if (pseudoElement != null)
                {
                    element = pseudoElement;
                }
            }

            computedStyle.SetDeclarations(rules.ComputeCascadedStyle(element));

            foreach (var node in nodes)
            {
                computedStyle.UpdateDeclarations(rules.ComputeCascadedStyle(node));
            }

            return(computedStyle);
        }
Example #5
0
        private CssStyleDeclaration ComputeCascadedStyleInReadOnlyDocument(StyleCollection styleRules, IElement element)
        {
            if (elementsStyleCache == null)
            {
                elementsStyleCache = new Dictionary <IElement, CssStyleDeclaration>();
            }

            CssStyleDeclaration elementStyle;

            if (!elementsStyleCache.TryGetValue(element, out elementStyle))
            {
                elementStyle = styleRules.ComputeCascadedStyle(element);
                elementsStyleCache[element] = elementStyle;
            }
            return(elementStyle);
        }