public static CSSStyleSheet ConvertToCSSStyleSheet(AntlrModel.Stylesheet stylesheet, string basePath = "") { var cssStyleSheet = new CSSStyleSheet(); cssStyleSheet.cssText = stylesheet.Serialize(); cssStyleSheet.charsetRule = ConvertToCSSCharsetRule(stylesheet.CharSet); foreach (var import in stylesheet.Imports) { var importRule = ConvertToCSSImportRule(import, basePath); cssStyleSheet.cssRules.appendRule(importRule); } foreach (var bodyset in stylesheet.BodyList) { var rule = new CSSRule(); if (bodyset is AntlrModel.RuleSet) { rule = ConvertToCSSStyleRule(bodyset as AntlrModel.RuleSet); } else if (bodyset is AntlrModel.PageRule) { rule = ConvertToCSSPageRule(bodyset as AntlrModel.PageRule); } else if (bodyset is AntlrModel.Media) { rule = ConvertToCSSMediaRule(bodyset as AntlrModel.Media); } cssStyleSheet.cssRules.appendRule(rule); } return(cssStyleSheet); }
/// <summary> /// The parsing algo from W3C does not parse CSS into right CSS object model, this is to convert to right CSSOM. /// </summary> /// <param name="rawstylesheet"></param> /// <returns></returns> private static CSSStyleSheet ParseCSSStyleSheet(stylesheet rawstylesheet, string baseurl, bool downloadImportRule, ref string OriginalCss) { CSSStyleSheet CssStyleSheet = new CSSStyleSheet(); foreach (var item in rawstylesheet.items) { if (item.Type == enumRuleType.QualifiedRule) { CSSRule rule = ParseQualifiedRule(item as QualifiedRule, ref OriginalCss); if (rule != null) { rule.parentStyleSheet = CssStyleSheet; CssStyleSheet.cssRules.appendRule(rule); } } else if (item.Type == enumRuleType.AtRule) { CSSRule rule = ParseAtRule(item as AtRule, CssStyleSheet, baseurl, downloadImportRule, ref OriginalCss); if (rule != null) { rule.parentStyleSheet = CssStyleSheet; CssStyleSheet.cssRules.appendRule(rule); } } } return(CssStyleSheet); }
public CssRuleViewModel(CSSRule rule) { Init(rule); switch (rule.Type) { case CssRuleType.FontFace: var font = (CSSFontFaceRule)rule; name = "@font-face"; Populate(font.CssRules); break; case CssRuleType.Keyframe: var keyframe = (CSSKeyframeRule)rule; name = keyframe.KeyText; Populate(keyframe.Style); break; case CssRuleType.Keyframes: var keyframes = (CSSKeyframesRule)rule; name = "@keyframes " + keyframes.Name; Populate(keyframes.CssRules); break; case CssRuleType.Media: var media = (CSSMediaRule)rule; name = "@media " + media.ConditionText; Populate(media.CssRules); break; case CssRuleType.Page: var page = (CSSPageRule)rule; name = "@page " + page.SelectorText; Populate(page.Style); break; case CssRuleType.Style: var style = (CSSStyleRule)rule; name = style.SelectorText; Populate(style.Style); break; case CssRuleType.Supports: var support = (CSSSupportsRule)rule; name = "@supports " + support.ConditionText; Populate(support.CssRules); break; default: name = rule.CssText; break; } }
public void BuildStyle() { var rule = new CSSRule(); rule.Type = CSSRuleType.StyleRule; rule.ParentStyleSheet = StyleSheet; rule.SelectorText = CurrentSelector.Replace("\r", ""); rule.Style = CSSStyleDeclaration.Parse(Fields); StyleSheet.CssRules.Add(rule); CurrentSelector = string.Empty; Fields.Clear(); }
/// <summary> /// Adds a new rule. /// </summary> /// <param name="rule">The new rule.</param> void AddRule(CSSRule rule) { rule.ParentStyleSheet = sheet; if (open.Count > 0) { var container = open.Peek() as ICssRules; if (container != null) { container.CssRules.List.Add(rule); rule.ParentRule = open.Peek(); } } else { sheet.CssRules.List.Add(rule); } open.Push(rule); }
public void InsertRule(CSSRule rule) { CssRules.Add(rule); }
public void DeleteRule(CSSRule rule) { CssRules.Remove(rule); }
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> /// make a list of changes to cssrules. /// </summary> /// <param name="store"></param> /// <param name="changelist"></param> public void UpdateStyle(List <CmsCssRuleChanges> changelist, Guid ParentStyleId) { Style style = SiteDb.Styles.Get(ParentStyleId); if (style == null) { return; } var ConvertedCssRules = Kooboo.Sites.Service.CssService.ConvertCss(style.Body, style.Id); List <ChangePlan> changeplans = new List <ChangePlan>(); string cssText = style.Body; foreach (var item in changelist) { if (item.ChangeType == ChangeType.Update || item.ChangeType == ChangeType.Delete) { var converted = ConvertedCssRules.Find(o => o.RuleId == item.CssRuleId); CmsCssRule oldrule = converted != null ? converted.CmsRule : null; if (oldrule != null) { CSSRule cssrule = converted.CssRule; if (oldrule.ruleType == RuleType.StyleRule) { if (item.ChangeType == ChangeType.Delete) { changeplans.Add(new ChangePlan() { StartIndex = cssrule.StartIndex, EndIndex = cssrule.EndIndex, ChangeInto = "" }); } else { changeplans.Add(new ChangePlan() { StartIndex = cssrule.StartIndex, EndIndex = cssrule.EndIndex, ChangeInto = item.CssText }); } } else if (oldrule.ruleType == RuleType.MediaRule) { // media rule can only change selector text. if (item.ChangeType == ChangeType.Update) { string newselector = item.selectorText; changeplans.Add(new ChangePlan() { StartIndex = cssrule.StartIndex, EndIndex = cssrule.EndSelectorIndex, ChangeInto = newselector }); } else { /// this is del this media rule. changeplans.Add(new ChangePlan() { StartIndex = cssrule.StartIndex, EndIndex = cssrule.EndIndex, ChangeInto = "" }); } } else if (oldrule.ruleType == RuleType.FontFaceRule) { if (item.ChangeType == ChangeType.Update) { string newcsstext = item.DeclarationText; changeplans.Add(new ChangePlan() { StartIndex = cssrule.EndSelectorIndex + 1, EndIndex = cssrule.EndIndex, ChangeInto = newcsstext }); } else { // to del one rule. changeplans.Add(new ChangePlan() { StartIndex = cssrule.StartIndex, EndIndex = cssrule.EndIndex, ChangeInto = "" }); } } else if (oldrule.ruleType == RuleType.ImportRule) { if (item.ChangeType == ChangeType.Update) { string newimportext = item.selectorText; changeplans.Add(new ChangePlan() { StartIndex = cssrule.EndSelectorIndex + 1, EndIndex = cssrule.EndIndex, ChangeInto = newimportext }); } else { changeplans.Add(new ChangePlan() { StartIndex = cssrule.StartIndex, EndIndex = cssrule.EndIndex, ChangeInto = "" }); } } } } else { //this is a new add. string newstring = "\r\n" + item.CssText; int insertposition = GetAppendIndex(ConvertedCssRules); if (item.CssText.IndexOf("@import", StringComparison.OrdinalIgnoreCase) != -1) { changeplans.Add(new ChangePlan() { StartIndex = 0, EndIndex = -1, ChangeInto = newstring }); } else if (insertposition > -1) { changeplans.Add(new ChangePlan() { StartIndex = insertposition, EndIndex = insertposition - 1, ChangeInto = newstring }); } else { changeplans.Add(new ChangePlan() { StartIndex = -1, EndIndex = -1, ChangeInto = newstring }); } } } string newCssText = ParseChange(cssText, changeplans); style.Body = newCssText; SiteDb.Styles.AddOrUpdate(style, true, true); }
public static List <SKPaint> ToSkPaints(this CSSRule rule) { // TODO: Impl return(new List <SKPaint>()); }
public CSSStyleSheet(CSSRule ownerRule) { this.ownerRule = ownerRule; }