/// <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; }
internal static CssStyleDeclaration ParseDeclarations(String declarations) { var parser = new CssParser(); var style = new CssStyleDeclaration(parser); style.Update(declarations); return style; }
/// <summary> /// Fills the given parent style with declarations given by the tokens. /// </summary> public void FillDeclarations(CssStyleDeclaration style) { var token = _tokenizer.Get(); while (token.IsNot(CssTokenType.Eof, CssTokenType.CurlyBracketClose)) { var property = CreateDeclaration(ref token); if (property != null && property.HasValue) style.SetProperty(property); } }
/// <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; }
/// <summary> /// Creates a new CSS style rule. /// </summary> internal CssStyleRule(CssParser parser) : base(CssRuleType.Style, parser) { _style = new CssStyleDeclaration(this); _selector = SimpleSelector.All; }
/// <summary> /// Creates a style declaration for the given source. /// </summary> /// <param name="source"> /// The source code for the inline style declaration. /// </param> /// <param name="options"> /// The options with the parameters for evaluating the style. /// </param> /// <returns>The created style declaration.</returns> public ICssStyleDeclaration ParseInline(String source, StyleOptions options) { var parser = new CssParser(_options, options.Configuration); var style = new CssStyleDeclaration(parser); style.Update(source); return style; }
public void CSSStyleDeclarationUnbound() { var parser = new CssParser(); var css = new CssStyleDeclaration(parser); var text = "background-color: rgb(255, 0, 0); color: rgb(0, 0, 0)"; css.CssText = text; Assert.AreEqual(text, css.CssText); Assert.AreEqual(2, css.Length); }
public void CSSStyleDeclarationEmpty() { var parser = new CssParser(); var css = new CssStyleDeclaration(parser); Assert.AreEqual("", css.CssText); Assert.AreEqual(0, css.Length); }
/// <summary> /// Creates a new @keyframe rule. /// </summary> internal CssKeyframeRule(CssParser parser) : base(CssRuleType.Keyframe, parser) { _style = new CssStyleDeclaration(this); }
/// <summary> /// Fills the given parent style with declarations given by the tokens. /// </summary> public TextPosition FillDeclarations(CssStyleDeclaration style) { var token = NextToken(); _nodes.Push(style); CollectTrivia(ref token); while (token.IsNot(CssTokenType.EndOfFile, CssTokenType.CurlyBracketClose)) { var property = CreateDeclarationWith(Factory.Properties.Create, ref token); if (property != null && property.HasValue) { style.SetProperty(property); } CollectTrivia(ref token); } _nodes.Pop(); return token.Position; }
/// <summary> /// Takes a string and appends all rules to the given list of /// properties. /// </summary> internal void AppendDeclarations(CssStyleDeclaration style, String declarations) { var tokenizer = CreateTokenizer(declarations, _config); var state = new CssUnknownState(tokenizer, this); state.FillDeclarations(style); }
public void CssPropertyFactoryCalls() { var parser = new CssParser(); var decl = new CssStyleDeclaration(parser); var invalid = decl.CreateProperty("invalid"); var border = decl.CreateProperty("border"); var color = decl.CreateProperty("color"); decl.SetProperty(color); var colorAgain = decl.CreateProperty("color"); Assert.IsNull(invalid); Assert.IsNotNull(border); Assert.IsNotNull(color); Assert.IsNotNull(colorAgain); Assert.IsInstanceOf<CssBorderProperty>(border); Assert.IsInstanceOf<CssColorProperty>(color); Assert.AreEqual(color, colorAgain); }