Add test showing how to autolink in a post-processing step

This commit is contained in:
Michael Ganss
2015-04-23 16:12:12 +02:00
parent c35f6a33ca
commit 1e5c2fa1dd
3 changed files with 114 additions and 68 deletions

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -35,6 +35,14 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AhoCorasick, Version=1.1.5577.36525, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AhoCorasick.1.1.5577.36525\lib\portable-net40+sl50+win+wpa81+wp80\AhoCorasick.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="AutoLink, Version=1.0.5591.26670, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AutoLink.1.0.5591.26670\lib\portable-net40+sl50+win+wpa81+wp80\AutoLink.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="CsQuery, Version=1.3.3.249, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\CsQuery.1.3.4\lib\net40\CsQuery.dll</HintPath>

View File

@@ -1,3 +1,5 @@
using CsQuery;
using Ganss.Text;
using NUnit.Framework;
using System;
using System.Collections.Generic;
@@ -2122,6 +2124,40 @@ rl(javascript:alert(""foo""))'>";
var html = @"<div>Hallo</div>";
Assert.That(sanitizer.Sanitize(html), Is.EqualTo(@"<div class=""test"">Hallo<b>Test</b></div>").IgnoreCase);
}
[Test]
public void AutoLinkTest()
{
var sanitizer = new HtmlSanitizer();
var autolink = new AutoLink();
sanitizer.PostProcessTag += (s, e) =>
{
var tag = e.Tag;
for (int i = 0; i < tag.ChildNodes.Length; i++)
{
var text = tag.ChildNodes[i] as IDomText;
if (text != null)
{
var autolinked = autolink.Link(text.NodeValue);
if (autolinked != text.NodeValue)
{
var a = CQ.Create(autolinked);
while (a.Document.ChildNodes.Any())
{
tag.ChildNodes.Insert(i, a.Document.ChildNodes.First());
i++;
}
tag.ChildNodes.RemoveAt(i);
i--;
}
}
}
};
var html = @"<div>Click here: http://example.com/.</div>";
Assert.That(sanitizer.Sanitize(html), Is.EqualTo(@"<div>Click here: <a href=""http://example.com/"">http://example.com/</a>.</div>").IgnoreCase);
}
}
}

View File

@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AhoCorasick" version="1.1.5577.36525" targetFramework="net45" />
<package id="AutoLink" version="1.0.5591.26670" targetFramework="net45" />
<package id="CsQuery" version="1.3.4" targetFramework="net45" />
<package id="NUnit" version="2.6.3" targetFramework="net45" />
</packages>