Exemplo n.º 1
0
        //    /**
        //     * Prepare to submit this form. A Connection object is created with the request set up from the form values. You
        //     * can then set up other options (like user-agent, timeout, cookies), then execute it.
        //     * @return a connection prepared from the values of this form.
        //     * @throws IllegalArgumentException if the form's absolute action URL cannot be determined. Make sure you pass the
        //     * document's base URI when parsing.
        //     */
        //    public Connection submit() {
        //        String action = hasAttr("action") ? absUrl("action") : baseUri();
        //        Validate.notEmpty(action, "Could not determine a form action URL for submit. Ensure you set a base URI when parsing.");
        //        Connection.Method method = attr("method").toUpperCase().equals("POST") ?
        //                Connection.Method.POST : Connection.Method.GET;
        //
        //        return Jsoup.connect(action)
        //                .data(formData())
        //                .method(method);
        //    }
        /// <summary>Get the data that this form submits.</summary>
        /// <remarks>
        /// Get the data that this form submits. The returned list is a copy of the data, and changes to the contents of the
        /// list will not be reflected in the DOM.
        /// </remarks>
        /// <returns>a list of key vals</returns>
        public virtual IList <KeyVal> FormData()
        {
            List <KeyVal> data = new List <KeyVal>();

            // iterate the form control elements and accumulate their values
            foreach (iText.StyledXmlParser.Jsoup.Nodes.Element el in elements)
            {
                if (!el.Tag().IsFormSubmittable())
                {
                    continue;
                }
                // contents are form listable, superset of submitable
                if (el.HasAttr("disabled"))
                {
                    continue;
                }
                // skip disabled form inputs
                String name = el.Attr("name");
                if (name.Length == 0)
                {
                    continue;
                }
                String type = el.Attr("type");
                if ("select".Equals(el.TagName()))
                {
                    iText.StyledXmlParser.Jsoup.Select.Elements options = el.Select("option[selected]");
                    bool set = false;
                    foreach (iText.StyledXmlParser.Jsoup.Nodes.Element option in options)
                    {
                        data.Add(KeyVal.Create(name, option.Val()));
                        set = true;
                    }
                    if (!set)
                    {
                        iText.StyledXmlParser.Jsoup.Nodes.Element option = el.Select("option").First();
                        if (option != null)
                        {
                            data.Add(KeyVal.Create(name, option.Val()));
                        }
                    }
                }
                else
                {
                    if ("checkbox".EqualsIgnoreCase(type) || "radio".EqualsIgnoreCase(type))
                    {
                        // only add checkbox or radio if they have the checked attribute
                        if (el.HasAttr("checked"))
                        {
                            String val = el.Val().Length > 0 ? el.Val() : "on";
                            data.Add(KeyVal.Create(name, val));
                        }
                    }
                    else
                    {
                        data.Add(KeyVal.Create(name, el.Val()));
                    }
                }
            }
            return(data);
        }