/// <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; }
/// <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> /// <returns>Returns the cascaded read-only style declaration.</returns> public static CssStyleDeclaration ComputeCascadedStyle(this StyleCollection styleCollection, IElement element) { var computedStyle = new CssStyleDeclaration(); var rules = styleCollection.SortBySpecifity(element); foreach (var rule in rules) { var inlineStyle = rule.Style; computedStyle.SetDeclarations(inlineStyle.Declarations); } return computedStyle; }