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
#![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() {
//!     // pass in the html, and css if the page came from a headless browser
//!     let config = AuditConfig::basic("<html>...</html>");
//!     let audit = audit(config);
//!     println!("{:?}", audit);
//! }
//! ```
//!

#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate rust_i18n;

/// the main engine for audits.
mod engine;
/// locales for translations.
mod i18n;

use crate::engine::audit::auditor::Auditor;
use crate::engine::issue::Issue;
use accessibility_scraper::ElementRef;

i18n!("locales");

/// 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,
}

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,
        }
    }

    /// basic audit
    pub fn basic(html: &'static 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 auditor = Auditor::new(
        &document,
        &config.css,
        engine::styles::css_cache::build_matching_context(&mut nth_index_cache),
        config.bounding_box,
    );
    engine::audit::wcag::WCAG3AA::audit(&auditor)
}