public IEnumerable<ValidationError> ValidateHtml(HtmlDocument document)
        {
            var records = new List<ValidationError>();

            var labels = document.GetNodes("//label");

            foreach (var label in labels)
            {
                var forAttribute = label.Attributes["for"];
                HtmlNode relatedElement = null;

                if (forAttribute != null)
                {
                    string labelId = forAttribute.Value;
                    string xpath = string.Format("//input[@id='{0}']", labelId);

                    relatedElement = document.GetNode(xpath);

                    if (relatedElement == null)
                    {
                        xpath = string.Format("//select[@id='{0}']", labelId);
                        relatedElement = document.GetNode(xpath);
                    }

                    if (relatedElement == null)
                    {
                        xpath = string.Format("//textarea[@id='{0}']", labelId);
                        relatedElement = document.GetNode(xpath);
                    }
                }

                if (relatedElement == null)
                {
                    string message = string.Format("Label does not relate to a form control: {0}", label.OuterHtml);
                    records.Add(new ValidationError(message));
                }
            }

            return records;
        }
예제 #2
0
        public IEnumerable<ValidationError> ValidateHtml(HtmlDocument document)
        {
            var records = new List<ValidationError>();

            var textDefinition = new {TagName = "text", Description = "Textbox", IsInput = true};
            var passwordDefinition = new {TagName = "password", Description = "Password textbox", IsInput = true};
            var checkboxDefinition = new {TagName = "checkbox", Description = "Checkbox", IsInput = true};
            var selectDefinition = new { TagName = "select", Description = "Select list", IsInput = false };
            var textAreaDefinition = new { TagName = "textarea", Description = "Text area", IsInput = false };

            var definitions = new[] {textDefinition, passwordDefinition, checkboxDefinition, selectDefinition, textAreaDefinition};

            foreach (var definition in definitions)
            {
                var formElementXPath = definition.IsInput ? string.Format("//input[@type='{0}']", definition.TagName) : string.Format("//{0}", definition.TagName);

                var elements = document.GetNodes(formElementXPath);

                foreach (var element in elements)
                {
                    var idAttribute = element.Attributes["id"];

                    HtmlNode correspondingLabel = null;

                    if (idAttribute != null)
                    {
                        var elementId = idAttribute.Value;
                        var xpath = string.Format("//label[@for='{0}']", elementId);

                        correspondingLabel = document.GetNode(xpath);
                    }

                    if (correspondingLabel == null)
                    {
                        var message = string.Format("{0} missing corresponding label: {1}", definition.Description, element.OuterHtml);
                        records.Add(new ValidationError(message, element.Line, element.LinePosition));
                    }
                }
            }

            return records;
        }