Browse Source

Fixed html encoding bug with

umlauts and special characters
pull/1263/head
AlexejLiebenthal 9 years ago
parent
commit
25cce2a0d6
  1. 32
      ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/Core/Html/HtmlEncodedTextWriter.cs
  2. 2
      ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/Core/Html/HtmlFromXamlConverter.cs
  3. 4
      ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/RichTextBox/Formatters/XamlFormatter.cs
  4. 1
      ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.csproj
  5. 8
      ExtendedWPFToolkitSolution/Tests/Xceed.Wpf.Toolkit.Tests/RichTextBox/Formatters/HtmlFormatterTests.cs

32
ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/Core/Html/HtmlEncodedTextWriter.cs

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;
namespace Xceed.Wpf.Toolkit.Core.Html
{
public class HtmlEncodedTextWriter : XmlTextWriter
{
private readonly XmlTextWriter _textWriter;
public HtmlEncodedTextWriter(TextWriter w) : base(w)
{
_textWriter = this;
}
#region Overrides of XmlTextWriter
/// <inheritdoc />
public override void WriteString(string text)
{
text = WebUtility.HtmlEncode(text);
_textWriter.WriteRaw(text);
}
#endregion
}
}

2
ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/Core/Html/HtmlFromXamlConverter.cs

@ -43,7 +43,7 @@ namespace Xceed.Wpf.Toolkit.Core.Html
xamlReader = new XmlTextReader(new StringReader(xamlString));
htmlStringBuilder = new StringBuilder(100);
htmlWriter = new XmlTextWriter(new StringWriter(htmlStringBuilder));
htmlWriter = new HtmlEncodedTextWriter(new StringWriter(htmlStringBuilder));
if (!WriteFlowDocument(xamlReader, htmlWriter))
{

4
ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/RichTextBox/Formatters/XamlFormatter.cs

@ -33,7 +33,7 @@ namespace Xceed.Wpf.Toolkit
using( MemoryStream ms = new MemoryStream() )
{
tr.Save( ms, DataFormats.Xaml );
return ASCIIEncoding.Default.GetString( ms.ToArray() );
return Encoding.UTF8.GetString( ms.ToArray() );
}
}
@ -50,7 +50,7 @@ namespace Xceed.Wpf.Toolkit
else
{
TextRange tr = new TextRange( document.ContentStart, document.ContentEnd );
using( MemoryStream ms = new MemoryStream( Encoding.ASCII.GetBytes( text ) ) )
using( MemoryStream ms = new MemoryStream( Encoding.UTF8.GetBytes( text ) ) )
{
tr.Load( ms, DataFormats.Xaml );
}

1
ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.csproj

@ -395,6 +395,7 @@
<Compile Include="Core\Converters\ColorModeToTabItemSelectedConverter.cs" />
<Compile Include="Core\Html\CssStylesheet.cs" />
<Compile Include="Core\Html\HtmlCSSParser.cs" />
<Compile Include="Core\Html\HtmlEncodedTextWriter.cs" />
<Compile Include="Core\Html\HtmlFromXamlConverter.cs" />
<Compile Include="Core\Html\HtmlLexicalAnalyzer.cs" />
<Compile Include="Core\Html\HtmlParser.cs" />

8
ExtendedWPFToolkitSolution/Tests/Xceed.Wpf.Toolkit.Tests/RichTextBox/Formatters/HtmlFormatterTests.cs

@ -16,14 +16,14 @@ namespace Xceed.Wpf.Toolkit.Tests
public FlowDocument CreateSimpleFlowDocument()
{
var r = new Run("Hallo Welt!");
var r = new Run("Hallo Welt! ÄÖÜ äöü ^ \" ß ' & <>");
var b = new Bold(r);
var p = new Paragraph(b);
return new FlowDocument(p);
}
public string CreateSimpleHtml() =>
"<html><body><p><b>Hallo Welt!</b></p><body></html>";
"<html><body><p><b>Hallo Welt! &#196;&#214;&#220; &#228;&#246;&#252; ^ &quot; &#223; &#39; &amp; &lt;&gt;</b></p><body></html>";
[Test]
public void GetText_ShouldReturnAHtmlText()
@ -33,7 +33,7 @@ namespace Xceed.Wpf.Toolkit.Tests
var actual = sut.GetText(doc);
const string expectedText = "Hallo Welt!";
const string expectedText = "Hallo Welt! &#196;&#214;&#220; &#228;&#246;&#252; ^ &quot; &#223; &#39; &amp; &lt;&gt;";
const string expectedBoldStyle = "bold";
const string expectedHtmlTag = "<html>";
@ -56,7 +56,7 @@ namespace Xceed.Wpf.Toolkit.Tests
var actual = new XamlFormatter().GetText(doc);
const string expectedText = "Hallo Welt!";
const string expectedText = "Hallo Welt! ÄÖÜ äöü ^ \" ß ' &amp; &lt;&gt;";
const string expectedBoldStyle = "Bold";
const string expectedSection = "Section";

Loading…
Cancel
Save