Jaa har nedanstående kod för att fïxa min sida XHTML validerad men den tar även bort alla åäö :(
Så nu tänkte jag att VI skulle fixa det :) , men jag är ju inte den bästa på RegExp så VI får hjälpas åt ;)
[redigerat]Jag tror jag hittade felet(fetmarkerad rad), men jag vet inte vilken encoding jag ska använda[/redigerat]
[red]
/*
* Written by Mark Pasternak, [email]mark.pasternak@universum.se[/email]
* Feel free to modify
*/
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace MarkPasternak.Helper
{
/// <summary>
/// Response Filter that modifies the ASP.NET output so it can validate against the XHTML 1.1 Strict standard.
/// Two things are modified:
/// 1. The form name attribute is removed. Warning, this may break JavaScript code!
/// 2. The viewstate is wrapped in a div element.
/// </summary>
public class XHTMLStrictFilter : Stream
{
private Stream _sink;
private long _position;
private static Regex _regForm = new Regex("<form .*name=.* .*>", RegexOptions.Compiled|RegexOptions.IgnoreCase);
private static Regex _regNameReplace = new Regex("name=[^ ]*", RegexOptions.Compiled|RegexOptions.IgnoreCase);
private static Regex _regViewState = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\" value=\".*\" />", RegexOptions.Compiled|RegexOptions.IgnoreCase);
private bool _formFix;
private bool _viewStateFix;
public XHTMLStrictFilter(Stream sink)
{
_sink = sink;
}
// The following members of Stream must be overriden.
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override long Length
{
get { return 0; }
}
public override long Position
{
get { return _position; }
set { _position = value; }
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
return _sink.Seek(offset, direction);
}
public override void SetLength(long length)
{
_sink.SetLength(length);
}
public override void Close()
{
_sink.Close();
}
public override void Flush()
{
_sink.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return _sink.Read(buffer, offset, count);
}
// The Write method actually does the filtering.
public override void Write(byte[] buffer, int offset, int count)
{
byte[] data;
if(!_formFix || !_viewStateFix)
{
[b]string htmlOutput = System.Text.UTF8Encoding.UTF8.GetString(buffer); [/b]
int length = htmlOutput.Length;
if(!_formFix && _regForm.IsMatch(htmlOutput))
{
string formMatch=_regForm.Match(htmlOutput).Value;
htmlOutput=Regex.Replace(htmlOutput, formMatch, _regNameReplace.Replace(formMatch, ""), RegexOptions.IgnoreCase);
_formFix=true;
}
if(!_viewStateFix && _regViewState.IsMatch(htmlOutput))
{
string viewStateMatch=_regViewState.Match(htmlOutput).Value;
htmlOutput=Regex.Replace(htmlOutput, viewStateMatch, "<div>"+viewStateMatch+"</div>", RegexOptions.IgnoreCase);
_viewStateFix=true;
}
data = System.Text.UTF8Encoding.UTF8.GetBytes(htmlOutput);
count -= length - htmlOutput.Length;
}
else
{
data = new byte[count];
Buffer.BlockCopy(buffer, offset, data, 0, count);
}
_sink.Write(data, 0, count);
}
}
}
[/red]
mvh icaaq