Esempio n. 1
0
        /// <summary>Gets the selected input by the given name attribute.
        /// E.g. there may be more than one input element (such as with radios); this is the active one.</summary>
        public HtmlElement getField(string name)
        {
            NodeList allWithName = getElementsByAttribute("name", name);

            if (allWithName.length == 0)
            {
                return(null);
            }

            HtmlInputElement tag = allWithName[0] as HtmlInputElement;

            if (allWithName.length == 1)
            {
                return(tag);
            }

            // We have more than one. If it's a radio, return the one which is selected.
            // Otherwise, return the last one.

            if (tag == null)
            {
                return(null);
            }

            if (tag.Type == InputType.Radio)
            {
                // Which is selected?
                foreach (HtmlElement radio in allWithName)
                {
                    if (((HtmlInputElement)radio).Checked)
                    {
                        return(radio);
                    }
                }
            }

            return(allWithName[allWithName.length - 1] as HtmlElement);
        }
Esempio n. 2
0
        /// <summary>Submits this form using the given button. It may override the action etc.</summary>
        public void submit(HtmlElement clickedButton)
        {
            // Generate a nice dictionary of the form contents.

            // Step 1: find the unique names of the elements:
            Dictionary <string, string> uniqueValues = new Dictionary <string, string>();

            // Get submittable elements:
            HTMLFormControlsCollection allInputs = GetAllInputs(InputSearchMode.Submittable);

            foreach (Element element in allInputs)
            {
                if (element is HtmlButtonElement)
                {
                    // No buttons.
                    continue;
                }

                string type = element.getAttribute("type");
                if (type == "submit")
                {
                    // No submit either.
                    continue;
                }

                string name = element.getAttribute("name");
                if (name == null)
                {
                    name = "";
                }

                // Step 2: For each one, get their value.
                // We might have a name repeated, in which case we check if they are radio boxes.

                if (uniqueValues.ContainsKey(name))
                {
                    // Ok the element is already added - we have two with the same name.
                    // If they are radio, then only overwrite value if the new addition is checked.
                    // Otherwise, overwrite - furthest down takes priority.
                    if (element.Tag == "input")
                    {
                        HtmlInputElement tag = (HtmlInputElement)element;

                        if (tag.Type == InputType.Radio && !tag.Checked)
                        {
                            // Don't overwrite.
                            continue;
                        }
                    }
                }
                string value = (element as HtmlElement).value;
                if (value == null)
                {
                    value = "";
                }
                uniqueValues[name] = value;
            }

            // Get the action:
            string action = GetOverriden("action", clickedButton);

            FormEvent formData = new FormEvent(uniqueValues);

            formData.SetTrusted(true);
            formData.EventType = "submit";
            // Hook up the form element:
            formData.form = this;

            if (dispatchEvent(formData))
            {
                // Get ready to post now!
                DataPackage package = new DataPackage(action, document.basepath);
                package.AttachForm(formData.ToUnityForm());

                // Apply request to the data:
                formData.request = package;

                // Apply load event:
                package.onload = package.onerror = delegate(UIEvent e){
                    // Attempt to run ondone (doesn't bubble):
                    formData.Reset();
                    formData.EventType = "done";

                    if (dispatchEvent(formData))
                    {
                        // Otherwise the ondone function quit the event.

                        // Load the result into target now.
                        string target = GetOverriden("target", clickedButton);

                        HtmlDocument targetDocument = ResolveTarget(target);

                        if (targetDocument == null)
                        {
                            // Posting a form to an external target.

                            Log.Add("Warning: Unity cannot post forms to external targets. It will be loaded a second time.");

                            // Open the URL outside of Unity:
                            UnityEngine.Application.OpenURL(package.location.absoluteNoHash);
                        }
                        else
                        {
                            // Change the location:
                            targetDocument.SetRawLocation(package.location);

                            // History entry required:
                            targetDocument.window.history.DocumentNavigated();

                            // Apply document content:
                            targetDocument.GotDocumentContent(package.responseText, package.statusCode, true);
                        }
                    }
                };

                // Send now!
                package.send();
            }
        }