private HtmlForm HandleForm(BeginTag formTag) { string name = formTag.GetAttributeValue("name"); string action = formTag.GetAttributeValue("action"); string method = formTag.GetAttributeValue("method"); HtmlForm htmlForm = new HtmlForm(name, action, method); Element el; while (null != (el = parser.Next())) { if (el is EndTag && ((EndTag)el).NameEquals("form")) break; BeginTag tag = el as BeginTag; if (tag == null) continue; switch (tag.Name.ToLowerInvariant()) { case "input": HandleInput(htmlForm, tag); break; case "select": HandleSelect(htmlForm, tag); break; case "textarea": HandleTextarea(htmlForm, tag); break; } } return htmlForm; }
private void HandleInput(HtmlForm parentForm, BeginTag inputTag) { string type = inputTag.GetAttributeValue("type"); if (type != null) type = type.Trim().ToLowerInvariant(); string name = inputTag.GetAttributeValue("name"); string value = inputTag.GetAttributeValue("value"); switch (type) { case "password": new Textbox(parentForm, name, value); break; case "checkbox": { int dummy; bool isChecked = inputTag.GetAttribute("checked", true, 0, out dummy) != null; new Checkbox(parentForm, name, value, isChecked); break; } case "radio": { int dummy; bool isChecked = inputTag.GetAttribute("checked", true, 0, out dummy) != null; new Radio(parentForm, name, value, isChecked); break; } case "submit": new SubmitButton(parentForm, name, value); break; case "image": new ImageButton(parentForm, name, value, inputTag.GetAttributeValue("src")); break; case "hidden": new Hidden(parentForm, name, value); break; case "text": default: new Textbox(parentForm, name, value); break; } }
/// <summary> /// Is the tag a meaningless tag such as <p></p> or <a href="..."></a> or <a href="..."> </a> /// </summary> /// <param name="htmlParser"></param> /// <param name="bt"></param> /// <returns></returns> private static bool RemoveMeaninglessTags(SimpleHtmlParser htmlParser, BeginTag bt) { // Look to see if the tag is a <p> without any attributes if ((bt.NameEquals("p") && bt.Attributes.Length == 0 && !bt.HasResidue)) { Element e = htmlParser.Peek(0); // Look to see if thereis a matching end tag to the element we are looking at if (e != null && e is EndTag && ((EndTag)e).NameEquals("p")) { // eat up the end tag htmlParser.Next(); return true; } } // Look to see if the tag is an <a> without a style/id/name attribute, but has an href... meaning the link is not useful if ((bt.NameEquals("a") && bt.GetAttribute("name") == null && bt.GetAttributeValue("style") == null && bt.GetAttributeValue("id") == null && bt.GetAttributeValue("href") != null)) { bool hadWhiteSpaceText = false; Element e = htmlParser.Peek(0); // Look to see if the a just has whitespace inside of it if (e is Text && HtmlUtils.UnEscapeEntities(e.RawText, HtmlUtils.UnEscapeMode.NonMarkupText).Trim().Length == 0) { e = htmlParser.Peek(1); hadWhiteSpaceText = true; } // Look to see if thereis a matching end tag to the element we are looking at if (e != null && e is EndTag && ((EndTag)e).NameEquals("a")) { // if this was an <a> with whitespace in the middle eat it up if (hadWhiteSpaceText) htmlParser.Next(); // eat up the end tag htmlParser.Next(); return true; } } return false; }
private void HandleTextarea(HtmlForm parentForm, BeginTag textareaTag) { string name = textareaTag.GetAttributeValue("name"); string value = parser.CollectTextUntil("textarea"); new Textarea(parentForm, name, value); }
private void HandleSelect(HtmlForm parentForm, BeginTag selectTag) { string name = selectTag.GetAttributeValue("name"); int dummy; bool multiple = selectTag.GetAttribute("multiple", true, 0, out dummy) != null; ArrayList optionInfos = new ArrayList(); Element el = parser.Next(); while (el != null) { BeginTag tag = el as BeginTag; if (tag != null && tag.NameEquals("option")) { string value = tag.GetAttributeValue("value"); bool isSelected = tag.GetAttribute("selected", true, 0, out dummy) != null; string label = string.Empty; el = parser.Next(); if (el != null && el is Text) { label = HtmlUtils.UnEscapeEntities(el.ToString(), HtmlUtils.UnEscapeMode.NonMarkupText).TrimEnd(' ', '\r', '\n', '\t'); el = parser.Next(); } optionInfos.Add(new OptionInfo(value, label, isSelected)); continue; } if (el is EndTag && ((EndTag)el).NameEquals("select")) { new Select(parentForm, name, multiple, (OptionInfo[])optionInfos.ToArray(typeof(OptionInfo))); return; } el = parser.Next(); } }