예제 #1
0
        /// <summary>
        /// Set the field values of given form by using the dictionary which
        /// contains name value pairs of input fields.
        /// </summary>
        /// <param name="form">The form to set.</param>
        /// <param name="fields">The fields to use as values.</param>
        /// <param name="createMissing">
        /// What to do if some field(s) have not been found in the form. If
        /// true, then new input will be created. Otherwise, an exception will
        /// be thrown.
        /// </param>
        /// <returns>The given form for chaining.</returns>
        public static IHtmlFormElement SetValues(this IHtmlFormElement form, IDictionary <String, String> fields, Boolean createMissing = false)
        {
            if (form == null)
            {
                throw new ArgumentNullException(nameof(form));
            }

            if (fields == null)
            {
                throw new ArgumentNullException(nameof(fields));
            }

            var inputs = form.Elements.OfType <HtmlFormControlElement>();

            foreach (var field in fields)
            {
                var targetInput = inputs.FirstOrDefault(e => e.Name.Is(field.Key));

                if (targetInput != null)
                {
                    if (targetInput is IHtmlInputElement)
                    {
                        var input = (IHtmlInputElement)targetInput;

                        if (input.Type.Is(InputTypeNames.Radio))
                        {
                            var radios = inputs.OfType <IHtmlInputElement>().Where(i => i.Name.Is(targetInput.Name));

                            foreach (var radio in radios)
                            {
                                radio.IsChecked = radio.Value.Is(field.Value);
                            }
                        }
                        else
                        {
                            input.Value = field.Value;
                        }
                    }
                    else if (targetInput is IHtmlTextAreaElement)
                    {
                        var textarea = (IHtmlTextAreaElement)targetInput;
                        textarea.Value = field.Value;
                    }
                    else if (targetInput is IHtmlSelectElement)
                    {
                        var select = (IHtmlSelectElement)targetInput;
                        select.Value = field.Value;
                    }
                    else
                    {
                        //Silently ignore unsupported input type, e.g.,
                        //no idea if modifying keygen fields is really
                        //useful or how it is regulated.
                    }
                }
                else if (createMissing)
                {
                    var newInput = form.Owner.CreateElement <IHtmlInputElement>();
                    newInput.Type  = InputTypeNames.Hidden;
                    newInput.Name  = field.Key;
                    newInput.Value = field.Value;
                    form.AppendChild(newInput);
                }
                else
                {
                    var message = $"Field {field.Key} not found.";
                    throw new KeyNotFoundException(message);
                }
            }

            return(form);
        }