Recursive AI

Export HTML accessibility findings as SARIF in C#

onehtml is a free, open-source HTML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

SARIF stores findings and source locations as JSON so code-scanning tools can display them. This check runs without a browser or JavaScript.

Terminal
dotnet add package RecursiveAI.OneHtml.Accessibility.Validation

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Accessibility
    .Validation;
 
var path = "page.html";
var doc = Document.Parse(
    File.ReadAllBytes(path));
var report = new Checker().Check(doc);
var json = report.ToSarifJson(
    new ExportOptions { Url = path });
File.WriteAllText("page.sarif", json);
Console.WriteLine("Wrote page.sarif");

Unzip the example, then dotnet run.

Prints Wrote page.sarif and saves the report beside page.html, which is included in the download:

HTML
<!doctype html>
<html lang="en">
<head><title>Trail guide</title></head>
<body>
  <main>
    <h1>Trail guide</h1>
    <img src="map.png">
  </main>
</body>
</html>

The checker can spot the missing image description from the markup alone; you don’t need map.png to run the example.

The full program

The download writes indented JSON and reads back one finding so you can see the rule ID and the source line it points to.

C#
using System.Text.Json;
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Accessibility
    .Validation;
 
var path = "page.html";
var doc = Document.Parse(
    File.ReadAllBytes(path));
var report = new Checker().Check(doc);
var options = new ExportOptions
{
    Url = "page.html",
    Indented = true
};
var json = report.ToSarifJson(options);
File.WriteAllText("page.sarif", json);
Console.WriteLine("Wrote page.sarif");
 
using var sarif = JsonDocument.Parse(json);
var root = sarif.RootElement;
var run = root.GetProperty("runs")[0];
var finding = run.GetProperty("results").EnumerateArray()
    .First(item => item.GetProperty("ruleId").GetString() == "image-alt");
var location = finding.GetProperty("locations")[0].GetProperty("physicalLocation");
var uri = location.GetProperty("artifactLocation").GetProperty("uri");
var line = location.GetProperty("region").GetProperty("startLine");
Console.WriteLine($"SARIF {root.GetProperty("version")}");
Console.WriteLine($"{finding.GetProperty("ruleId")}: {finding.GetProperty("kind")} at {uri}:{line}");

dotnet run prints:

Text
Wrote page.sarif
SARIF 2.1.0
image-alt: fail at page.html:7

If a finding has no source range, its SARIF result has no locations; check for that field before reading it in code that processes arbitrary findings.

Variations

Replace the options in the full program to include passed results as well as findings:

C#
var options = new ExportOptions
{
    Url = "page.html",
    Indented = true,
    IncludePassed = true
};

The sample report contains 16 kind: pass results with this option and none without it.

The short answer

Read the HTML as bytes, parse it with Document.Parse, and pass the document to Checker.Check. Call ToSarifJson on the returned report, set ExportOptions.Url to identify the original HTML, and write the returned string to a .sarif file. The checker produces the findings; the export puts them into a format another tool can read. Neither step uploads a report.

What is in a SARIF file?

SARIF 2.1.0 stands for Static Analysis Results Interchange Format. The top-level version identifies the format, not the NuGet package version. In a onehtml export, runs[0].tool.driver identifies the checker and its rules, runs[0].artifacts identifies the checked HTML, and runs[0].results contains the findings. It’s possible for a rule to appear in the rule list even if it produces no exported results.

A result has a ruleId and a message.text. For the image in this recipe, ruleId is image-alt. The line identifies the markup to inspect, not what the image should say. Whether the map needs a text alternative and what that text should convey depend on its purpose on the page.

How do SARIF results point to the original HTML?

Set ExportOptions.Url to the path or URL you want readers to open. Use a path relative to where the report consumer expects to find the HTML, not an absolute path from your machine. If you leave Url unset, the export uses an identifier based on a hash of the input bytes rather than a file path. Setting Url labels the input; it doesn’t fetch it.

When a location exists, the region uses one-based line and column numbers, with columns counted in UTF-16 code units. It also supplies a byte offset and length into the original input. Use those byte ranges against the file you checked, not a copy with normalized line endings or a different encoding. For an element, the region usually points to its start tag, as it does for the image here.

Which accessibility findings appear in the SARIF export?

By default, failed checks are written as kind: fail. Checks that need human review or rendered facts are written as kind: review with level: none. If you filter solely by error or warning level you can miss those review results; use kind and properties.outcome to separate definite failures from work still to do. Passed and inapplicable results are omitted unless requested explicitly.

This is a report on the HTML and facts supplied, not certification of an accessible page. For example, when a page adds controls through JavaScript, checking the original HTML can miss those controls altogether. For the full field mapping, see the SARIF export docs.

All onehtml recipes