Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
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 technique:
ErrorType=1 and Success=false, and it is an Error.ErrorType=2 and Success=true, and it is a Warning.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. |
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:
ruleResult objects contained within validationResult.Details. These represent different accessibility criteria.ruleDetail contained within ruleResult.Errors. The
ITechniqueResult objects represent individual rule results for the criterion that reported errors.error object from the ruleDetail. The ruleDetail represents a specific accessibility issue, and the error object contains information about that issue.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. 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}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.
Use the IError.Target property. The returned Target object indicates whether the source is an HTML element, CSS rule, or CSS stylesheet.
Not always. Warnings can indicate improvement opportunities or non-critical findings, but they should still be reviewed before treating a page as production-ready.
ValidationResult, RuleValidationResult, and technique details.You can also use the online Web Accessibility Checker to scan a page before inspecting detailed errors in code.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.