Fixes U4-1512 by using the same safe alias method as in the legacy code base.

This should ensure that published content aliases has the same format as in v4.
This commit is contained in:
Morten Christensen
2013-01-22 08:44:06 -01:00
parent f4574b6014
commit 72ff03f142
4 changed files with 55 additions and 5 deletions

View File

@@ -392,8 +392,7 @@ namespace Umbraco.Core.Models
public static XElement ToXml(this IContent content)
{
//nodeName should match Casing.SafeAliasWithForcingCheck(content.ContentType.Alias);
//var nodeName = content.ContentType.Alias.ToUmbracoAlias(StringAliasCaseType.CamelCase, true);
var nodeName = content.ContentType.Alias;
var nodeName = UmbracoSettings.UseLegacyXmlSchema ? "node" : content.ContentType.Alias.ToSafeAliasWithForcingCheck();
var niceUrl = content.Name.FormatUrl().ToLower();
var xml = new XElement(nodeName,
@@ -412,7 +411,8 @@ namespace Umbraco.Core.Models
new XAttribute("writerName", content.GetWriterProfile().Name),
new XAttribute("creatorName", content.GetCreatorProfile().Name),
new XAttribute("path", content.Path),
new XAttribute("isDoc", ""));
new XAttribute("isDoc", ""),
UmbracoSettings.UseLegacyXmlSchema ? new XAttribute("nodeTypeAlias", content.ContentType.Alias) : null);
foreach (var property in content.Properties)
{

View File

@@ -1,6 +1,7 @@
using System;
using System.Xml;
using System.Xml.Linq;
using Umbraco.Core.Configuration;
namespace Umbraco.Core.Models
{
@@ -13,7 +14,7 @@ namespace Umbraco.Core.Models
/// <returns>Xml of the property and its value</returns>
public static XElement ToXml(this Property property)
{
string nodeName = property.Alias.ToUmbracoAlias(StringAliasCaseType.CamelCase, true);
string nodeName = UmbracoSettings.UseLegacyXmlSchema ? "data" : property.Alias.ToSafeAlias();
var xd = new XmlDocument();
XmlNode xmlNode = xd.CreateNode(XmlNodeType.Element, nodeName, "");

View File

@@ -771,5 +771,54 @@ namespace Umbraco.Core
return newUrl;
}
/// <summary>
/// An extention method to ensure that an Alias string doesn't contains any illegal characters
/// which is defined in a private constant 'ValidCharacters' in this class.
/// Conventions over configuration, baby. You can't touch this - MC Hammer!
/// </summary>
/// <remarks>
/// Copied and cleaned up a bit from umbraco.cms.helpers.Casing.
/// </remarks>
/// <param name="alias">The alias.</param>
/// <returns>An alias guaranteed not to contain illegal characters</returns>
public static string ToSafeAlias(this string alias)
{
const string validAliasCharacters = "_-abcdefghijklmnopqrstuvwxyz1234567890";
const string invalidFirstCharacters = "01234567890";
var safeString = new StringBuilder();
int aliasLength = alias.Length;
for (int i = 0; i < aliasLength; i++)
{
string currentChar = alias.Substring(i, 1);
if (validAliasCharacters.Contains(currentChar.ToLower()))
{
// check for camel (if previous character is a space, we'll upper case the current one
if (safeString.Length == 0 && invalidFirstCharacters.Contains(currentChar.ToLower()))
{
currentChar = "";
}
else
{
if (i < aliasLength - 1 && i > 0 && alias.Substring(i - 1, 1) == " ")
currentChar = currentChar.ToUpper();
safeString.Append(currentChar);
}
}
}
return safeString.ToString();
}
public static string ToSafeAliasWithForcingCheck(this string alias)
{
if (UmbracoSettings.ForceSafeAliases)
{
return alias.ToSafeAlias();
}
return alias;
}
}
}

View File

@@ -53,7 +53,7 @@ namespace Umbraco.Tests.Models
var content = MockedContent.CreateTextpageContent(contentType, "Root Home", -1);
ServiceContext.ContentService.Save(content, 0);
var nodeName = content.ContentType.Alias.ToUmbracoAlias(StringAliasCaseType.CamelCase, true);
var nodeName = content.ContentType.Alias.ToSafeAliasWithForcingCheck();
// Act
XElement element = content.ToXml();