private static HtmlFormDefinition PopulateForm(Match formMatch, string pageHtml) { var parsedForm = new HtmlFormDefinition { PageHtml = pageHtml }; foreach (var attribute in Utility.ParseAttributes(formMatch.Groups[RegexLibrary.ParseFormsAttributesGroup].Value)) { parsedForm.attributes.Add(attribute.Key, attribute.Value); } // TODO: need to remove comments from form HTML before parsing out controls; commented out controls will be found! // Populate controls var controlMatches = RegexCache.Instance.Regex(RegexLibrary.ParseFormControls, RegexLibrary.ParseFormControlsOptions).Matches(formMatch.Groups[RegexLibrary.ParseFormsBodyGroup].Value); foreach (Match controlMatch in controlMatches) { HtmlFormControl control; if (controlMatch.Groups[RegexLibrary.ParseFormControlsInputGroup].Value.Length > 0) { var inputControl = new InputHtmlFormControl(controlMatch.Value); if (inputControl.ControlType == InputHtmlFormControlType.Radio) { control = new InputRadioHtmlFormControl(controlMatch.Value); } else if (inputControl.ControlType == InputHtmlFormControlType.CheckBox) { control = new InputCheckBoxHtmlFormControl(controlMatch.Value); } else { // Generic control control = inputControl; } } else if (controlMatch.Groups[RegexLibrary.ParseFormControlsSelectGroup].Value.Length > 0) { control = new SelectHtmlFormControl(controlMatch.Value); } else if (controlMatch.Groups[RegexLibrary.ParseFormControlsTextAreaGroup].Value.Length > 0) { control = new TextAreaHtmlFormControl(controlMatch.Value); } else { throw new System.Net.WebException(string.Format(CultureInfo.CurrentCulture, NScrapeResources.UnsupportedHtmlControl, controlMatch.Value)); } if (control.Name != null) { parsedForm.controls.Add(control); } } return(parsedForm); }
/// <summary> /// Loads the form specified by the provided form definition. /// </summary> /// <param name="formUrl">Contains the URL where the page containing the form resides.</param> /// <param name="formDefinition">Contains the form definition.</param> public void Load(Uri formUrl, HtmlFormDefinition formDefinition) { FormUrl = formUrl; Html = formDefinition.PageHtml; PopulateForm(formDefinition); }
private void Initialize(int formOrdinal) { var formDefinitions = HtmlFormDefinition.Parse(Html).ToList(); if (formOrdinal < 0 || formOrdinal >= formDefinitions.Count) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, NScrapeResources.InvalidFormOrdinal, formOrdinal)); } PopulateForm(formDefinitions.ElementAt(formOrdinal)); }
private void PopulateForm(HtmlFormDefinition formDefinition) { foreach (var attribute in formDefinition.Attributes) { Attributes.Add(attribute.Key, attribute.Value); } foreach (var control in formDefinition.Controls) { Controls.Add(control); } }
private void Initialize(KeyValuePair <string, string> identifyingAttribute) { var formDefinitions = HtmlFormDefinition.Parse(Html).ToList(); var formDefinition = formDefinitions.FirstOrDefault(d => d.Attributes.ContainsKey(identifyingAttribute.Key) && d.Attributes[identifyingAttribute.Key] == identifyingAttribute.Value); if (formDefinition == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, NScrapeResources.InvalidFormId, identifyingAttribute.Key.ToUpperInvariant(), identifyingAttribute.Value)); } PopulateForm(formDefinition); }
private void PopulateForm( HtmlFormDefinition formDefinition ) { foreach ( var attribute in formDefinition.Attributes ) { Attributes.Add( attribute.Key, attribute.Value ); } foreach ( var control in formDefinition.Controls ) { Controls.Add( control ); } }
/// <summary> /// Loads the form specified by the provided form definition. /// </summary> /// <param name="formUrl">Contains the URL where the page containing the form resides.</param> /// <param name="formDefinition">Contains the form definition.</param> public void Load( Uri formUrl, HtmlFormDefinition formDefinition ) { FormUrl = formUrl; Html = formDefinition.PageHtml; PopulateForm( formDefinition ); }
private static HtmlFormDefinition PopulateForm( Match formMatch, string pageHtml ) { var parsedForm = new HtmlFormDefinition { PageHtml = pageHtml }; foreach ( var attribute in Utility.ParseAttributes( formMatch.Groups[RegexLibrary.ParseFormsAttributesGroup].Value ) ) { parsedForm.attributes.Add( attribute.Key, attribute.Value ); } // TODO: need to remove comments from form HTML before parsing out controls; commented out controls will be found! // Populate controls var controlMatches = RegexCache.Instance.Regex( RegexLibrary.ParseFormControls, RegexLibrary.ParseFormControlsOptions ).Matches( formMatch.Groups[RegexLibrary.ParseFormsBodyGroup].Value ); foreach ( Match controlMatch in controlMatches ) { HtmlFormControl control; if ( controlMatch.Groups[RegexLibrary.ParseFormControlsInputGroup].Value.Length > 0 ) { var inputControl = new InputHtmlFormControl( controlMatch.Value ); if ( inputControl.ControlType == InputHtmlFormControlType.Radio ) { control = new InputRadioHtmlFormControl( controlMatch.Value ); } else if ( inputControl.ControlType == InputHtmlFormControlType.CheckBox ) { control = new InputCheckBoxHtmlFormControl( controlMatch.Value ); } else { // Generic control control = inputControl; } } else if ( controlMatch.Groups[RegexLibrary.ParseFormControlsSelectGroup].Value.Length > 0 ) { control = new SelectHtmlFormControl( controlMatch.Value ); } else if ( controlMatch.Groups[RegexLibrary.ParseFormControlsTextAreaGroup].Value.Length > 0 ) { control = new TextAreaHtmlFormControl( controlMatch.Value ); } else { throw new System.Net.WebException( string.Format( CultureInfo.CurrentCulture, NScrapeResources.UnsupportedHtmlControl, controlMatch.Value ) ); } if ( control.Name != null ) { parsedForm.controls.Add( control ); } } return parsedForm; }