Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding issue #158

Closed
Sicos1977 opened this issue Jan 25, 2019 · 4 comments
Closed

Encoding issue #158

Sicos1977 opened this issue Jan 25, 2019 · 4 comments

Comments

@Sicos1977
Copy link

Sicos1977 commented Jan 25, 2019

Hi,

Is it somehow possible to let HtmlSanitizer detect the encoding of the string that it is sanitizing?

I now load the file with File.ReadAllText and then feed it to the .Sanitize method but after that words like kopieën end up like kopiee?n

In this case there is encoding information in the header of the html file.
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

When that encoding is used then everything is oke

@mganss
Copy link
Owner

mganss commented Jan 25, 2019

Can you post a demo file?

@Sicos1977
Copy link
Author

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;
	mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
p
	{mso-style-priority:99;
	mso-margin-top-alt:auto;
	margin-right:0cm;
	mso-margin-bottom-alt:auto;
	margin-left:0cm;
	font-size:12.0pt;
	font-family:"Times New Roman",serif;}
p.msonormal0, li.msonormal0, div.msonormal0
	{mso-style-name:msonormal;
	mso-style-priority:99;
	mso-margin-top-alt:auto;
	margin-right:0cm;
	mso-margin-bottom-alt:auto;
	margin-left:0cm;
	font-size:12.0pt;
	font-family:"Times New Roman",serif;}
span.E-mailStijl19
	{mso-style-type:personal;
	font-family:"Calibri",sans-serif;
	color:windowtext;}
span.E-mailStijl20
	{mso-style-type:personal;
	font-family:"Calibri",sans-serif;
	color:#1F497D;}
span.E-mailStijl21
	{mso-style-type:personal-reply;
	font-family:"Calibri",sans-serif;
	color:#1F497D;}
.MsoChpDefault
	{mso-style-type:export-only;
	font-size:10.0pt;}
@page WordSection1
	{size:612.0pt 792.0pt;
	margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="NL" link="blue" vlink="purple">
<div class="WordSection1">

<p style="margin-left:35.4pt"><span style="font-size:8.0pt;font-family:&quot;Arial&quot;,sans-serif">*****</span><o:p></o:p></p>
<p style="margin-left:35.4pt"><i><span style="font-size:8.0pt;font-family:&quot;Arial&quot;,sans-serif">kopieën</span></i><o:p></o:p></p>
<p style="margin-left:35.4pt"><span style="font-size:8.0pt;font-family:&quot;Arial&quot;,sans-serif">*****</span><o:p></o:p></p>
</div>
</body>
</html>

@mganss
Copy link
Owner

mganss commented Jan 26, 2019

This is not an issue of HtmlSanitizer. When you read an ISO-8859-1 encoded file using File.ReadAllText without specifying the encoding, the returned string will contain the 0xfffd "replacement character" � instead of "ë". At this point, the information is lost and can't be fixed afterwards. Also, keep in mind that a string in C# doesn't have an encoding that you can change, it's always UTF-16 internally.

I'm not sure if AngleSharp does encoding detection if you supply a byte stream to its HTML parser. I'll try and possibly add overloads of Sanitize that take a Stream instead of a string.

@mganss mganss closed this as completed in 11b2716 Jan 27, 2019
@mganss
Copy link
Owner

mganss commented Jan 27, 2019

In 4.0.205 there is now an overload of SanitizeDocument that takes a Stream. You can use it like so to enable your use case:

using (var stream = File.OpenRead("path/to/your/file"))
{
    var sanitized = sanitizer.SanitizeDocument(stream);
    // ...
}

If you're on .NET Core, you need to add the System.Text.Encoding.CodePages NuGet package and call Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); before sanitizing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants