Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.HTML for .NET provides the Aspose.Html.Accessibility namespace, which is for all web accessibility related manipulations and checks. The AccessibilityValidator class initializes an object that is used to validate the document against WCAG success criteria. The class does not have an internal constructor, therefore users cannot directly create an object of the class. To create an object, you need to use the WebAccessibility container.
To create a validator, initialize the object of the WebAccessibility class like this:
1 // Initialize webAccessibility container
2 var webAccessibility = new WebAccessibility();The webAccessibility object has the following basic methods for creating a validator:
| Methods | Description |
|---|---|
| CreateValidator() | An instance of AccessibilityValidator is created according to all the rules with a ValidationBuilder.All object. |
CreateValidator(Action<ValidationBuilder> builder) | An instance of AccessibilityValidator is created according to all the rules, where the builder is a lambda expression with a list of methods-settings. |
CreateValidator(ValidationBuilder builder) | An instance of AccessibilityValidator is created according to all the rules with a ValidationBuilder object. |
CreateValidator(IRule rule) | A new AccessibilityValidator instance is created for a specific rule, given the full parameters of the ValidationBuilder.All object. |
CreateValidator(IRule rule, Action<ValidationBuilder> builder) | Initializes a new instance of the AccessibilityValidator that contains a rule that matches the builder’s settings, where the builder is a lambda expression with a list of methods-settings. |
CreateValidator(IRule rule, ValidationBuilder builder) | Initializes a new instance of the AccessibilityValidator that contains a rule that matches the builder’s settings. The builder instance is a static instance of the class ValidationBuilder. |
CreateValidator(IList<IRule> rules) | An AccessibilityValidator instance is created for a specific rules list. |
CreateValidator(IList<IRule> rules, ValidationBuilder builder) | An AccessibilityValidator instance is created for a specific list of rules based on the settings of the ValidationBuilder object. |
CreateValidator(IList<IRule> rules, Action<ValidationBuilder> builder) | An AccessibilityValidator instance is created for a specific list of rules where the builder is a lambda expression with a list of settings ValidationBuilder. |
The following are examples of initializing an
AccessibilityValidator object. The first validator (validator) is a comprehensive instance created with ValidationBuilder.All, applying all rules from the repository. A specific principle is retrieved using its code, and a second validator (validatorRule) is created for the rules associated with that principle while adhering to the same comprehensive settings. Finally, a third validator (validatorCSS) is set up using a lambda expression to refine the validation criteria, targeting all levels and incorporating CSS-based rules. This approach offers flexibility in tailoring validators for specific accessibility checks, enabling targeted validation strategies.
1 // Initialize a webAccessibility container
2 var webAccessibility = new WebAccessibility();
3
4 // Create an accessibility validator with a static instance for all rules from repository that match the builder settings
5 var validator = webAccessibility.CreateValidator(ValidationBuilder.All);
6
7 // Get a principle by code
8 IRule rule = webAccessibility.Rules.GetPrinciple("1");
9
10 // Create an accessibility validator for one principle, whose rules match the instance settings ValidationBuilder.All
11 var validatorRule = webAccessibility.CreateValidator(rule, ValidationBuilder.All);
12
13 // Create an accessibility validator with lambda-expression settings for all rules from repository that match the builder settings inside a project
14 var validatorCSS = new AccessibilityValidator(builder => builder.AllLevels().UseCSS());The ValidationBuilder class defines a helper class that can be used to customize the list of rules to be checked by the validator. The ValidationBuilder defines methods and provides concrete implementations of the configuration steps.
| Method/Property | Description |
|---|---|
| ValidationBuilder.None | None of the parameter settings are specified, it is convenient for creating a filter from scratch, when you need to specify which levels, technologies, etc. should be used when checking. |
| ValidationBuilder.Default | Rules for General technologies, Failures for all levels of criteria will be used when checking the document. |
| ValidationBuilder.All | The verification will take place at all levels of criteria, for all technologies, including HTML,CSS,Crtipt. |
SetHTMLTags(params string[] tags) | The user can specify a list of html-tags to be checked in the document. |
| AllTechnologies() | All technologies will be applied when checking the document. |
| UseHTML() | The method indicates that elements of the HTML technology will be checked in the document. |
| UseCSS() | The method indicates that elements of the CSS technology will be checked in the document. |
| UseScript() | The method indicates that elements of the ClientSide-Script technology will be checked in the document. |
| UseFailures() | The method indicates that failures rules will be checked in the document. |
| UseGeneral() | The method indicates that general rules will be checked in the document. |
| AllLevels() | The method sets all criteria levels and indicates that the document will be checked according to the criteria of all three levels. |
| UseLowestLevel() | The method determines that the document must be validated against low-level criteria. |
| UseMiddleLevel() | The method determines that the document must be validated against mid-level criteria. |
| UseHighestLevel() | The method determines that the document must be validated against high-level criteria. |
The following code snippet is an example of creating the builder and passing it as a parameter to the validator:
1 // Initialize a webAccessibility container
2 WebAccessibility webAccessibility = new WebAccessibility();
3
4 // Create an accessibility validator with object All
5 var validator = webAccessibility.CreateValidator(ValidationBuilder.All);
6
7 // Create an accessibility validator with empty filters, and set's html and css technologies, low and middle levels
8 var validatorHTML = webAccessibility.CreateValidator(ValidationBuilder.None.UseHTML()
9 .UseCSS()
10 .UseLowestLevel()
11 .UseMiddleLevel());
12
13 // Create an accessibility validator with lambda-expression for all levels and only CSS technologies
14 var validatorCSS = webAccessibility.CreateValidator(builder => builder.AllLevels().UseCSS());
15
16 // Create an accessibility validator for all levels of criterion and only img and input elements will be checking
17 var validatorIMG = webAccessibility.CreateValidator(builder => builder.AllLevels()
18 .AllTechnologies()
19 .SetHTMLTags("img", "input"));The
ValidationResult class is the central component of web accessibility validation. It encapsulates the results of accessibility checks, providing a structured way to analyze, manage, and act on the results of validation operations. To use it, instantiate a validator object and invoke the
Validate(document) method, passing in the document to be validated. The method returns a ValidationResult object containing the validation process results.
Key Properties:
| Property | Description |
|---|---|
| Success | Returns a boolean that says whether the validation succeeded. |
| Details | Returns Collection of RuleValidationResult objects containing details about any validation results. |
To run document validation, you need to perform the following steps:
validator object.document) method to validate the document against web accessibility rules.Here is an example of calling a document validation. The following code will write to the console which of the rules passed the test and which did not:
1// Validate HTML against WCAG rules using C#
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
9// Prepare a path to a source HTML file
10string documentPath = Path.Combine(DataDir, "input.html");
11
12// Initialize an object of the HTMLDocument class
13using (HTMLDocument document = new HTMLDocument(documentPath))
14{
15 // Check the document
16 ValidationResult validationResult = validator.Validate(document);
17
18 // Checking for success
19 if (!validationResult.Success)
20 {
21 // Get a list of RuleValidationResult Details
22 foreach (RuleValidationResult detail in validationResult.Details)
23 {
24 Console.WriteLine("{0}:{1} = {2}", detail.Rule.Code, detail.Rule.Description, detail.Success);
25 }
26 }
27}A more complete description can be found in the article Validation Results – Check Web Accessibility in C#.
Aspose.HTML offers free online Web Accessibility Checker. This tool scans web pages, validates them for WCAG compliance, identifies problems, and suggests improvements. Get instant insights into your website’s compliance, allowing you to determine the scope of necessary corrections and the gap between the current state of your website or HTML document and WCAG requirements.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.