Errors and Warnings – Web 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. 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.
Property | Description |
---|---|
ErrorMessage | Returns a string message of the error or warning. |
Target | Returns an HTML or CSS element where the error was found. The returned object is of type Target. |
ErrorTypeName | Description of the presentation of the error object. It has two options “Error” or “Warning”. |
ErrorType | Returns error type numeric value. |
Success | Result 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.
Property | Description |
---|---|
Item | Returns Object of html or css element. |
TargetType | Returns the type of the contained object. Object type of TargetTypes. |
TargetTypes
The TargetTypes Enum of element types from the HTML document containing the error:
Value | Description |
---|---|
HTMLElement | The element containing the HTMLElement from document. |
CSSStyleRule | The element containing the CSSStyleRule from document. |
CSSStyleSheet | The 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:
- Use the
RuleValidationResult class and iterate through the
ruleResult
objects contained withinvalidationResult.Details
. These represent different accessibility criteria. - 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.
- Use the Success property to check the criteria.
- Use the
ITechniqueResult objects –
ruleDetail
contained withinruleResult.Errors
. The ITechniqueResult objects represent individual rule results for the criterion that reported errors. - Print the information about the method, including the method code, success status, and description.
- Use the
IError property to obtain the
error
object from theruleDetail
. TheruleDetail
represents a specific accessibility issue, and theerror
object contains information about that issue. - Use the
Target property to retrieve the
target
object associated with the error. Thetarget
object typically represents the specific HTML element, CSS rule, or other content that triggered the accessibility error. - Print information about the error. It includes the error type (error.ErrorTypeName) and the error message (error.ErrorMessage).
- 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// 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
7string documentPath = Path.Combine(DataDir, "input.html");
8
9// Initialize an object of the HTMLDocument class
10using (var document = new HTMLDocument(documentPath))
11{
12 // Check the document
13 ValidationResult validationResult = validator.Validate(document);
14
15 foreach (RuleValidationResult ruleResult in validationResult.Details)
16 {
17 // list only unsuccessful rule
18 if (!ruleResult.Success)
19 {
20 // print the code and description of the rule
21 Console.WriteLine("{0}:{1} = {2}", ruleResult.Rule.Code, ruleResult.Rule.Description, ruleResult.Success);
22 // print the results of methods with errors
23 foreach (ITechniqueResult ruleDetail in ruleResult.Errors)
24 {
25 // print the code and description of the method
26 StringBuilder str = new StringBuilder(string.Format("\n{0}: {1} - {2}",
27 ruleDetail.Rule.Code, ruleDetail.Success,
28 ruleDetail.Rule.Description));
29 // get an error object
30 var error = ruleDetail.Error;
31 // get a target object
32 var target = error.Target;
33 // get error type and message
34 str.AppendFormat("\n\n\t{0} : {1}", error.ErrorTypeName, error.ErrorMessage);
35 if (target != null)
36 {
37 // Checking the type of the contained object for casting and working with it
38 if (target.TargetType == TargetTypes.CSSStyleRule)
39 {
40 var cssRule = (ICSSStyleRule)target.Item;
41 str.AppendFormat("\n\n\t{0}", cssRule.CSSText);
42 }
43 if (ruleDetail.Error.Target.TargetType == TargetTypes.CSSStyleSheet)
44 str.AppendFormat("\n\n\t{0}", ((ICSSStyleSheet)target.Item).Title);
45
46 if (ruleDetail.Error.Target.TargetType == TargetTypes.HTMLElement)
47 str.AppendFormat("\n\n\t{0}", ((HTMLElement)target.Item).OuterHTML);
48 }
49 Console.WriteLine(str.ToString());
50 }
51 }
52 }
53}