1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#![warn(missing_docs)]
//! Audit html to see how it complies with WCAG
//! standards.
//!
//! accessibility-rs is a web accessibility
//! engine that can replicate websites without
//! a browser to get complex accessibility reports.
//!
//! # How to use accessibility-rs
//!
//! There are a couple of ways to use accessibility-rs:
//!
//! - **Audit** perform an audit against an html page.
//! - [`audit`] is used to audit a web page for issues.
//!
//! [`audit`]: fn.audit.html#method.audit
//!
//! # Examples
//!
//! A basic WCAG audit for a website:
//!
//! ```no_run
//! use accessibility_rs::{audit, AuditConfig};
//!
//! fn main() {
//! let config = AuditConfig::basic(r###"<html><body><h1>My Title</h1><input type="text" placeholder="Type me"></input><img src="tabby_cat.png"></img></body></html>"###);
//! let audit = audit(config);
//! println!("{:?}", audit);
//! }
//! ```
//!
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate rust_i18n;
/// the main engine for accessibility auditing.
pub mod engine;
/// locales for translations.
pub mod i18n;
use crate::engine::audit::auditor::Auditor;
use crate::engine::issue::Issue;
use accessibility_scraper::ElementRef;
i18n!("locales", fallback = "en");
/// support guidelines for auditing
#[derive(Default)]
pub enum Conformance {
/// Level AAA includes all Level A, AA, and AAA requirements
#[default]
WCAGAAA,
}
/// configs for the audit
#[derive(Default)]
pub struct AuditConfig<'a> {
/// the html source code
pub html: &'a str,
/// the css rules to apply
pub css: &'a str,
/// extract bounding box of elements
pub bounding_box: bool,
/// the locale of the audit translations
pub locale: &'a str,
/// the guideline spec
pub conformance: Conformance,
}
impl<'a> AuditConfig<'a> {
/// a new audit configuration
pub fn new(html: &'a str, css: &'a str, bounding_box: bool, locale: &'a str) -> Self {
AuditConfig {
html,
css,
bounding_box,
locale,
..Default::default()
}
}
/// basic audit
pub fn basic(html: &'a str) -> Self {
AuditConfig {
html,
..Default::default()
}
}
}
/// audit a web page passing the html and css rules.
pub fn audit(config: AuditConfig) -> Vec<Issue> {
let document = accessibility_scraper::Html::parse_document(config.html);
let mut nth_index_cache = selectors::NthIndexCache::from(Default::default());
let mut auditor = Auditor::new(
&document,
&config.css,
engine::styles::css_cache::build_matching_context(&mut nth_index_cache),
config.bounding_box,
config.locale,
);
engine::audit::wcag::WCAGAAA::audit(&mut auditor)
}