From: Dion O. <dol...@us...> - 2005-12-08 01:43:19
|
Update of /cvsroot/magicajax/magicajax/Core/UI/Controls In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15699/magicajax/Core/UI/Controls Modified Files: AjaxPanel.cs Log Message: Added the ContainsUpdatedFormValues(string html) method, that parses all form input controls from the html, and checks if their values were updated (i.e. different from the current Request.Form values) Note: not 100% tested yet Index: AjaxPanel.cs =================================================================== RCS file: /cvsroot/magicajax/magicajax/Core/UI/Controls/AjaxPanel.cs,v retrieving revision 1.29 retrieving revision 1.30 diff -C2 -d -r1.29 -r1.30 *** AjaxPanel.cs 5 Dec 2005 18:44:31 -0000 1.29 --- AjaxPanel.cs 8 Dec 2005 01:43:11 -0000 1.30 *************** *** 30,33 **** --- 30,34 ---- using System.Globalization; using System.Text; + using System.Text.RegularExpressions; namespace MagicAjax.UI.Controls *************** *** 422,425 **** --- 423,542 ---- /// <summary> + /// Parses all form input controls from the html, and checks if + /// their values were updated (i.e. different from the current Request.Form values) + /// </summary> + /// <param name="html"></param> + protected bool ContainsUpdatedFormValues(string html) + { + NameValueCollection form = Context.Request.Form; + RegexOptions options = RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled; + + #region Check <input> tags + // + // Check <input> tags + // + Regex regEx = new System.Text.RegularExpressions.Regex("<input\\s((?<attrname>[-\\w]+)=\"(?<attrvalue>.*?)\"\\s?)*\\s*/>", options); + MatchCollection matches = regEx.Matches(html); + for (int i=0; i<matches.Count; i++) + { + Match match = matches[i]; + CaptureCollection attrnames = match.Groups["attrname"].Captures; + CaptureCollection attrvalues = match.Groups["attrvalue"].Captures; + + Hashtable attrNameValues = new Hashtable(); + for (int j=0; j< attrnames.Count; j++) + { + attrNameValues.Add(attrnames[j].Value.ToLower(), attrvalues[j].Value); + } + + string type = (string)attrNameValues["type"]; + string name = (string)attrNameValues["name"]; + string value = attrNameValues.ContainsKey("value") ? (string)attrNameValues["value"] : string.Empty; + bool isChecked = attrNameValues.ContainsKey("checked"); + + if (type != null && name != null) + { + switch (type) + { + case "text": + case "password": + if (value != form[name]) + return true; + break; + case "checkbox": + if (isChecked != (form[name] != null)) + return true; + break; + case "radio": + if ((isChecked && form[name] != value) || (!isChecked && form[name] == value)) + return true; + break; + } + } + } + #endregion + + #region Check <textarea> tags + // + // Check <textarea> tags + // + regEx = new System.Text.RegularExpressions.Regex("<textarea\\s.*?name=\"(?<name>[-\\w]+)\".*?>(?<value>.*?)</textarea>", options); + matches = regEx.Matches(html); + for (int i=0; i<matches.Count; i++) + { + Match match = matches[i]; + string name = match.Groups["name"].Value; + string value = match.Groups["value"].Success ? match.Groups["value"].Value : string.Empty; + + if (value != form[name]) + return true; + } + #endregion + + #region Check <select> tags + // + // Check <select> tags + // + regEx = new System.Text.RegularExpressions.Regex("<select\\s.*?name=\"(?<name>[-\\w]+)\".*?(?<multiple>multiple=\"multiple\")?.*>.*?</select>", options); + Regex regExOpt = new System.Text.RegularExpressions.Regex("<option\\s.*?(?<selected>selected=\"selected\")?.*?value=\"(?<value>.*?)\".*?>.*?</option>", options); + matches = regEx.Matches(html); + for (int i=0; i<matches.Count; i++) + { + Match match = matches[i]; + + string name = match.Groups["name"].Value; + bool multiple = match.Groups["multiple"].Success; + + if (name != null) + { + string serverValue = null; + + //now match the options within this <select> tag + MatchCollection matchesOpt = regExOpt.Matches(match.Value); + for (int j = 0; j < matchesOpt.Count; j++) + { + Match matchOpt = matchesOpt[j]; + + string optValue = matchOpt.Groups["value"].Value; //TODO: check value encodings etc. + bool optSelected = matchOpt.Groups["selected"].Success; + + if (optSelected) + { + serverValue += (serverValue == null ? string.Empty : ",") + optValue; + if (!multiple) + break; + } + } + + if (serverValue != form[name]) + return true; + } + } + #endregion + + return false; + } + + /// <summary> /// Determines whether the AjaxPanel should treat the given control as a separate /// 'html holder' to compare and 'reflect' its html rendering. *************** *** 845,863 **** { string htmlFingerprint = Util.GetFingerprint(html); ! ! if (htmlFingerprint == (string)_controlHtmlFingerprints[con]) ! { ! // Its html rendering fingerprint is the same, ignore the ! // html and compare the form elements that it contains ! // with those on client's page. ! ReflectContainedFormControls (con); ! } ! else { // Its html rendering fingerprint has changed, // "reflect" its html on client. ExtendedWriteSetHtmlOfElementScript(html, GetAjaxElemID(con)); ! _controlHtmlFingerprints[con] = htmlFingerprint; } } else --- 962,991 ---- { string htmlFingerprint = Util.GetFingerprint(html); ! ! if (htmlFingerprint != (string)_controlHtmlFingerprints[con] ! || ContainsUpdatedFormValues(html)) { // Its html rendering fingerprint has changed, // "reflect" its html on client. ExtendedWriteSetHtmlOfElementScript(html, GetAjaxElemID(con)); ! _controlHtmlFingerprints[con] = htmlFingerprint; } + + //previous code, commented by Dion + // + //if (htmlFingerprint == (string)_controlHtmlFingerprints[con]) + //{ + // // Its html rendering fingerprint is the same, ignore the + // // html and compare the form elements that it contains + // // with those on client's page. + // ReflectContainedFormControls (con); + //} + //else + //{ + // // Its html rendering fingerprint has changed, + // // "reflect" its html on client. + // ExtendedWriteSetHtmlOfElementScript(html, GetAjaxElemID(con)); + // _controlHtmlFingerprints[con] = htmlFingerprint; + //} } else |