#19426: Updating slugify method to take in to account contiguous dashes

Work Item: 19426

--HG--
branch : 1.x
This commit is contained in:
Nicholas Mayne
2013-06-06 21:59:13 +01:00
parent 9701bc4760
commit d2917b03d8
3 changed files with 41 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Moq;
using NUnit.Framework;
using Orchard.Autoroute.Services;
namespace Orchard.Tests.Modules.Autoroute {
[TestFixture]
public class DefaultSlugServiceTests {
[Test]
public void ShouldStripContiguousDashes() {
DefaultSlugService slugService = new DefaultSlugService(new Mock<ISlugEventHandler>().Object);
Assert.That(slugService.Slugify("a - b"), Is.EqualTo("a-b"));
}
[Test]
public void ShouldStripContiguousDashes2() {
DefaultSlugService slugService = new DefaultSlugService(new Mock<ISlugEventHandler>().Object);
Assert.That(slugService.Slugify("a - - - - - -b"), Is.EqualTo("a-b"));
}
[Test]
public void ShouldStripContiguousDashesEverywhere() {
DefaultSlugService slugService = new DefaultSlugService(new Mock<ISlugEventHandler>().Object);
Assert.That(slugService.Slugify("a - b - c -- d"), Is.EqualTo("a-b-c-d"));
}
}
}

View File

@@ -137,6 +137,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Autoroute\DefaultSlugServiceTests.cs" />
<Compile Include="CodeGeneration\Commands\CodeGenerationCommandsTests.cs" />
<Compile Include="Comments\Services\CommentServiceTests.cs" />
<Compile Include="Email\EmailChannelTests.cs" />
@@ -197,6 +198,10 @@
<Project>{D5D447D7-EF8E-43A6-B9A4-3B025DD9F45D}</Project>
<Name>Lucene</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Modules\Orchard.Autoroute\Orchard.Autoroute.csproj">
<Project>{66fccd76-2761-47e3-8d11-b45d0001ddaa}</Project>
<Name>Orchard.Autoroute</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Modules\Orchard.CodeGeneration\Orchard.CodeGeneration.csproj">
<Project>{C0C45321-B51D-4D8D-9B7B-AA4C2E0B2962}</Project>
<Name>Orchard.CodeGeneration</Name>

View File

@@ -28,7 +28,9 @@ namespace Orchard.Autoroute.Services {
var disallowed = new Regex(@"[/:?#\[\]@!$&'()*+,;=\s\""\<\>\\\|%]+");
slugContext.Slug = disallowed.Replace(slugContext.Title, "-").Trim('-','.');
var cleanedSlug = disallowed.Replace(slugContext.Title, "-").Trim('-','.').Replace("--", "-");
slugContext.Slug = Regex.Replace(cleanedSlug, @"\-{2,}", "-");
if (slugContext.Slug.Length > 1000)
slugContext.Slug = slugContext.Slug.Substring(0, 1000).Trim('-', '.');