/// <summary> /// Transform each form element to a HtmlFormTag. /// </summary> /// <param name="htmlDoc"> The HTML DOM Document to process.</param> public static HtmlFormTagCollection TransformFormElements(IHTMLDocument2 htmlDoc, Uri currentUri) { // For each form, add in temp FormTags Collection FormConverter converter = new FormConverter(); HtmlFormTagCollection formCollection = new HtmlFormTagCollection(htmlDoc.forms.length); try { foreach ( HTMLFormElementClass formElement in htmlDoc.forms ) { System.Windows.Forms.Application.DoEvents(); // Convert to HTML Form Tag HtmlFormTag form = converter.ConvertToHtmlFormTag(formElement, currentUri); if ( !formCollection.ContainsKey(form.Name) ) { formCollection.Add(form.Name, form); } } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.ToString()); Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManager.Publish(ex); } return formCollection; }
/// <summary> /// Loads the forms into a HtmlFormTagCollection. /// </summary> /// <param name="html"> The parsed HTML content.</param> /// <returns> Returns a HtmlFormTagCollection with the forms contained in the HTML.</returns> public HtmlFormTagCollection LoadForm(string html) { HtmlFormTagCollection forms; try { forms = new HtmlFormTagCollection(); XPathDocument doc; if ( HasForms(html) ) { doc = this.DocumentCache; XPathNavigator nav = doc.CreateNavigator(); #region "Set namespaces" if ( this.NamespaceCache == null) { //create prefix<->namespace mappings (if any) XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable); // resolve namespaces before loading document Hashtable values = ResolveNamespaces(new XmlTextReader(new StringReader(html))); foreach (DictionaryEntry de in values) { nsMgr.AddNamespace((string)de.Key,(string)de.Value); } this.NamespaceCache = nsMgr; } #endregion int i=0; XPathExpression expr = nav.Compile("//form"); expr.SetContext(this.NamespaceCache); // select all form elements XPathNodeIterator nodes = nav.Select(expr); #region Build Form Tag while (nodes.MoveNext()) { HtmlFormTag f = new HtmlFormTag(); f.FormIndex = i; f.Id=nodes.Current.GetAttribute("id",nodes.Current.NamespaceURI); f.Style=nodes.Current.GetAttribute("style",nodes.Current.NamespaceURI); f.Enctype=nodes.Current.GetAttribute("enctype",nodes.Current.NamespaceURI); f.Class=nodes.Current.GetAttribute("class",nodes.Current.NamespaceURI); f.Name=nodes.Current.GetAttribute("name",nodes.Current.NamespaceURI); f.Action=nodes.Current.GetAttribute("action",nodes.Current.NamespaceURI); f.Method=nodes.Current.GetAttribute("method",nodes.Current.NamespaceURI); f.Id=nodes.Current.GetAttribute("id",nodes.Current.NamespaceURI); f.OnSubmit=nodes.Current.GetAttribute("onsubmit",nodes.Current.NamespaceURI); if (f.Action.Length == 0 ) { // add dummy action f.Action = "dummyAction"; } if ( f.Method.Length==0 ) { f.Method="get"; } if ( f.Id!=String.Empty ) { f.Name = f.Id; } if ( forms.ContainsKey(f.Name) ) { forms.Add("_" + f.Name,f); } else { if ( f.Name!=String.Empty ) { forms.Add(f.Name,f); } if (f.Id==String.Empty && f.Name == String.Empty ) { f.Id = "Form " + i; f.Name = "Form " + i; forms.Add(f.Name,f); } } #region " Loop thru descendants from Form" // Select descendants, childs XPathNodeIterator items = nodes.Current.SelectDescendants(XPathNodeType.Element,true); int autoId = 0; while ( items.MoveNext() ) { // if exists, add to same HtmlTagBaseList type // else create new string name = items.Current.GetAttribute("name",items.Current.NamespaceURI); string id = items.Current.GetAttribute("id",items.Current.NamespaceURI); string onclick = items.Current.GetAttribute("onclick",items.Current.NamespaceURI); if ( onclick == String.Empty ) { onclick = items.Current.GetAttribute("onClick",items.Current.NamespaceURI); } // prioritize name use // else use id if ( name == String.Empty ) { name = id; // if no id, generate one if ( name == String.Empty ) { id = autoId.ToString(); name = id; } } switch ( items.Current.Name ) { case "div": if ( onclick != String.Empty ) { AddCommonTag(f,onclick,id,name); } break; case "span": if ( onclick != String.Empty ) { AddCommonTag(f,onclick,id,name); } break; case "a": HtmlALinkTag a = CreateLinkTag(items.Current); if ( f.ContainsKey(name) ) { // just add the value HtmlTagBaseList array = ((HtmlTagBaseList)f[name]); array.Add(a); } else { HtmlTagBaseList list = new HtmlTagBaseList(); list.Add(a); f.Add("a" + autoId.ToString(),list); } break; case "input": // if exists, add to same HtmlTagBaseList type // else create new // verify by name and type HtmlInputTag input = FillInputTag(items.Current); input.Name = name; if ( f.ContainsKey(name) ) { // just add the value HtmlTagBaseList array = ((HtmlTagBaseList)f[name]); array.Add(input); } else { HtmlTagBaseList list = new HtmlTagBaseList(); //HtmlInputTag input = FillInputTag(items.Current); list.Add(input); f.Add(input.Name,list); } break; case "button": // if exists, add to same HtmlTagBaseList type // else create new // verify by name HtmlButtonTag button = FillButtonTag(items.Current); button.Name = name; if ( f.ContainsKey(name) ) { // just add the value HtmlTagBaseList array = ((HtmlTagBaseList)f[name]); //HtmlButtonTag button = FillButtonTag(items.Current); array.Add(button); } else { HtmlTagBaseList buttonList = new HtmlTagBaseList(); buttonList.Add(button); f.Add(button.Name,buttonList); } break; case "select": // if exists, add to same HtmlTagBaseList type // else create new // verify by name HtmlSelectTag select = CreateSelectTag(items.Current); select.Name = name; if ( f.ContainsKey(name) ) { HtmlTagBaseList array = ((HtmlTagBaseList)f[name]); array.Add(select); } else { HtmlTagBaseList selectList = new HtmlTagBaseList(); //HtmlSelectTag select = FillSelectTag(items.Current); selectList.Add(select); f.Add(select.Name,selectList); } break; case "textarea": // if exists, add to same HtmlTagBaseList type // else create new // verify by name HtmlTextAreaTag textarea = FillTextAreaTag(items.Current); textarea.Name = name; if ( f.ContainsKey(name) ) { HtmlTagBaseList array = ((HtmlTagBaseList)f[name]); //HtmlTextAreaTag textarea = FillTextAreaTag(items.Current); array.Add(textarea); } else { HtmlTagBaseList textAreaList = new HtmlTagBaseList(); textAreaList.Add(textarea); f.Add(textarea.Name,textAreaList); } break; } // increase autoId++; } i++; #endregion } #endregion } } catch { throw; } return forms; }