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 of the WebAccessibility class like this:

1    // Initialize webAccessibility container
2    var webAccessibility = new WebAccessibility();

The webAccessibility object has the following basic methods for creating a validator:

MethodsDescription
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. The first validator (validator) is a comprehensive instance created with ValidationBuilder.All, applying all rules from the repository. A specific principle is retrieved using its code, and a second validator (validatorRule) is created for the rules associated with that principle while adhering to the same comprehensive settings. Finally, a third validator (validatorCSS) is set up using a lambda expression to refine the validation criteria, targeting all levels and incorporating CSS-based rules. This approach offers flexibility in tailoring validators for specific accessibility checks, enabling targeted validation strategies.

 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/PropertyDescription
ValidationBuilder.NoneNone 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.DefaultRules for General technologies, Failures for all levels of criteria will be used when checking the document.
ValidationBuilder.AllThe 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"));

ValidationResult Class

The ValidationResult class is the central component of web accessibility validation. It encapsulates the results of accessibility checks, providing a structured way to analyze, manage, and act on the results of validation operations. To use it, instantiate a validator object and invoke the Validate(HTMLDocument document) method, passing in the document to be validated. The method returns a ValidationResult object containing the validation process results.

Key Properties:

PropertyDescription
SuccessReturns a boolean that says whether the validation succeeded.
DetailsReturns Collection of RuleValidationResult objects containing details about any validation results.

Validate Document

To run document validation, you need to perform the following steps:

  1. Use the WebAccessibility() constructor to create an instance of the WebAccessibility class responsible for web accessibility checking.
  2. Call the CreateValidator() method to create a validator object.
  3. Load an HTML document using one of the HTMLDocument() constructors.
  4. Use the Validate(HTMLDocument document) method to validate the document against web accessibility rules.
  5. The result of the validation is an instance of the ValidationResult class.
  6. Print the validation results to the console and use them as needed for further analysis.

Here is an example of calling a document validation. The following code will write to the console which of the rules passed the test and which did not:

 1// Initialize a webAccessibility container
 2var webAccessibility = new WebAccessibility();
 3
 4// Create an accessibillity validator with static instance for all rules from repository that match the builder settings
 5var validator = webAccessibility.CreateValidator(ValidationBuilder.All);
 6
 7// Prepare a path to a source HTML file
 8string documentPath = Path.Combine(DataDir, "input.html");
 9
10// Initialize an object of the HTMLDocument class
11using (var document = new HTMLDocument(documentPath))
12{
13    // Check the document
14    ValidationResult validationResult = validator.Validate(document);
15
16    // Checking for success
17    if (!validationResult.Success)
18    {
19        // Get a list of RuleValidationResult Details
20        foreach (var detail in validationResult.Details)
21        {
22            Console.WriteLine("{0}:{1} = {2}", detail.Rule.Code, detail.Rule.Description, detail.Success);
23        }
24    }
25}

A more complete description can be found in the article Validation Results – Check Web Accessibility in C#.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.