Skip to content

Commit

Permalink
chore(conf): add audit config
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Oct 10, 2023
1 parent da5ade8 commit 2644363
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
The Rust web accessibility engine.

```rs
// pass in html and css rules prior. If css rules are not passed in internal extraction is performed.
let audit = accessibility_rs::audit(&html, &css_rules, false);
let audit = accessibility_rs::audit(&AuditConfig::new(&html, &css, false, "en"));
```

## Features
Expand Down
2 changes: 1 addition & 1 deletion accessibility-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "accessibility-rs"
version = "0.0.7"
version = "0.0.8"
authors = ["The A11yWatch Project Developers", "Jeff Mendez <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
7 changes: 3 additions & 4 deletions accessibility-rs/src/i18n/locales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ impl Langs {
/// the context of the issue
impl Messages {
/// create a new message
pub fn new(en: M, es: M, de: M, ja: M) -> Messages
{
pub fn new(en: M, es: M, de: M, ja: M) -> Messages {
Messages {
en,
es,
Expand Down Expand Up @@ -107,8 +106,8 @@ lazy_static! {
pub static ref LOCALES: BTreeMap<&'static str, Messages> = {
BTreeMap::from([
(Techniques::H25.pairs()[0], Messages::new(
"A title should be provided for the document, using a non-empty title element in the head section.",
"Se debe proporcionar un título para el documento, utilizando un elemento de título no vacío en la sección head.",
"A title should be provided for the document, using a non-empty title element in the head section.",
"Se debe proporcionar un título para el documento, utilizando un elemento de título no vacío en la sección head.",
"",
"head セクションの空でない title 要素を使って、文書にタイトルをつけるべきです。"
)),
Expand Down
46 changes: 42 additions & 4 deletions accessibility-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,53 @@ pub use crate::engine::audit::auditor::Auditor;
pub use crate::engine::issue::Issue;
pub use accessibility_scraper::ElementRef;

/// configs for the audit
#[derive(Default)]
pub struct AuditConfig {
/// the html source code
pub html: &'static str,
/// the css rules to apply
pub css: &'static str,
/// extract bounding box of elements
pub bounding_box: bool,
/// the locale of the audit translations
pub locale: &'static str,
}

impl AuditConfig {
/// a new audit configuration
pub fn new(
html: &'static str,
css: &'static str,
bounding_box: bool,
locale: &'static 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(html: &str, css_rules: &str, bounding_box: bool) -> Vec<Issue> {
let document = accessibility_scraper::Html::parse_document(html);
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,
&css_rules,
&config.css,
engine::styles::css_cache::build_matching_context(&mut nth_index_cache),
bounding_box,
config.bounding_box,
);
engine::audit::wcag::WCAG3AA::audit(&auditor)
}
3 changes: 2 additions & 1 deletion accessibility-rs/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Test suite for handling accessibility in Rust.
mod mocks;
use accessibility_rs::AuditConfig;
use mocks::mock;

#[test]
fn _audit() {
let _ = accessibility_rs::audit(mock::MOCK_WEBSITE_HTML, &mock::MOCK_CSS_RULES, false);
let _ = accessibility_rs::audit(&AuditConfig::new(mock::MOCK_WEBSITE_HTML, &mock::MOCK_CSS_RULES, false, "en"));
}
142 changes: 70 additions & 72 deletions accessibility-rs/tests/unit/meta.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Test for meta data.
use accessibility_rs::AuditConfig;

use crate::mocks::mock;

#[test]
/// missing title element
fn _audit_missing_headers() {
let audit = accessibility_rs::audit(mock::MOCK_WEBSITE_HTML, &"", false);
let audit = accessibility_rs::audit(&AuditConfig::basic(mock::MOCK_WEBSITE_HTML));
let mut valid = true;

for x in &audit {
Expand All @@ -21,25 +23,23 @@ fn _audit_missing_headers() {
#[test]
/// meta refresh redirect
fn _audit_meta_refresh() {
let audit = accessibility_rs::audit(
let audit = accessibility_rs::audit(&AuditConfig::basic(
r###"<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Do not use this!</title>
<meta http-equiv="refresh"
content="5; url=http://www.example.com/newpage" />
</head>
<body>
<p>
If your browser supports Refresh, you'll be
transported to our
<a href="http://www.example.com/newpage">new site</a>
in 5 seconds, otherwise, select the link manually.
</p>
</body>
</html>"###,
&"",
false,
);
<head>
<title>Do not use this!</title>
<meta http-equiv="refresh"
content="5; url=http://www.example.com/newpage" />
</head>
<body>
<p>
If your browser supports Refresh, you'll be
transported to our
<a href="http://www.example.com/newpage">new site</a>
in 5 seconds, otherwise, select the link manually.
</p>
</body>
</html>"###,
));
let mut valid = true;

for x in &audit {
Expand All @@ -51,18 +51,16 @@ fn _audit_meta_refresh() {

assert_eq!(valid, false);

let audit = accessibility_rs::audit(
let audit = accessibility_rs::audit(&AuditConfig::basic(
r###"<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>HTML Techniques for WCAG 2.0</title>
<meta http-equiv="refresh" content="60" />
</head>
<body>
</body>
</html>"###,
&"",
false,
);
<head>
<title>HTML Techniques for WCAG 2.0</title>
<meta http-equiv="refresh" content="60" />
</head>
<body>
</body>
</html>"###,
));
let mut valid = true;

for x in &audit {
Expand All @@ -78,18 +76,16 @@ fn _audit_meta_refresh() {
#[test]
/// no blink elements
fn _audit_blink_found() {
let audit = accessibility_rs::audit(
let audit = accessibility_rs::audit(&AuditConfig::basic(
r###"<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Do not use this!</title>
</head>
<body>
<p>My Great Product <blink>Sale! $44,995!</blink></p>
</body>
</html>"###,
&"",
false,
);
<head>
<title>Do not use this!</title>
</head>
<body>
<p>My Great Product <blink>Sale! $44,995!</blink></p>
</body>
</html>"###,
));
let mut valid = true;

for x in &audit {
Expand All @@ -105,8 +101,8 @@ fn _audit_blink_found() {
#[test]
/// iframe missing title
fn _iframe_missing_title() {
let audit = accessibility_rs::audit(
r###"<html xmlns="http://www.w3.org/1999/xhtml">
let audit = accessibility_rs::audit(&AuditConfig {
html: r###"<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>A simple frameset document</title>
</head>
Expand All @@ -121,9 +117,8 @@ fn _iframe_missing_title() {
</noframes>
</frameset>
</html>"###,
&"",
false,
);
..Default::default()
});
let mut valid = true;

for x in &audit {
Expand All @@ -134,26 +129,27 @@ fn _iframe_missing_title() {
}

assert_eq!(valid, true);

let audit = accessibility_rs::audit(
let config = AuditConfig::new(
r###"<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>A simple frameset document</title>
</head>
<frameset cols="10%, 90%">
<frame src="nav.html" />
<frame src="doc.html" />
<noframes>
<body>
<a href="lib.html" title="Library link">Select to
go to the electronic library</a>
</body>
</noframes>
</frameset>
</html>"###,
<head>
<title>A simple frameset document</title>
</head>
<frameset cols="10%, 90%">
<frame src="nav.html" />
<frame src="doc.html" />
<noframes>
<body>
<a href="lib.html" title="Library link">Select to
go to the electronic library</a>
</body>
</noframes>
</frameset>
</html>"###,
&"",
false,
"en",
);
let audit = accessibility_rs::audit(&config);
let mut valid = true;

for x in &audit {
Expand All @@ -164,20 +160,22 @@ fn _iframe_missing_title() {
}

assert_eq!(valid, false);

let audit = accessibility_rs::audit(
let config = AuditConfig::new(
r###" <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>A document using iframe</title>
</head>
<iframe src="banner-ad.html" id="testiframe"
name="testiframe" title="Advertisement">
<a href="banner-ad.html">Advertisement</a>
</iframe>
</html>"###,
<head>
<title>A document using iframe</title>
</head>
<iframe src="banner-ad.html" id="testiframe"
name="testiframe" title="Advertisement">
<a href="banner-ad.html">Advertisement</a>
</iframe>
</html>"###,
&"",
false,
"en",
);

let audit = accessibility_rs::audit(&config);
let mut valid = true;

for x in &audit {
Expand Down

0 comments on commit 2644363

Please sign in to comment.