/// <summary>
        /// Creates a style sheet from CSS and style rules
        /// </summary>
        /// <param name="data">The style sheet data</param>
        /// <returns>The style sheet combined from the CSS and the rules</returns>
        /// <remarks>
        /// Any "umbraco style rules" in the CSS will be removed and replaced with the rules passed in <see cref="data"/>
        /// </remarks>
        public string PostInterpolateStylesheetRules(StylesheetData data)
        {
            // first remove all existing rules
            var existingRules = data.Content.IsNullOrWhiteSpace()
                ? new Core.Strings.Css.StylesheetRule[0]
                : StylesheetHelper.ParseRules(data.Content).ToArray();

            foreach (var rule in existingRules)
            {
                data.Content = StylesheetHelper.ReplaceRule(data.Content, rule.Name, null);
            }

            data.Content = data.Content.TrimEnd(CharArrays.LineFeedCarriageReturn);

            // now add all the posted rules
            if (data.Rules != null && data.Rules.Any())
            {
                foreach (var rule in data.Rules)
                {
                    data.Content = StylesheetHelper.AppendRule(data.Content, new Core.Strings.Css.StylesheetRule
                    {
                        Name     = rule.Name,
                        Selector = rule.Selector,
                        Styles   = rule.Styles
                    });
                }

                data.Content += Environment.NewLine;
            }

            return(data.Content);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Removes an Umbraco stylesheet property
 /// </summary>
 /// <param name="name"></param>
 public void RemoveProperty(string name)
 {
     if (Properties.Any(x => x.Name.InvariantEquals(name)))
     {
         Content = StylesheetHelper.ReplaceRule(Content, name, null);
     }
 }
        public ActionResult EditRule(StylesheetRuleEditorModel ruleModel)
        {
            Mandate.ParameterNotNull(ruleModel, "rule");

            if (!TryValidateModel(ruleModel))
            {
                return(View(ruleModel));
            }

            using (var uow = _hive.Create())
            {
                var repo       = uow.Repositories;
                var stylesheet = repo.Get <Umbraco.Framework.Persistence.Model.IO.File>(ruleModel.ParentId);
                var rule       = BackOfficeRequestContext.Application.FrameworkContext.TypeMappers.Map <StylesheetRule>(ruleModel);

                if (!ruleModel.Id.IsNullValueOrEmpty())
                {
                    var idParts = ruleModel.Id.StringParts().ToList();
                    if (idParts.Count == 2)
                    {
                        var oldRuleName = idParts[1].Replace("__s__", " ");

                        StylesheetHelper.ReplaceRule(stylesheet, oldRuleName, rule);
                    }
                    else
                    {
                        StylesheetHelper.AppendRule(stylesheet, rule);
                    }
                }
                else
                {
                    StylesheetHelper.AppendRule(stylesheet, rule);
                }

                repo.AddOrUpdate(stylesheet);
                uow.Complete();

                //Always update the Id to be based on the name
                ruleModel.Id = new HiveId(new Uri("storage://stylesheets"), string.Empty, new HiveIdValue(ruleModel.ParentId.Value + "/" + ruleModel.Name.Replace(" ", "__s__")));
            }

            Notifications.Add(new NotificationMessage("Rule saved", NotificationType.Success));

            //add path for entity for SupportsPathGeneration (tree syncing) to work,
            //we need to manually create this path:
            GeneratePathsForCurrentEntity(new EntityPathCollection(ruleModel.Id, new[] { new EntityPath(new[]
                {
                    _hive.GetRootNodeId(),
                    ruleModel.ParentId,
                    ruleModel.Id
                }) }));

            return(RedirectToAction("EditRule", new { id = ruleModel.Id }));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// If the property has changed then we need to update the content
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Property_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var prop = (StylesheetProperty)sender;

            //Ensure we are setting base.Content here so that the properties don't get reset and thus any event handlers would get reset too
            base.Content = StylesheetHelper.ReplaceRule(Content, prop.Name, new StylesheetRule
            {
                Name     = prop.Name,
                Selector = prop.Alias,
                Styles   = prop.Value
            });
        }
        public void Replace_Rule()
        {
            var css     = @"body {font-family:Arial;}/** Umb_Name: Test1 */ p { font-size: 1em; } /** umb_name:  Test2 */ li {padding:0px;} table {margin:0;}";
            var results = StylesheetHelper.ParseRules(css);

            var result = StylesheetHelper.ReplaceRule(css, results.First().Name, new StylesheetRule()
            {
                Name     = "My new rule",
                Selector = "p",
                Styles   = "font-size:1em; color:blue;"
            });

            Assert.AreEqual(@"body {font-family:Arial;}/**umb_name:My new rule*/
p{font-size:1em; color:blue;} /** umb_name:  Test2 */ li {padding:0px;} table {margin:0;}".StripWhitespace(), result.StripWhitespace());
        }
        public ActionResult DeleteRule(HiveId id)
        {
            Mandate.ParameterNotEmpty(id, "id");

            var idParts      = id.StringParts().ToList();
            var stylesheetId = idParts[0];
            var ruleName     = idParts[1];

            using (var uow = _hive.Create())
            {
                var repo       = uow.Repositories;
                var stylesheet = repo.Get <Umbraco.Framework.Persistence.Model.IO.File>(new HiveId(stylesheetId));

                StylesheetHelper.ReplaceRule(stylesheet, ruleName, null);

                repo.AddOrUpdate(stylesheet);
                uow.Complete();
            }

            //return a successful JSON response
            return(Json(new { message = "Success" }));
        }