示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PrioritizedStyle"/> structure.
 /// </summary>
 /// <param name="style">The style being applied.</param>
 /// <param name="selector">The selector which caused the style to be applied.</param>
 /// <param name="priority">The style's priority.</param>
 /// <param name="index">The index of the style's rule set within its style sheet.</param>
 public PrioritizedStyle(UvssRule style, UvssSelector selector, Int32 priority, Int32 index)
 {
     this.Style    = style;
     this.Selector = selector;
     this.Priority = priority;
     this.Index    = index;
 }
        /// <summary>
        /// Adds a styling rule to the prioritizer.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="selector">The selector which caused this style to be considered.</param>
        /// <param name="navigationExpression">The navigation expression associated with the style.</param>
        /// <param name="rule">The styling rule to add to the prioritizer.</param>
        /// <param name="index">The index of the rule's rule set within the style sheet.</param>
        public void Add(UltravioletContext uv, UvssSelector selector, NavigationExpression?navigationExpression, UvssRule rule, Int32 index)
        {
            Contract.Require(uv, nameof(uv));

            var key      = new StyleKey(rule.CanonicalName, navigationExpression);
            var priority = CalculatePriorityFromSelector(selector, rule.IsImportant);

            PrioritizedStyle existing;

            if (!rules.TryGetValue(key, out existing))
            {
                rules[key] = new PrioritizedStyle(rule, selector, priority, index);
            }
            else
            {
                var comparison = selector.ComparePriority(existing.Selector);
                if (comparison == 0 && index > existing.Index)
                {
                    comparison = 1;
                }

                if (comparison > 0 && (rule.IsImportant || !existing.Style.IsImportant))
                {
                    rules[key] = new PrioritizedStyle(rule, selector, priority, index);
                }
            }
        }