Exemplo n.º 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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Specific version optimized for a read-only Html document
        /// </summary>
        public ICssStyleDeclaration GetComputedStyleInReadOnlyDocument(IElement element, String pseudo = null)
        {
            if (styleRulesCache == null)
            {
                styleRulesCache = this.GetStyleCollection();
                styleRulesCache.FreezeForReuse();
            }

            var pseudoElement = PseudoElement.Create(element, pseudo);

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

            var computedStyle = new CssStyleDeclaration();

            var elementStyle = this.ComputeCascadedStyleInReadOnlyDocument(styleRulesCache, element);

            computedStyle.SetDeclarations(elementStyle.Declarations);

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

            foreach (var ancestorElement in ancestorElements)
            {
                var ancestorStyle = ComputeCascadedStyleInReadOnlyDocument(styleRulesCache, ancestorElement);
                computedStyle.UpdateDeclarations(ancestorStyle.Declarations);
            }

            return(computedStyle);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Computes the cascaded style, i.e. resolves the cascade by ordering after specifity.
        /// Two rules with the same specifity are ordered according to their appearance. The more
        /// recent declaration wins. Inheritance is not taken into account.
        /// </summary>
        /// <param name="styleCollection">The style rules to apply.</param>
        /// <param name="element">The element to compute the cascade for.</param>
        /// <param name="parent">The potential parent for the cascade.</param>
        /// <returns>Returns the cascaded read-only style declaration.</returns>
        public static ICssStyleDeclaration ComputeCascadedStyle(this StyleCollection styleCollection, IElement element, ICssStyleDeclaration parent = null)
        {
            var computedStyle = new CssStyleDeclaration(element.Owner?.Context);
            var rules         = styleCollection.SortBySpecificity(element);

            foreach (var rule in rules)
            {
                var inlineStyle = rule.Style;
                computedStyle.SetDeclarations(inlineStyle);
            }

            if (element is IHtmlElement || element is ISvgElement)
            {
                computedStyle.SetDeclarations(element.GetStyle());
            }

            if (parent != null)
            {
                computedStyle.UpdateDeclarations(parent);
            }

            return(computedStyle);
        }
Exemplo n.º 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);
        }