Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.HTML for .NET 提供了 Aspose.Html.Accessibility 命名空间,该命名空间用于所有与网页无障碍相关的操作和检查。本文将介绍在检查网站是否符合 WCAG 标准时帮助收集错误和警告信息的类和接口。我们将特别关注失败标准和报告错误的方法,并查看在验证 HTML 文档后获取网页无障碍错误的 C# 示例。
您可以从 无障碍验证器 – C# 中的网站无障碍检查 文章中了解如何使用 AccessibilityValidator 类以编程方式检查网页无障碍性。
检测错误有助于识别可能阻止残障人士有效使用和与网页内容交互的障碍。错误和警告指导必须解决哪些问题以改善网页无障碍性。它们为开发人员和设计师提供了进行必要更改的路线图。
IError 是一个包含验证错误信息的公共接口。这意味着一个错误的检查,即规则未通过 – 其结果与无障碍性不匹配。
| 属性 | 描述 |
|---|---|
| ErrorMessage | 返回错误或警告的字符串消息。 |
| Target | 返回发现错误的 HTML 或 CSS 元素。返回的对象类型为 Target。 |
| ErrorTypeName | 错误对象呈现的描述。它有两个选项:“Error"或"Warning”。 |
| ErrorType | 返回错误类型数值。 |
| Success | 此对象的结果。 |
IError 对象具有以下含义:
* ErrorType = 1 且 Success = false – 这意味着错误是严重的,检查结果未执行。
* ErrorType = 2 且 Success = true – 这意味着错误不严重,但具有信息性质并显示可能改进的提示。
错误类型由技术类型决定:
ErrorType=1 和 Success=false,这是一个错误。ErrorType=2 和 Success=true,这是一个警告。Target 是一个公共类,包含发现错误的文档的 HTML 或 CSS 元素项。
| 属性 | 描述 |
|---|---|
| Item | 返回 html 或 css 元素的对象。 |
| TargetType | 返回包含对象的类型。对象类型为 TargetTypes。 |
TargetTypes 是包含错误的 HTML 文档元素类型的枚举:
| 值 | 描述 |
|---|---|
| HTMLElement | 包含文档中 HTMLElement 的元素。 |
| CSSStyleRule | 包含文档中 CSSStyleRule 的元素。 |
| CSSStyleSheet | 包含文档中 CSSStyleSheet 的元素。 |
让我们看一下用于遍历网页无障碍检查结果的 C# 代码,特别关注失败标准和报告错误的方法的详细信息。获取 HTML 文档的错误详细信息和元素的示例:
validationResult.Details 中包含的 ruleResult 对象。这些代表不同的无障碍标准。ruleResult.Errors 中包含的 ruleDetail。
ITechniqueResult 对象表示报告错误的标准的各个规则结果。ruleDetail 获取 error 对象。ruleDetail 表示特定的无障碍问题,error 对象包含有关该问题的信息。target 对象。target 对象通常表示触发无障碍错误的特定 HTML 元素、CSS 规则或其他内容。 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}另请参阅
Aspose.HTML 提供免费的在线 网页无障碍检查器。此工具扫描网页,验证它们是否符合 WCAG 标准,识别问题并提出改进建议。立即了解您网站的合规性,让您能够确定必要更正的范围以及您的网站或 HTML 文档当前状态与 WCAG 要求之间的差距。
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.