예제 #1
0
        public void EnsureCssRuleId(List <Model.StyleModel> items, RenderContext context)
        {
            List <Model.StyleModel> addtional = new List <Model.StyleModel>();

            foreach (var item in items.GroupBy(o => o.StyleId))
            {
                if (item.Key != default(Guid))
                {
                    var allrules = context.WebSite.SiteDb().CssRules.Query.Where(o => o.ParentStyleId == item.Key).SelectAll();

                    foreach (var ruleitem in item)
                    {
                        if (ruleitem.RuleId == default(Guid) && !string.IsNullOrEmpty(ruleitem.Selector))
                        {
                            var foundrules = allrules.FindAll(o => CssSelectorComparer.IsEqual(o.SelectorText, ruleitem.Selector));

                            if (foundrules != null && foundrules.Count() > 0)
                            {
                                var result = AssignRuleId(ruleitem, foundrules);
                                addtional.AddRange(result);
                            }
                        }
                    }
                }
            }

            if (addtional.Count() > 0)
            {
                items.AddRange(addtional);
            }
        }
예제 #2
0
        public void EnsureCssRuleId(List <Model.StyleModel> items, RenderContext context)
        {
            var addtional = new List <Model.StyleModel>();

            Func <string, string> removeSpace = (s) => Regex.Replace(s, @"\s", "", RegexOptions.IgnoreCase);

            foreach (var item in items.GroupBy(o => o.StyleId))
            {
                if (item.Key != default(Guid))
                {
                    var allrules = context.WebSite.SiteDb().CssRules.Query.Where(o => o.ParentStyleId == item.Key).SelectAll();

                    foreach (var ruleitem in item)
                    {
                        if (ruleitem.RuleId == default(Guid) && !string.IsNullOrEmpty(ruleitem.Selector))
                        {
                            var foundrules = allrules.FindAll(o => CssSelectorComparer.IsEqual(o.SelectorText, ruleitem.Selector));

                            if (foundrules != null && foundrules.Count() > 0)
                            {
                                if (!string.IsNullOrWhiteSpace(ruleitem.MediaRuleList))
                                {
                                    foundrules = foundrules.Where(w => {
                                        var parent = allrules.FirstOrDefault(f => f.Id == w.ParentCssRuleId);
                                        return(parent != null && removeSpace(parent.SelectorText) == removeSpace(ruleitem.MediaRuleList));
                                    }).ToList();
                                }
                                else
                                {
                                    foundrules = foundrules.Where(e => e.ParentCssRuleId == default(Guid)).ToList();
                                }

                                var result = AssignRuleId(ruleitem, foundrules);
                                addtional.AddRange(result);
                            }
                        }
                    }
                }
            }

            if (addtional.Count() > 0)
            {
                items.AddRange(addtional);
            }
        }
예제 #3
0
        internal void UpdateStyleSheet(RenderContext context, Guid?styleId)
        {
            if (!styleId.HasValue)
            {
                return;
            }
            var rules      = context.WebSite.SiteDb().CssRules.Query.Where(o => o.ParentStyleId == styleId).SelectAll();
            var changelist = new List <CmsCssRuleChanges>();

            if (!string.IsNullOrWhiteSpace(MediaRuleList))
            {
                var mediaRuleId = rules.FirstOrDefault(f => CssSelectorComparer.IsEqual(f.SelectorText, MediaRuleList))?.Id;
                if (!mediaRuleId.HasValue)
                {
                    rules = rules.Where(w => w.ParentCssRuleId == mediaRuleId).ToList();
                }
            }

            rules = rules.Where(w => CssSelectorComparer.IsEqual(w.SelectorText, Selector)).ToList();

            foreach (var rule in rules)
            {
                CSSDeclarationBlock declarations = Service.CssService.ParseStyleDeclaration(rule);
                if (declarations == null)
                {
                    declarations = new CSSDeclarationBlock();
                }

                if (!string.IsNullOrEmpty(Value))
                {
                    declarations.setProperty(Property, Value, Important);
                    var prop = declarations.item.FirstOrDefault(f => f != null && f.propertyname == Property);
                    if (prop != null)
                    {
                        declarations.item.Remove(prop);
                        declarations.item.Add(prop);
                    }
                }
                else
                {
                    declarations.removeProperty(Property);
                }

                var onechange = new CmsCssRuleChanges
                {
                    CssRuleId       = rule.Id,
                    selectorText    = rule.SelectorText,
                    DeclarationText = declarations.GenerateCssText()
                };

                if (declarations.item.Count() == 0)
                {
                    onechange.ChangeType = ChangeType.Delete;
                }
                else
                {
                    if (rule.Id == default(Guid))
                    {
                        onechange.ChangeType = ChangeType.Add;
                    }
                    else
                    {
                        onechange.ChangeType = ChangeType.Update;
                    }
                }

                changelist.Add(onechange);
            }

            context.WebSite.SiteDb().CssRules.UpdateStyle(changelist, styleId.Value);
        }