Web Accessibility Rules – Website Accessibility Check in C#

Aspose.HTML for .NET provides the Aspose.Html.Accessibility namespace, which is intended for all Web Accessibility related manipulations and checks. In this article, you will learn how to use the AccessibilityRules class, which is a repository of WCAG 2.0requirements, success criteria, and techniques.

You can learn how to use the AccessibilityValidator class to check web accessibility from the article Accessibility Validator – Website Accessibility Check in C#.

Accessibility rules are a set of guidelines, standards, or best practices that define how digital content and interfaces should be designed and developed to ensure they are accessible to people with disabilities. These guidelines are intended to make digital content, such as websites, apps, and multimedia, accessible to a wide range of people, including those with visual, hearing, motor, and cognitive impairments. Accessibility rules are crucial for promoting digital inclusion and ensuring everyone has equal access to online information and services.

The structure of the quick reference can be presented in the form of a diagram, which is indicated below:

Text “The structure of the web accessibility rules – principles, guidelines, and criteria.”

The directory contains the rules that implement the IRule interface.

Accessibility Rules

The AccessibilityRules class is a repository of WCAG 2.0 requirements, success criteria, and techniques and has a structure that corresponds to the specification WCAG (Quick Reference) and is loaded using the WebAccessibilityDataSource class. The class has an internal constructor. Therefore users cannot directly create an object of the class. In order to view the list of rules, you need to initialize the WebAccessibility container and use the property Rules.

1    // Initialize a webAccessibility container
2    var webAccessibility = new WebAccessibility();
3
4    // Get the accessibilityRules repository
5    AccessibilityRules accessibilityRules = webAccessibility.Rules;

The property returns an object of type AccessibilityRules, which has such methods for accessing the rules:

MethodsDescription
GetPrinciple(string code)Returns Principle object by code from WCAG.
GetPrinciples()Returns a list of principles IList< Principle>.
GetRules(params string[] codes)Returns a list of rules IList< IRule> for given codes from WCAG.

The directory contains rules that inherit from the abstract class Rule. There are several types of rules:

IRule

All rules implement the interface IRule is a public interface that defines the basic properties of the rules:

PropertyDescription
CodeRule code from the quick reference WCAG.
DescriptionDescription of Rule from the quick reference WCAG.

Rule

The base abstract class for all rules, which implements the interface IRule. To get any rule, you can use the GetRules() method:

 1// Initialize a webAccessibility container
 2WebAccessibility webAccessibility = new WebAccessibility();
 3
 4// List of rule codes can contain both technique codes and principles, guidelines and criteria - all rules that implement the interface IRule
 5var rulesCodes = new string[] { "H2", "H37", "H30", "1.1", "1.2.1" };
 6
 7// Get a list of IRule objects; if a rule with the specified code is not found, it will not be in the list
 8var rules = webAccessibility.Rules.GetRules(rulesCodes);
 9
10// Get code and description of rule
11foreach (var rule in rules)
12    Console.WriteLine("{0}:{1}", rule.Code, rule.Description);
view raw Example-GetRules hosted with ❤ by GitHub

The program will output to the console:

1    H2:Combining adjacent image and text links for the same resource
2    H37:Using alt attributes on img elements
3    H30:Providing link text that describes the purpose of a link for anchor elements
4    1.1:Text Alternatives
5    1.2.1:Audio-only and Video-only (Prerecorded)

Principle

At the first level of the list of rules are the Principles, they indicate the main direction and purpose of the rules that are in this section. Therefore, work with the directory begins with them.

The Principle class inherited from Rule and also contains a list of guidelines.

MethodDescription
GetGuideline(string code)Get Guideline by code from WCAG, contained in principle. Return Guideline object.
GetGuidelines()Get a list of Guideline from Principle. Return IList< Guideline> object.

An example of how to get a Principle object from a list of rules:

1           
2// Initialize a webAccessibility container
3var webAccessibility = new WebAccessibility();
4
5// Get the principle by code
6Principle rule = webAccessibility.Rules.GetPrinciple("1");
7
8// Get code and description of principle
9Console.WriteLine("{0}:{1}", rule.Code, rule.Description);  // output: 1:Perceivable      

Guideline

The Guideline class inherited from Rule and contains a criteria list. Guidelines are the next level after principles. They outline frameworks and general goals that help authors understand success criteria and better apply the techniques.

MethodDescription
GetCriterion(string code)Get Guideline by code from WCAG, contained in current Guideline. Return Criterion object.
GetCriterions()Get a list of Criterion from current Guideline. Return IList< Criterion> object.

An example of how to get a Guideline object from a list of rules:

 1           
 2// Initialize a webAccessibility container
 3var webAccessibility = new WebAccessibility();
 4
 5// Get the principle by code 
 6Principle principle = webAccessibility.Rules.GetPrinciple("1");
 7
 8// Get guideline 1.1
 9Guideline guideline = principle.GetGuideline("1.1");
10if (guideline != null)
11{
12    Console.WriteLine("{0}:{1}", guideline.Code, guideline.Description, guideline);  // output: 1.1:Text Alternatives
13}

Criterion

The Criterion class describes the WCAG success criteria, inherited from abstract class Rule. Detailed information can be found here – Understanding Techniques for WCAG Success Criteria. The basis for determining conformance to WCAG 2.0 is the success criteria from the WCAG 2.0 standard – not the techniques.

Verifiable success criteria are provided for each recommendation so WCAG 2.0 can be applied in areas where compliance testing is required.

PropertyDescription
LevelCompliance levels: A (lowest), AA and AAA (highest).
SufficientTechniquesReturns list of Sufficient techniques that are reliable ways to meet the success criteria.
AdvisoryTechniquesReturns list of Advisory techniques that are suggested ways to improve accessibility. They are often very helpful to some users, and may be the only way that some users can access some types of content.
FailuresReturns list of Failures that are things that cause accessibility barriers and fail specific success criteria.

The criterion contains a list of techniques for meeting WCAG web content accessibility guidelines. If all sufficient methods for a given criterion are supported by accessibility, then the success criterion has passed.

Use the GetCriterion(string code) method to access criteria from guideline:

 1           
 2// Initialize a webAccessibility container
 3var webAccessibility = new WebAccessibility();
 4
 5// Get the principle by code 
 6Principle principle = webAccessibility.Rules.GetPrinciple("1");
 7
 8// Get guideline 
 9Guideline guideline = principle.GetGuideline("1.1");
10
11// Get criterion by code
12Criterion criterion = guideline.GetCriterion("1.1.1");
13if (criterion != null)
14{
15    Console.WriteLine("{0}:{1} - {2}", criterion.Code, criterion.Description, criterion.Level); // output: 1.1.1:Non-text Content - A
16
17    // Get all Sufficient Techniques and write to console
18    foreach (var technique in criterion.SufficientTechniques)
19        Console.WriteLine("{0}:{1}", technique.Code, technique.Description);
20}

Check HTML Against Specific Web Accessibility Rules

 1var htmlPath = Path.Combine(DataDir, "input.html");
 2
 3// Initialize a webAccessibility container
 4var webAccessibility = new WebAccessibility();
 5
 6// List of necessary rules for checking (rule code according to the specification) 
 7var rulesCode = new string[] { "H2", "H37", "H67", "H86" };
 8
 9// Get the required IList<Rule> rules from the rules reference
10var rules = webAccessibility.Rules.GetRules(rulesCode);
11
12// Create an accessibility validator, pass the found rules as parameters, and specify the full validation settings
13var validator = webAccessibility.CreateValidator(rules, ValidationBuilder.All);
14
15// Initialize an object of the HTMLDocument
16using (var document = new HTMLDocument(htmlPath))
17{
18    // Check the document
19    var validationResult = validator.Validate(document);
20
21    // Return the result in string format
22    // SaveToString - return only errors and warnings
23    Console.WriteLine(validationResult.SaveToString());
24}

This code demonstrates how to validate an HTML document for web accessibility using the specified set of rules.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.