[Fat-develop] FAT/src/FAT.Web/HtmlDetails HtmlDetailsEnhancer.cs,1.1,1.2
Brought to you by:
exortech
|
From: <dmc...@pr...> - 2004-02-01 12:29:08
|
Update of /cvsroot/fat/FAT/src/FAT.Web/HtmlDetails In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18067/src/FAT.Web/HtmlDetails Modified Files: HtmlDetailsEnhancer.cs Log Message: Relative urls in html is now expanded so that <img src="url" >, <a href="url" >, <form action="url" > etc. work in thumbnail creation and in IE pop-up when thumbnail is clicked. Introduced "view source" link so that original, unmodified html source is still accessible. Index: HtmlDetailsEnhancer.cs =================================================================== RCS file: /cvsroot/fat/FAT/src/FAT.Web/HtmlDetails/HtmlDetailsEnhancer.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** HtmlDetailsEnhancer.cs 28 Jan 2004 22:14:49 -0000 1.1 --- HtmlDetailsEnhancer.cs 1 Feb 2004 12:27:15 -0000 1.2 *************** *** 1,3 **** --- 1,4 ---- using System; + using System.Text.RegularExpressions; using System.Drawing; using System.IO; *************** *** 8,12 **** public class HtmlDetailsEnhancer { ! private const int screenWidth = 1400; private const int screenHeight = 1050; private const int scalingFactor = 8; --- 9,13 ---- public class HtmlDetailsEnhancer { ! private const int screenWidth = 1050; private const int screenHeight = 1050; private const int scalingFactor = 8; *************** *** 35,40 **** public string HtmlWithAbsoluteUrls { ! // TODO: expand all urls in the html to be absolute url so that the page functions correctly (in most cases) when fetched from the FAT site. ! get {return html;} } --- 36,52 ---- public string HtmlWithAbsoluteUrls { ! // here we expand urls in the html to be absolute urls so that the page functions correctly when ! // fetched from the FAT site or when viewed as a thumbnail. This means expanding relative urls to be ! // absolute for all <img src="">, <form action="">, <a href=""> etc. ! // it's not perfect - just good enough - urls embedded in javascript etc. are not caught, ! // RegExes works only where attribute values is enclosed in "". ! get ! { ! string expandingHtml = html; ! expandingHtml = ExpandRelativeUrls(expandingHtml, "form", "action"); ! expandingHtml = ExpandRelativeUrls(expandingHtml, "img", "src"); ! expandingHtml = ExpandRelativeUrls(expandingHtml, "a", "img"); ! return expandingHtml; ! } } *************** *** 44,51 **** } private Image CreateImage() { string temporaryFileName = GetTemporaryFileName(); ! WriteHtmlToFile(temporaryFileName); Image image = CreateImageFromFile(temporaryFileName); DeleteTemporaryFile(temporaryFileName); --- 56,83 ---- } + public string UrlRoot { get { return Regex.Replace(url, "(http://.*)/.*$", "$1"); }} + public string UrlHost { get { return Regex.Replace(url, "(http://.*?)/.*", "$1"); }} + + private string ExpandRelativeUrls(string html, string element, string attribute) + { + return ExpandHostRelativeUrls(ExpandFolderRelativeUrls(html, element, attribute), element, attribute); + } + + private string ExpandFolderRelativeUrls(string html, string element, string attribute) + { + // Example: Default.aspx => http://localhost/FAT.Web/Default.aspx + return Regex.Replace(html, "(<" + element + " .*?" + attribute + @"\s*=\s*"")((?(http://|/)no).*?"".*?>)", "$1" + UrlRoot + "/$2", RegexOptions.Singleline); + } + + private string ExpandHostRelativeUrls(string html, string element, string attribute) + { + // Example: /FAT.Web/Default.aspx => http://localhost/FAT.Web/Default.aspx + return Regex.Replace(html, "(<" + element + " .*?" + attribute + @"\s*=\s*"")((?(http://)no).*?"".*?>)", "$1" + UrlHost + "$2", RegexOptions.Singleline); + } + private Image CreateImage() { string temporaryFileName = GetTemporaryFileName(); ! WriteHtmlWithAbsoluteUrlsToFile(temporaryFileName); Image image = CreateImageFromFile(temporaryFileName); DeleteTemporaryFile(temporaryFileName); *************** *** 58,65 **** } ! private void WriteHtmlToFile(string fileName) { StreamWriter writer = new StreamWriter(fileName); ! writer.Write(html); writer.Close(); } --- 90,97 ---- } ! private void WriteHtmlWithAbsoluteUrlsToFile(string fileName) { StreamWriter writer = new StreamWriter(fileName); ! writer.Write(HtmlWithAbsoluteUrls); writer.Close(); } |