コード例 #1
0
        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);
        }
コード例 #2
0
        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;
        }