/// <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 void Duplicate_Names()
        {
            var css     = @"/** Umb_Name: Test */ p { font-size: 1em; } /** umb_name:  Test */ li {padding:0px;}";
            var results = StylesheetHelper.ParseRules(css);

            Assert.AreEqual(1, results.Count());
        }
        /// <summary>
        /// Need to check if the node being requested is a file, if so, return the rules for it, otherwise process as per normal
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="queryStrings"></param>
        /// <returns></returns>
        protected override UmbracoTreeResult GetTreeData(HiveId parentId, FormCollection queryStrings)
        {
            using (var uow = Hive.CreateReadonly())
            {
                var stylesheet = uow.Repositories.Get <File>(parentId);
                if (!stylesheet.IsContainer)
                {
                    var rules = StylesheetHelper.ParseRules(stylesheet);
                    if (rules.Any())
                    {
                        foreach (var rule in rules)
                        {
                            var hiveId = new HiveId(new Uri(HiveUriRouteMatch), string.Empty, new HiveIdValue(parentId.Value + "/" + rule.Name.Replace(" ", "__s__")));
                            var node   = CreateTreeNode(hiveId,
                                                        null,
                                                        rule.Name,
                                                        Url.GetEditorUrl("EditRule", hiveId, EditorControllerId, BackOfficeRequestContext.RegisteredComponents, BackOfficeRequestContext.Application.Settings),
                                                        false,
                                                        "tree-css-item");
                            node.AddEditorMenuItem <Delete>(this, "deleteUrl", "DeleteRule");
                            NodeCollection.Add(node);
                        }
                    }
                    return(UmbracoTree());
                }
            }

            return(base.GetTreeData(parentId, queryStrings));
        }
        public ActionResult EditRule(HiveId id)
        {
            Mandate.ParameterNotEmpty(id, "id");

            //The rule Id consists of both the stylesheet Id and the rule name
            var idParts      = id.StringParts().ToList();
            var stylesheetId = idParts[0];
            var ruleName     = idParts.Count > 1 ? idParts[1] : "";

            ViewBag.IsNew = string.IsNullOrWhiteSpace(ruleName);

            using (var uow = _hive.Create())
            {
                var stylesheet = uow.Repositories.Get <Umbraco.Framework.Persistence.Model.IO.File>(new HiveId(stylesheetId));
                var rule       = !string.IsNullOrWhiteSpace(ruleName) ?
                                 StylesheetHelper.ParseRules(stylesheet).Single(x => x.Name.Replace(" ", "__s__") == ruleName) :
                                 new StylesheetRule()
                {
                    StylesheetId = id
                };

                var ruleModel = BackOfficeRequestContext.Application.FrameworkContext.TypeMappers.Map <StylesheetRuleEditorModel>(rule);

                return(View(ruleModel));
            }
        }
        public void ParseFormattedRules_CanParse()
        {
            // base CSS
            var css = Tabbed(
                @"body {
#font-family:Arial;
}

/**umb_name:Test*/
.test {
#font-color: red;
#margin: 1rem;
}

/**umb_name:Test2*/
.test2 {
#font-color: green;
}");
            var rules = StylesheetHelper.ParseRules(css);

            Assert.AreEqual(2, rules.Count());

            Assert.AreEqual("Test", rules.First().Name);
            Assert.AreEqual(".test", rules.First().Selector);
            Assert.AreEqual(
                @"font-color: red;
margin: 1rem;", rules.First().Styles);

            Assert.AreEqual("Test2", rules.Last().Name);
            Assert.AreEqual(".test2", rules.Last().Selector);
            Assert.AreEqual("font-color: green;", rules.Last().Styles);
        }
        public void ParseRules_DoesntParse(string css)
        {
            // Act
            var results = StylesheetHelper.ParseRules(css);

            // Assert
            Assert.IsTrue(results.Count() == 0);
        }
        public void ParseRules_DoesntParse(string css)
        {
            // Act
            IEnumerable <StylesheetRule> results = StylesheetHelper.ParseRules(css);

            // Assert
            Assert.IsTrue(results.Any() == false);
        }
        public void ParseRules_Parses(string name, string selector, string styles, string css)
        {
            // Act
            var results = StylesheetHelper.ParseRules(css);

            // Assert
            Assert.AreEqual(1, results.Count());

            //Assert.IsTrue(results.First().RuleId.Value.Value.ToString() == file.Id.Value.Value + "/" + name);
            Assert.AreEqual(name, results.First().Name);
            Assert.AreEqual(selector, results.First().Selector);
            Assert.AreEqual(styles.StripWhitespace(), results.First().Styles.StripWhitespace());
        }
        /// <summary>
        /// Extracts "umbraco style rules" from a style sheet
        /// </summary>
        /// <param name="data">The style sheet data</param>
        /// <returns>The style rules</returns>
        public StylesheetRule[] PostExtractStylesheetRules(StylesheetData data)
        {
            if (data.Content.IsNullOrWhiteSpace())
            {
                return(new StylesheetRule[0]);
            }

            return(StylesheetHelper.ParseRules(data.Content)?.Select(rule => new StylesheetRule
            {
                Name = rule.Name,
                Selector = rule.Selector,
                Styles = rule.Styles
            }).ToArray());
        }
Example #10
0
        private void InitializeProperties()
        {
            //if the value is already created, we need to be created and update the collection according to
            //what is now in the content
            if (_properties != null && _properties.IsValueCreated)
            {
                //re-parse it so we can check what properties are different and adjust the event handlers
                var parsed   = StylesheetHelper.ParseRules(Content).ToArray();
                var names    = parsed.Select(x => x.Name).ToArray();
                var existing = _properties.Value.Where(x => names.InvariantContains(x.Name)).ToArray();
                //update existing
                foreach (var stylesheetProperty in existing)
                {
                    var updateFrom = parsed.Single(x => x.Name.InvariantEquals(stylesheetProperty.Name));
                    //remove current event handler while we update, we'll reset it after
                    stylesheetProperty.PropertyChanged -= Property_PropertyChanged;
                    stylesheetProperty.Alias            = updateFrom.Selector;
                    stylesheetProperty.Value            = updateFrom.Styles;
                    //re-add
                    stylesheetProperty.PropertyChanged += Property_PropertyChanged;
                }
                //remove no longer existing
                var nonExisting = _properties.Value.Where(x => names.InvariantContains(x.Name) == false).ToArray();
                foreach (var stylesheetProperty in nonExisting)
                {
                    stylesheetProperty.PropertyChanged -= Property_PropertyChanged;
                    _properties.Value.Remove(stylesheetProperty);
                }
                //add new ones
                var newItems = parsed.Where(x => _properties.Value.Select(p => p.Name).InvariantContains(x.Name) == false);
                foreach (var stylesheetRule in newItems)
                {
                    var prop = new StylesheetProperty(stylesheetRule.Name, stylesheetRule.Selector, stylesheetRule.Styles);
                    prop.PropertyChanged += Property_PropertyChanged;
                    _properties.Value.Add(prop);
                }
            }

            //we haven't read the properties yet so create the lazy delegate
            _properties = new Lazy <List <StylesheetProperty> >(() =>
            {
                var parsed = StylesheetHelper.ParseRules(Content);
                return(parsed.Select(statement =>
                {
                    var property = new StylesheetProperty(statement.Name, statement.Selector, statement.Styles);
                    property.PropertyChanged += Property_PropertyChanged;
                    return property;
                }).ToList());
            });
        }
Example #11
0
        public void StylesheetHelperTests_ParseRules_DoesntParse(string css)
        {
            // Arrange
            var file = new File(new HiveId("styles.css"))
            {
                ContentBytes = Encoding.UTF8.GetBytes(css)
            };

            // Act
            var results = StylesheetHelper.ParseRules(file);

            // Assert
            Assert.IsTrue(results.Count() == 0);
        }
        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());
        }
Example #13
0
        public void StylesheetHelperTests_ParseRules_Parses(string name, string selector, string styles, string css)
        {
            // Arrange
            var file = new File(new HiveId("styles.css"))
            {
                ContentBytes = Encoding.UTF8.GetBytes(css)
            };

            // Act
            var results = StylesheetHelper.ParseRules(file);

            // Assert
            Assert.IsTrue(results.Count() == 1);

            Assert.IsTrue(results.First().RuleId.Value.Value.ToString() == file.Id.Value.Value + "/" + name);
            Assert.IsTrue(results.First().Name == name);
            Assert.IsTrue(results.First().Selector == selector);
            Assert.IsTrue(results.First().Styles == styles);
        }
        protected override void CustomizeFileNode(TreeNode n, FormCollection queryStrings)
        {
            n.AddEditorMenuItem <CreateItem>(this, "createUrl", "EditRule");
            base.CustomizeFileNode(n, queryStrings);
            n.AddMenuItem <Reload>();

            n.Icon = "tree-css";

            using (var uow = Hive.CreateReadonly())
            {
                var stylesheet = uow.Repositories.Get <File>(n.HiveId);
                var rules      = StylesheetHelper.ParseRules(stylesheet);
                n.HasChildren = rules.Count() > 0;
                if (n.HasChildren)
                {
                    n.JsonUrl = Url.GetTreeUrl(GetType(), n.HiveId, queryStrings);
                }
            }
        }