/// <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); }
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 })); }
public void Append_Rule() { var css = @"body {font-family:Arial;}/** Umb_Name: Test1 */ p { font-size: 1em; } /** umb_name: Test2 */ li {padding:0px;} table {margin:0;}"; var result = StylesheetHelper.AppendRule(css, new StylesheetRule() { Name = "My new rule", Selector = "p", Styles = "font-size:1em; color:blue;" }); Assert.AreEqual(@"body {font-family:Arial;}/** Umb_Name: Test1 */ p { font-size: 1em; } /** umb_name: Test2 */ li {padding:0px;} table {margin:0;} /**umb_name:My new rule*/ p{font-size:1em; color:blue;}".StripWhitespace(), result.StripWhitespace()); }
/// <summary> /// Adds an Umbraco stylesheet property for use in the back office /// </summary> /// <param name="property"></param> public void AddProperty(StylesheetProperty property) { if (Properties.Any(x => x.Name.InvariantEquals(property.Name))) { throw new DuplicateNameException("The property with the name " + property.Name + " already exists in the collection"); } //now we need to serialize out the new property collection over-top of the string Content. Content = StylesheetHelper.AppendRule(Content, new StylesheetRule { Name = property.Name, Selector = property.Alias, Styles = property.Value }); //re-set lazy collection InitializeProperties(); }
public void AppendRules_IsFormatted() { // base CSS var css = Tabbed( @"body { #font-family:Arial; }"); // add a couple of rules var result = StylesheetHelper.AppendRule(css, new StylesheetRule { Name = "Test", Selector = ".test", Styles = "font-color: red;margin: 1rem;" }); result = StylesheetHelper.AppendRule(result, new StylesheetRule { Name = "Test2", Selector = ".test2", Styles = "font-color: green;" }); // verify the CSS formatting including the indents Assert.AreEqual(Tabbed( @"body { #font-family:Arial; } /**umb_name:Test*/ .test { #font-color: red; #margin: 1rem; } /**umb_name:Test2*/ .test2 { #font-color: green; }"), result ); }