void Populate(CSSRuleList rules) { foreach (var rule in rules) { children.Add(new CssRuleViewModel(rule)); } }
private static CSSRule ParseMediaRule(AtRule rule, CSSStyleSheet parentSheet, ref string OriginalCss) { CSSMediaRule mediarule = new CSSMediaRule(); mediarule.parentStyleSheet = parentSheet; string media = string.Empty; string wholeconditiontext = string.Empty; int startindex = -1; int endindex = -1; int endindexselector = -1; startindex = rule.prelude[0].startindex; // the first componentvalue is a preservedtoken and it media. rule.prelude.RemoveAt(0); wholeconditiontext = ComponentValueExtension.getString(rule.prelude, ref startindex, ref endindexselector, ref OriginalCss); foreach (var item in wholeconditiontext.Split(',')) { mediarule.media.appendMedium(item); } CSSRuleList blockrulelist = ParseMediaRuleList(rule.block, ref endindex, mediarule, ref OriginalCss); if (blockrulelist != null) { mediarule.cssRules = blockrulelist; } mediarule.conditionText = wholeconditiontext; //SelectorText is assigned in a different way now. // mediarule.selectorText = wholeconditiontext; /// NON-W3C. mediarule.StartIndex = rule.startindex; if (rule.endindex > endindex) { endindex = rule.endindex; } mediarule.EndIndex = endindex; mediarule.EndSelectorIndex = endindexselector - mediarule.StartIndex + 1; return(mediarule); }
public static List <CssConvertResult> ConvertCss(CSSRuleList rulelist, Guid ParentStyleId, Guid ParentCssRuleId = default(Guid)) { List <CssConvertResult> Result = new List <CssConvertResult>(); int counter = rulelist.item.Count; for (int i = 0; i < counter; i++) { CmsCssRule cmsrule = new CmsCssRule(); cmsrule.ParentCssRuleId = ParentCssRuleId; cmsrule.ParentStyleId = ParentStyleId; cmsrule.OwnerObjectId = ParentStyleId; cmsrule.OwnerObjectConstType = ConstObjectType.Style; cmsrule.TempCssRuleIndex = i; CSSRule item = rulelist.item[i]; cmsrule.selectorPositionIndex = item.EndSelectorIndex; if (item.type == enumCSSRuleType.STYLE_RULE) { CSSStyleRule stylerule = item as CSSStyleRule; cmsrule.CssText = stylerule.cssText; cmsrule.ruleType = RuleType.StyleRule; cmsrule.Properties = stylerule.style.item.Where(o => o != null && !string.IsNullOrWhiteSpace(o.propertyname)).Select(o => o.propertyname).ToList(); } else if (item.type == enumCSSRuleType.IMPORT_RULE) { CSSImportRule importrule = item as CSSImportRule; cmsrule.CssText = importrule.cssText; cmsrule.ruleType = RuleType.ImportRule; } else if (item.type == enumCSSRuleType.MEDIA_RULE) { CSSMediaRule mediarule = item as CSSMediaRule; cmsrule.CssText = mediarule.cssText; cmsrule.ruleType = RuleType.MediaRule; } else if (item.type == enumCSSRuleType.FONT_FACE_RULE) { CSSFontFaceRule facerule = item as CSSFontFaceRule; cmsrule.CssText = facerule.cssText; cmsrule.ruleType = RuleType.FontFaceRule; } else { continue; } /// check duplicate in the current collections. cmsrule.DuplicateIndex = CssService.GetDuplicateIndex(Result, cmsrule.SelectorText, cmsrule.ruleType); CssConvertResult converted = new CssConvertResult(); converted.RuleId = cmsrule.Id; converted.CmsRule = cmsrule; converted.CssRule = item; Result.Add(converted); if (item.type == enumCSSRuleType.MEDIA_RULE) { var mediarule = item as Kooboo.Dom.CSS.CSSMediaRule; var sub = ConvertCss(mediarule.cssRules, ParentStyleId, cmsrule.Id); if (sub != null && sub.Count() > 0) { Result.AddRange(sub); } } } return(Result); }
/// <summary> /// consume a list of cssrule from simpleblock, /// Recursive. /// </summary> /// <param name="block"></param> /// <returns></returns> private static CSSRuleList ParseMediaRuleList(SimpleBlock block, ref int endindex, CSSMediaRule parentmediarule, ref string OriginalCss) { int count = block.value.Count; CSSRuleList rulelist = new CSSRuleList(); MediaRuleParseState state = MediaRuleParseState.init; CSSStyleRule stylerule = null; CSSMediaRule mediarule = null; string media = string.Empty; string wholeconditiontext = string.Empty; int startindex = -1; for (int i = 0; i < count; i++) { if (block.value[i].endindex > endindex) { endindex = block.value[i].endindex; } if (startindex < 0) { startindex = block.value[i].startindex; } switch (state) { case MediaRuleParseState.init: { if (block.value[i].Type == CompoenentValueType.preservedToken) { PreservedToken pretoken = block.value[i] as PreservedToken; if (pretoken.token.Type == enumTokenType.whitespace) { // ignored whitespace at the beginning. } else if (pretoken.token.Type == enumTokenType.at_keyword) { // at keyword token, only handle media now. // others to be added. at_keyword_token token = pretoken.token as at_keyword_token; if (token.value.ToLower() == "media") { state = MediaRuleParseState.mediarule; i = i - 1; // reconsume to have correct startindex. } else { // other at rules. state = MediaRuleParseState.OtherAtRule; i = i - 1; } } /// else treat as regular style rule. else { state = MediaRuleParseState.stylerule; i = i - 1; // reconsume. } } break; } case MediaRuleParseState.stylerule: { if (stylerule == null) { stylerule = new CSSStyleRule(); startindex = block.value[i].startindex; } if (block.value[i].Type == CompoenentValueType.preservedToken) { PreservedToken pretoken = block.value[i] as PreservedToken; // not a defined way to parse the selector, assembly them back and give it to selector module. // in the new way of getting selectorText, we have not need to assign it any more. //stylerule.selectorText += pretoken.token.getString(); } else if (block.value[i].Type == CompoenentValueType.simpleBlock) { int endselectorindex = block.value[i].startindex; stylerule.style = ParseDeclarations(block.value[i] as SimpleBlock, ref endindex, ref OriginalCss); stylerule.StartIndex = startindex; stylerule.EndIndex = endindex; stylerule.EndSelectorIndex = endselectorindex - stylerule.StartIndex; stylerule.parentRule = parentmediarule; stylerule.parentStyleSheet = parentmediarule.parentStyleSheet; rulelist.appendRule(stylerule); stylerule = null; state = MediaRuleParseState.init; startindex = -1; } break; } case MediaRuleParseState.mediarule: { if (mediarule == null) { mediarule = new CSSMediaRule(); media = string.Empty; wholeconditiontext = string.Empty; startindex = block.value[i].startindex; mediarule.parentStyleSheet = parentmediarule.parentStyleSheet; mediarule.parentRule = parentmediarule; } if (block.value[i].Type == CompoenentValueType.preservedToken) { PreservedToken pretoken = block.value[i] as PreservedToken; if (pretoken.token.Type == enumTokenType.comma) { if (!string.IsNullOrEmpty(media)) { mediarule.media.appendMedium(media.Trim()); media = string.Empty; } wholeconditiontext += ","; } else { // can be delim token. if (string.IsNullOrEmpty(media) && pretoken.token.Type == enumTokenType.whitespace) { // the start of whitespace will be ignored. } else { media += pretoken.token.GetString(ref OriginalCss); wholeconditiontext += pretoken.token.GetString(ref OriginalCss); } } } else if (block.value[i].Type == CompoenentValueType.simpleBlock) { CSSRuleList mediarulelist = ParseMediaRuleList(block.value[i] as SimpleBlock, ref endindex, mediarule, ref OriginalCss); mediarule.cssRules = mediarulelist; if (!string.IsNullOrEmpty(media)) { mediarule.media.appendMedium(media.Trim()); wholeconditiontext += media; } mediarule.conditionText = wholeconditiontext; mediarule.selectorText = wholeconditiontext; /// NON-W3C. mediarule.StartIndex = startindex; mediarule.EndIndex = endindex; rulelist.appendRule(mediarule); state = 0; mediarule = null; media = string.Empty; wholeconditiontext = string.Empty; startindex = -1; } break; } case MediaRuleParseState.OtherAtRule: { //if (mediarule == null) //{ // mediarule = new CSSMediaRule(); // media = string.Empty; // wholeconditiontext = string.Empty; // startindex = block.value[i].startindex; // mediarule.parentStyleSheet = parentmediarule.parentStyleSheet; // mediarule.parentRule = parentmediarule; //} if (block.value[i].Type == CompoenentValueType.preservedToken) { //PreservedToken pretoken = block.value[i] as PreservedToken; //if (pretoken.token.Type == enumTokenType.comma) //{ // if (!string.IsNullOrEmpty(media)) // { // mediarule.media.appendMedium(media.Trim()); // media = string.Empty; // } // wholeconditiontext += ","; //} //else //{ // // can be delim token. // if (string.IsNullOrEmpty(media) && pretoken.token.Type == enumTokenType.whitespace) // { // // the start of whitespace will be ignored. // } // else // { // media += pretoken.token.GetString(ref OriginalCss); // wholeconditiontext += pretoken.token.GetString(ref OriginalCss); // } //} } else if (block.value[i].Type == CompoenentValueType.simpleBlock) { // not implemented now. //CSSRuleList mediarulelist = ParseMediaRuleList(block.value[i] as SimpleBlock, ref endindex, mediarule, ref OriginalCss); //mediarule.cssRules = mediarulelist; //if (!string.IsNullOrEmpty(media)) //{ // mediarule.media.appendMedium(media.Trim()); // wholeconditiontext += media; //} //mediarule.conditionText = wholeconditiontext; //mediarule.selectorText = wholeconditiontext; /// NON-W3C. //mediarule.StartIndex = startindex; //mediarule.EndIndex = endindex; //rulelist.appendRule(mediarule); state = MediaRuleParseState.init; startindex = -1; } break; } default: break; } } if (stylerule != null) { if (stylerule.EndIndex > stylerule.StartIndex) { rulelist.appendRule(stylerule); } stylerule = null; } if (mediarule != null) { rulelist.appendRule(mediarule); mediarule = null; } return(rulelist); }
/// <summary> /// Converts the list of CSS rules to a CSS block string. /// </summary> /// <param name="rules">The list of CSS rules.</param> /// <returns>The block string representation.</returns> public static String ToCssBlock(this CSSRuleList rules) { return(String.Concat("{ ", rules.ToCss(), rules.Length > 0 ? " }" : "}")); }