Errors and Warnings – Web Accessibility Check in C#

Contents
[ Hide Show ]

Aspose.HTML for .NET provides the Aspose.Html.Accessibility namespace, which is intended for all web accessibility related manipulations and checks. This article will examine classes and interfaces that help collect error and warning information when checking a website’s accessibility against WCAG compliance. We will pay particular attention to the failure criteria and methods that report errors and look at a C# example of getting web accessibility errors after validating an HTML document.

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

Detecting errors helps identify barriers that may prevent people with disabilities from using and interacting with web content effectively. Errors and warnings guide what must be addressed to improve web accessibility. They serve as a roadmap for developers and designers to make necessary changes.

IError

The IError is a public interface containing information about the validation error. This means an erroneous check, i.e., the rule was not passed – and its result does not match Accessibility.

PropertyDescription
ErrorMessageReturns a string message of the error or warning.
TargetReturns an HTML or CSS element where the error was found. The returned object is of type Target.
ErrorTypeNameDescription of the presentation of the error object. It has two options “Error” or “Warning”.
ErrorTypeReturns error type numeric value.
SuccessResult of this object.

IError object has such meanings:

* ErrorType = 1 and Success = false – this means that the error is critical, and the result of the check is not performed.
* ErrorType = 2 and Success = true – this means that the error is not critical but has an informational character and displays hints for possible improvement. 

The type of error is determined by the type of equipment; if the technique is sufficient, then the ErrorType = 1, and it is Error; otherwise, ErrorType = 2 – Warning.

Target

The Target is a public class that contains item of HTML or CSS element of document where the error was found.

PropertyDescription
ItemReturns Object of html or css element.
TargetTypeReturns the type of the contained object. Object type of TargetTypes.

TargetTypes

The TargetTypes Enum of element types from the HTML document containing the error:

ValueDescription
HTMLElementThe element containing the HTMLElement from document.
CSSStyleRuleThe element containing the CSSStyleRule from document.
CSSStyleSheetThe element containing the CSSStyleSheet from document.

Let’s look at the C# code for iterating through the web accessibility check results, paying particular attention to the failure criteria and the details of the methods that report errors. Example of getting errors details and elements of an HTML document:

  1. Use the RuleValidationResult class and iterate through the ruleResult objects contained within validationResult.Details. These represent different accessibility criteria.
  2. Print the code and description of the criterion and whether it was successful or not. For this, use the Code and Description properties of the IRule interface.
  3. Use the Success property to check the criteria.
  4. Use the ITechniqueResult objects – result contained within ruleResult.Errors. The ITechniqueResult objects represent individual rule results for the criterion that reported errors.
  5. Print the information about the method, including the method code, success status, and description.
  6. Use the IError property to obtain the error object from the result. The result represents a specific accessibility issue, and the error object contains information about that issue.
  7. Use the Target property to retrieve the target object associated with the error. The target object typically represents the specific HTML element, CSS rule, or other content that triggered the accessibility error.
  8. Print information about the error. It includes the error type (error.ErrorTypeName) and the error message (error.ErrorMessage).
  9. Use the TargetType property of the Target class to check the type of the target object. Depending on the type of the target object, specific information is extracted and printed to the console.
 1 foreach (var ruleResult in validationResult.Details)
 2 {
 3     // Print the code and description of the criterion
 4     Console.WriteLine(string.Format("{0}: {1} ({2})",
 5             ruleResult.Rule.Code,
 6             ruleResult.Success,
 7             ruleResult.Rule.Description
 8             ));
 9     //  List only unsuccessful criteria
10     if (!ruleResult.Success)
11     {
12         // Print the results of methods with errors
13         foreach (ITechniqueResult result in ruleResult.Errors)
14         {
15            // Print the code and description of the technique
16             Console.WriteLine(string.Format("\n{0}: {1} ({2})",
17                  result.Rule.Code,
18                  result.Success,
19                  result.Rule.Description
20                  ));
21
22             if (result.Error != null)
23             {
24                // Get error object
25                var error = result.Error;
26                // Get target object
27                var target = error.Target;
28                // Print error type and message
29                Console.WriteLine("\n\t{0} {1}", error.ErrorTypeName, error.ErrorMessage);
30
31                if (target != null)
32                {
33                    // Checking the type of the contained object for casting and working with it
34                    if (target.TargetType == TargetTypes.CSSStyleRule)
35                        Console.WriteLine("\n\n\t{0}", ((ICSSStyleRule)target.Item).CSSText);
36                    if (target.TargetType == TargetTypes.CSSStyleSheet)
37                        Console.WriteLine("\n\n\t{0}", ((ICSSStyleSheet)target.Item).Title);
38                    if (target.TargetType == TargetTypes.HTMLElement)
39                        Console.WriteLine("\n\n\t{0}", ((HTMLElement)target.Item).OuterHTML);
40                }
41             }
42         }
43     }
44 }
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.