1. Jag använda följande kod för få min sida validerad som XHTML:
[kod]
/*
* Written by Mark Pasternak, [email protected]
* Feel free to modify
*/
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace Consolidate
{
/// <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 Rege
"<form .*name=.* .*>", RegexOptions.Compiled|RegexOptions.IgnoreCase);
private static Regex _regNameReplace = new Rege
"name=[^ ]*", RegexOptions.Compiled|RegexOptions.IgnoreCase);
private static Regex _regViewState = new Rege
"<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)
{
string htmlOutput = System.Text.UTF8Encoding.UTF8.GetString(buffer);
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);
}
}
}
[/kod]
Den ska bl.a. ta bort name="foo" ur alla form-taggar. Problemet är att det inte funkar om action-attributet innehåller ett frågetecken:
[kod]
<!-- Funkar -->
<form name="foo" action="test.html">
<!-- Funkar inte -->
<form name="foo" action="test.html?var=1">
[/kod]
Hur kan man lösa det?
2. Vad jag förstår går det att lägga till ovanstående klass i global.asax så att alla sidor körs genom XHTML-filtrer. Hur gör jag det?
[kod]
/*
* Written by Mark Pasternak, [email protected]
* Feel free to modify
*/
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace Consolidate
{
/// <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 Rege
"<form .*name=.* .*>", RegexOptions.Compiled|RegexOptions.IgnoreCase); private static Regex _regNameReplace = new Rege
"name=[^ ]*", RegexOptions.Compiled|RegexOptions.IgnoreCase); private static Regex _regViewState = new Rege
"<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)
{
string htmlOutput = System.Text.UTF8Encoding.UTF8.GetString(buffer);
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);
}
}
}
[/kod]
Den ska bl.a. ta bort name="foo" ur alla form-taggar. Problemet är att det inte funkar om action-attributet innehåller ett frågetecken:
[kod]
<!-- Funkar -->
<form name="foo" action="test.html">
<!-- Funkar inte -->
<form name="foo" action="test.html?var=1">
[/kod]
Hur kan man lösa det?
2. Vad jag förstår går det att lägga till ovanstående klass i global.asax så att alla sidor körs genom XHTML-filtrer. Hur gör jag det?


och svaret låg i den här raden.
Comment