Accessibility Validator – Website Accessibility Check in C#
Create Validator
Aspose.HTML for .NET provides the Aspose.Html.Accessibility namespace, which is for all web accessibility related manipulations and checks. The AccessibilityValidator class initializes an object that is used to validate the document against WCAG success criteria. The class has an internal constructor, therefore users cannot directly create an object of the class. To create an object, you need to use the WebAccessibility container.
To create a validator, initialize the object WebAccessibility like this:
1 // Initialize webAccessibility container
2 var webAccessibility = new WebAccessibility();
The webAccessibility
object has the following basic methods for creating a validator:
Methods | Description |
---|---|
CreateValidator() | An instance of AccessibilityValidator is created according to all the rules with a ValidationBuilder.All object. |
CreateValidator(Action\<ValidationBuilder\> builder ) | An instance of AccessibilityValidator is created according to all the rules, where the builder is a lambda expression with a list of methods-settings. |
CreateValidator(ValidationBuilder builder ) | An instance of AccessibilityValidator is created according to all the rules with a ValidationBuilder object. |
CreateValidator(IRule rule ) | A new AccessibilityValidator instance is created for a specific rule, given the full parameters of the ValidationBuilder.All object. |
CreateValidator(IRule rule, Action\<ValidationBuilder\> builder ) | Initializes a new instance of the AccessibilityValidator that contains a rule that matches the builder’s settings, where the builder is a lambda expression with a list of methods-settings. |
CreateValidator(IRule rule, ValidationBuilder builder ) | Initializes a new instance of the AccessibilityValidator that contains a rule that matches the builder’s settings. The builder instance is a static instance of the class ValidationBuilder. |
CreateValidator(IList\<IRule\> rules ) | An AccessibilityValidator instance is created for a specific rules list. |
CreateValidator(IList<IRule> rules, ValidationBuilder builder ) | An AccessibilityValidator instance is created for a specific list of rules based on the settings of the ValidationBuilder object. |
CreateValidator(IList\<IRule\> rules, Action\<ValidationBuilder\> builder ) | An AccessibilityValidator instance is created for a specific list of rules where the builder is a lambda expression with a list of settings ValidationBuilder. |
The following are examples of initializing an AccessibilityValidator object:
1 // Initialize a webAccessibility container
2 var webAccessibility = new WebAccessibility();
3
4 // Create an accessibility validator with a static instance for all rules from repository that match the builder settings
5 var validator = webAccessibility.CreateValidator(ValidationBuilder.All);
6
7 // Get a principle by code
8 IRule rule = webAccessibility.Rules.GetPrinciple("1");
9
10 // Create an accessibility validator for one principle, whose rules match the instance settings ValidationBuilder.All
11 var validatorRule = webAccessibility.CreateValidator(rule, ValidationBuilder.All);
12
13 // Create an accessibility validator with lambda-expression settings for all rules from repository that match the builder settings inside a project
14 var validatorCSS = new AccessibilityValidator(builder => builder.AllLevels().UseCSS());
ValidationBuilder Class
The ValidationBuilder class defines a helper class that can be used to customize the list of rules to be checked by the validator. The ValidationBuilder defines methods and provides concrete implementations of the configuration steps.
Method/Property | Description |
---|---|
ValidationBuilder.None | None of the parameter settings are specified, it is convenient for creating a filter from scratch, when you need to specify which levels, technologies, etc. should be used when checking. |
ValidationBuilder.Default | Rules for General technologies, Failures for all levels of criteria will be used when checking the document. |
ValidationBuilder.All | The verification will take place at all levels of criteria, for all technologies, including HTML,CSS,Crtipt. |
SetHTMLTags(params string[] tags ) | The user can specify a list of html-tags to be checked in the document. |
AllTechnologies() | All technologies will be applied when checking the document. |
UseHTML() | The method indicates that elements of the HTML technology will be checked in the document. |
UseCSS() | The method indicates that elements of the CSS technology will be checked in the document. |
UseScript() | The method indicates that elements of the ClientSide-Script technology will be checked in the document. |
UseFailures() | The method indicates that failures rules will be checked in the document. |
UseGeneral() | The method indicates that general rules will be checked in the document. |
AllLevels() | The method sets all criteria levels and indicates that the document will be checked according to the criteria of all three levels. |
UseLowestLevel() | The method determines that the document must be validated against low-level criteria. |
UseMiddleLevel() | The method determines that the document must be validated against mid-level criteria. |
UseHighestLevel() | The method determines that the document must be validated against high-level criteria. |
The following code snippet is an example of creating the builder and passing it as a parameter to the validator:
1 // Initialize a webAccessibility container
2 WebAccessibility webAccessibility = new WebAccessibility();
3
4 // Create an accessibility validator with object All
5 var validator = webAccessibility.CreateValidator(ValidationBuilder.All);
6
7 // Create an accessibility validator with empty filters, and set's html and css technologies, low and middle levels
8 var validatorHTML = webAccessibility.CreateValidator(ValidationBuilder.None.UseHTML()
9 .UseCSS()
10 .UseLowestLevel()
11 .UseMiddleLevel());
12
13 // Create an accessibility validator with lambda-expression for all levels and only CSS technologies
14 var validatorCSS = webAccessibility.CreateValidator(builder => builder.AllLevels().UseCSS());
15
16 // Create an accessibility validator for all levels of criterion and only img and input elements will be checking
17 var validatorIMG = webAccessibility.CreateValidator(builder => builder.AllLevels()
18 .AllTechnologies()
19 .SetHTMLTags("img", "input"));
Validate Document
To run document verification, you should call the
Validate(HTMLDocument document
) method of the
AccessibilityValidator object. The method checks a document for compliance with accessibility rules. The result of executing the method is an instance of the class
ValidationResult.
Here is an example of calling a document validation:
1 // Create an accessibility validator
2 AccessibilityValidator validator = webAccessibility.CreateValidator();
3
4 // Initialize HTMLDocument object
5 using (var document = new Aspose.Html.HTMLDocument("index.html"))
6 {
7 // Check the document
8 var result = validator.Validate(document);
9
10 // Checking for success
11 if (!result.Success)
12 {
13 foreach (var detail in result.Details)
14 {
15 // ... do the analysis here...
16 }
17 }
18 }
ValidationResult Class
To run the validator, instantiate the validator object and call the
Validate(HTMLDocument document
) method, passing in the document to validate. The result of executing this method is an instance of the
ValidationResult class containing information about document validation:
Property | Description |
---|---|
Success | Returns a boolean that says whether the validation succeeded. |
Details | Returns Collection of RuleValidationResult objects containing details about any validation results. |
The following code will write to the console which of the rules passed the test and which did not:
1 // Initialize an object of the HTMLDocument class
2 using (var document = new Aspose.Html.HTMLDocument("index.html"))
3 {
4 // Check the document
5 ValidationResult result = validator.Validate(document);
6
7 // Checking for success
8 if (!result.Success)
9 {
10 foreach (var detail in result.Details)
11 {
12 Console.WriteLine("{0}:{1} = {2}", detail.Rule.Code, detail.Rule.Description, detail.Success);
13 }
14 }
15 }
A more complete description can be found in the article Validation Results – Check Web Accessibility in C#.