2012-10-04 13:05:31 -02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2012-10-04 11:44:31 -02:00
|
|
|
|
using System.Linq;
|
2012-10-04 13:05:31 -02:00
|
|
|
|
using System.Runtime.Serialization;
|
2012-10-24 06:42:04 -02:00
|
|
|
|
using System.Text;
|
2012-10-04 11:44:31 -02:00
|
|
|
|
using Umbraco.Core.IO;
|
|
|
|
|
|
using Umbraco.Core.Models.Css;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Models
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents a Stylesheet file
|
|
|
|
|
|
/// </summary>
|
2012-10-04 13:05:31 -02:00
|
|
|
|
[Serializable]
|
|
|
|
|
|
[DataContract(IsReference = true)]
|
2012-10-04 11:44:31 -02:00
|
|
|
|
public class Stylesheet : File
|
|
|
|
|
|
{
|
|
|
|
|
|
public Stylesheet(string path) : base(path)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Path = path;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-10-24 06:42:04 -02:00
|
|
|
|
/// Returns a flat list of <see cref="StylesheetProperty"/> objects
|
2012-10-04 11:44:31 -02:00
|
|
|
|
/// </summary>
|
2012-10-24 06:42:04 -02:00
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// Please note that the list is flattend by formatting single css selectors with
|
|
|
|
|
|
/// its value(s). Blocks in css @ rules are also flatten, but noted as part of an @ rule
|
|
|
|
|
|
/// by setting the <see cref="StylesheetProperty"/> property IsPartOfAtRule=true.
|
|
|
|
|
|
/// This is done to make the stylesheet usable in the backoffice.
|
|
|
|
|
|
/// </remarks>
|
2012-10-04 13:05:31 -02:00
|
|
|
|
[IgnoreDataMember]
|
2012-10-04 11:44:31 -02:00
|
|
|
|
public IEnumerable<StylesheetProperty> Properties
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
var properties = new List<StylesheetProperty>();
|
2012-10-11 13:11:17 -02:00
|
|
|
|
var parser = new CssParser(Content);
|
2012-10-04 11:44:31 -02:00
|
|
|
|
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
foreach (var statement in parser.StyleSheet.Statements.OfType<CssAtRule>())
|
2012-10-04 11:44:31 -02:00
|
|
|
|
{
|
2012-10-24 06:42:04 -02:00
|
|
|
|
var cssBlock = statement.Block;
|
|
|
|
|
|
if(cssBlock == null) continue;
|
2012-10-04 11:44:31 -02:00
|
|
|
|
|
2012-10-24 06:42:04 -02:00
|
|
|
|
var cssValues = cssBlock.Values;
|
|
|
|
|
|
if(cssValues == null) continue;
|
|
|
|
|
|
|
|
|
|
|
|
properties.AddRange(FormatCss(cssBlock.Values, true));
|
2012-10-04 11:44:31 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-24 06:42:04 -02:00
|
|
|
|
var statements = parser.StyleSheet.Statements.Where(s => s is CssRuleSet);
|
|
|
|
|
|
properties.AddRange(FormatCss(statements, false));
|
|
|
|
|
|
|
2012-10-04 11:44:31 -02:00
|
|
|
|
return properties;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-24 06:42:04 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Formats a list of statements to a simple <see cref="StylesheetProperty"/> object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="statements">Enumerable list of <see cref="ICssValue"/> statements</param>
|
|
|
|
|
|
/// <param name="isPartOfAtRule">Boolean indicating whether the current list of statements is part of an @ rule</param>
|
|
|
|
|
|
/// <returns>An Enumerable list of <see cref="StylesheetProperty"/> objects</returns>
|
|
|
|
|
|
private IEnumerable<StylesheetProperty> FormatCss(IEnumerable<ICssValue> statements, bool isPartOfAtRule)
|
|
|
|
|
|
{
|
|
|
|
|
|
var properties = new List<StylesheetProperty>();
|
|
|
|
|
|
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
foreach (var statement in statements.OfType<CssRuleSet>())
|
2012-10-24 06:42:04 -02:00
|
|
|
|
{
|
|
|
|
|
|
foreach (var selector in statement.Selectors)
|
|
|
|
|
|
{
|
|
|
|
|
|
var declarations = new StringBuilder();
|
|
|
|
|
|
foreach (var declaration in statement.Declarations)
|
|
|
|
|
|
{
|
|
|
|
|
|
declarations.AppendFormat("{0}:{1};", declaration.Property, FormatCss(declaration.Value));
|
|
|
|
|
|
declarations.AppendLine("");
|
|
|
|
|
|
}
|
|
|
|
|
|
properties.Add(new StylesheetProperty(selector.Value.TrimStart('.', '#'), declarations.ToString()) { IsPartOfAtRule = isPartOfAtRule });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return properties;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Formats a <see cref="CssValueList"/> to a single string
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="valueList"><see cref="CssValueList"/> to format</param>
|
|
|
|
|
|
/// <returns>Value list formatted as a string</returns>
|
|
|
|
|
|
private string FormatCss(CssValueList valueList)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool space = false;
|
|
|
|
|
|
var values = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (CssString value in valueList.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (space)
|
|
|
|
|
|
{
|
|
|
|
|
|
values.Append(" ");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
space = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
values.Append(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return values.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-04 11:44:31 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Boolean indicating whether the file could be validated
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if file is valid, otherwise false</returns>
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
//TODO: This makes no sense to be here, any validation methods should be at the service level,
|
|
|
|
|
|
// when we move Scripts to truly use IFileSystem, then this validation logic doesn't work anymore
|
2012-10-04 11:44:31 -02:00
|
|
|
|
public override bool IsValid()
|
|
|
|
|
|
{
|
|
|
|
|
|
var dirs = SystemDirectories.Css;
|
|
|
|
|
|
|
|
|
|
|
|
//Validate file
|
2013-02-06 13:25:27 -01:00
|
|
|
|
var validFile = IOHelper.VerifyEditPath(Path, dirs.Split(','));
|
2012-10-04 11:44:31 -02:00
|
|
|
|
|
|
|
|
|
|
//Validate extension
|
2013-02-06 13:25:27 -01:00
|
|
|
|
var validExtension = IOHelper.VerifyFileExtension(Path, new List<string> {"css"});
|
2012-10-04 11:44:31 -02:00
|
|
|
|
|
|
|
|
|
|
return validFile && validExtension;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Boolean indicating whether the file is valid css using a css parser
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if css is valid, otherwise false</returns>
|
|
|
|
|
|
public bool IsFileValidCss()
|
|
|
|
|
|
{
|
2012-10-24 06:42:04 -02:00
|
|
|
|
var parser = new CssParser(Content);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var styleSheet = parser.StyleSheet;//Get stylesheet to invoke parsing
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
//Log exception?
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return !parser.Errors.Any();
|
2012-10-04 11:44:31 -02:00
|
|
|
|
}
|
2013-01-29 12:16:13 -01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Indicates whether the current entity has an identity, which in this case is a path/name.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// Overrides the default Entity identity check.
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public override bool HasIdentity
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return string.IsNullOrEmpty(Path) == false; }
|
|
|
|
|
|
}
|
2012-10-04 11:44:31 -02:00
|
|
|
|
}
|
|
|
|
|
|
}
|