Accessibility Errors and Warnings in C#

Contents
[ Hide Show ]

Use Aspose.HTML for .NET to collect accessibility errors and warnings in C#. After validation, inspect IError, Target, TargetTypes, failed technique results, messages, and the source HTML or CSS element that caused the issue.

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 technique:

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 – ruleDetail 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 ruleDetail. The ruleDetail 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// Check HTML for WCAG compliance and output failed rule codes and error messages
 2
 3// Initialize a webAccessibility container
 4WebAccessibility webAccessibility = new WebAccessibility();
 5
 6// Create an accessibillity validator with static instance for all rules from repository that match the builder settings
 7AccessibilityValidator validator = webAccessibility.CreateValidator(ValidationBuilder.All);
 8
 9string documentPath = Path.Combine(DataDir, "input.html");
10
11// Initialize an object of the HTMLDocument class
12using (HTMLDocument document = new HTMLDocument(documentPath))
13{
14    // Check the document
15    ValidationResult validationResult = validator.Validate(document);
16
17    foreach (RuleValidationResult ruleResult in validationResult.Details)
18    {
19        //  list only unsuccessful rule
20        if (!ruleResult.Success)
21        {
22            // print the code and description of the rule
23            Console.WriteLine("{0}:{1} = {2}", ruleResult.Rule.Code, ruleResult.Rule.Description, ruleResult.Success);
24            // print the results of methods with errors
25            foreach (ITechniqueResult ruleDetail in ruleResult.Errors)
26            {
27                // print the code and description of the method
28                StringBuilder str = new StringBuilder(string.Format("\n{0}: {1} - {2}",
29                                                           ruleDetail.Rule.Code, ruleDetail.Success,
30                                                           ruleDetail.Rule.Description));
31                // get an error object 
32                IError error = ruleDetail.Error;
33                // get a target object 
34                Target target = error.Target;
35                // get error type and message 
36                str.AppendFormat("\n\n\t{0} : {1}", error.ErrorTypeName, error.ErrorMessage);
37                if (target != null)
38                {
39                    // Checking the type of the contained object for casting and working with it
40                    if (target.TargetType == TargetTypes.CSSStyleRule)
41                    {
42                        ICSSStyleRule cssRule = (ICSSStyleRule)target.Item;
43                        str.AppendFormat("\n\n\t{0}", cssRule.CSSText);
44                    }
45                    if (ruleDetail.Error.Target.TargetType == TargetTypes.CSSStyleSheet)
46                        str.AppendFormat("\n\n\t{0}", ((ICSSStyleSheet)target.Item).Title);
47
48                    if (ruleDetail.Error.Target.TargetType == TargetTypes.HTMLElement)
49                        str.AppendFormat("\n\n\t{0}", ((HTMLElement)target.Item).OuterHTML);
50                }
51                Console.WriteLine(str.ToString());
52            }
53        }
54    }
55}

FAQ

What is the difference between an accessibility error and warning?

An error represents a failed required check. A warning usually reports a non-critical issue or improvement hint while preserving the success state of that specific result.

How do I find the HTML element that caused an issue?

Use the IError.Target property. The returned Target object indicates whether the source is an HTML element, CSS rule, or CSS stylesheet.

Should warnings be treated as failures?

Not always. Warnings can indicate improvement opportunities or non-critical findings, but they should still be reviewed before treating a page as production-ready.

Related Articles

You can also use the online Web Accessibility Checker to scan a page before inspecting detailed errors in code.