webForumDet fria alternativet

XHTML response

3 svar · 383 visningar · startad av icaaq

icaaqMedlem sedan okt. 20005 273 inlägg
#1

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

BrimbaMedlem sedan dec. 19995 875 inlägg
#2

Vad är ��� ?

red: Jag hittade det, åäö måste det väl vara.

icaaqMedlem sedan okt. 20005 273 inlägg
#3

hmmm Jag vet inte varför mitt inlägg blev som det blev men jag har redigerat det så att det blev läsbart nu iaf.

Men åter till mitt problem jag använder ju "iso-8859-1" som encoding, men hur ska jag anpassa ovanstående kod till det??

icaaqMedlem sedan okt. 20005 273 inlägg
#4

Jag löste det så här

[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) 
			{ 
				string htmlOutput = System.Text.Encoding.Unicode.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.Encoding.Unicode.GetBytes(htmlOutput); 
				count -= length - htmlOutput.Length; 
			} 
			else 
			{ 
				data = new byte[count]; 
				Buffer.BlockCopy(buffer, offset, data, 0, count); 
			} 
			_sink.Write(data, 0, count); 
		} 
	} 
} 
[/red]

Men nu till en andra fråga, jag skulle vilja placera denna class i global.asax så att den körs på varje sida. Men var ska jag lägga den då (i global.asax) ??

mvh icaaq

135 ms totalt · 3 externa anrop · v20260731065814-full.beb2e261
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
133 ms — hämta tråd, inlägg och bilagor (db)