using System;
using System.Xml;
using System.Collections;
namespace SharpVectors.Dom.Css
{
///
/// Used internally to store collected properties.
///
public class CssCollectedProperty
{
#region Constructors
internal CssCollectedProperty(string name, int specificity, CssValue cssValue, CssStyleSheetType origin, string priority)
{
Name = name;
Specificity = specificity;
Origin = origin;
CssValue = cssValue;
Priority = priority;
}
#endregion
#region Public properties
///
/// The name of the property
///
public string Name;
///
/// The calculated specificity
///
public int Specificity;
///
/// The origin of the collected property.
///
public CssStyleSheetType Origin;
///
/// The value of the property
///
public CssValue CssValue;
///
/// The priority of the property, e.g. "important"
///
public string Priority;
#endregion
#region Internal methods
internal bool IsBetterThen(CssCollectedProperty existing)
{
bool yes = false;
#region sorting according to the rules at http://www.w3.org/TR/CSS21/cascade.html#cascading-order
bool gotHigherSpecificity = (Specificity>=existing.Specificity);
switch(existing.Origin)
{
case CssStyleSheetType.UserAgent:
yes = (Origin == CssStyleSheetType.UserAgent) ? gotHigherSpecificity : true;
break;
case CssStyleSheetType.NonCssPresentationalHints:
yes = (Origin != CssStyleSheetType.UserAgent);
break;
case CssStyleSheetType.Inline:
yes = (Origin != CssStyleSheetType.UserAgent &&
Origin != CssStyleSheetType.NonCssPresentationalHints);
break;
case CssStyleSheetType.Author:
if(Origin == CssStyleSheetType.Author &&
Priority == existing.Priority &&
gotHigherSpecificity)
{
// author rules of the same priority
yes = true;
}
else if(Origin == CssStyleSheetType.Inline)
{
// inline rules override author rules
yes = true;
}
else if(Origin == CssStyleSheetType.User && Priority == "important")
{
// !important user rules overrides author rules
yes = true;
}
else if(Origin == CssStyleSheetType.Author &&
existing.Priority != "important" &&
Priority == "important"
)
{
// !important author rules override non-!important author rules
yes = true;
}
break;
case CssStyleSheetType.User:
if(Origin == CssStyleSheetType.User &&
existing.Priority == Priority &&
gotHigherSpecificity)
{
yes = true;
}
else if((Origin == CssStyleSheetType.Author || Origin == CssStyleSheetType.Inline) &&
existing.Priority != "important")
{
// author rules overrides not !important user rules
yes = true;
}
else if(Origin == CssStyleSheetType.User &&
Priority == "important")
{
// !important user rules override non-!important user rules
yes = true;
}
break;
}
#endregion
return yes;
}
#endregion
}
}