From e20f657f4f436c392dfed30cc2634ac8a45b2df1 Mon Sep 17 00:00:00 2001 From: j-mendez Date: Tue, 10 Oct 2023 21:32:02 -0400 Subject: [PATCH] chore(audit): add localization dynamic values setup --- Cargo.lock | 445 +++++++++++++++++- accessibility-rs/Cargo.toml | 1 + accessibility-rs/src/engine/audit/wcag.rs | 14 +- accessibility-rs/src/engine/rules/rule.rs | 48 +- .../src/engine/rules/wcag_rule_map.rs | 22 +- accessibility-rs/src/i18n/locales.rs | 14 +- accessibility-rs/src/lib.rs | 4 + accessibility-rs/tests/integration_test.rs | 7 +- 8 files changed, 527 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de90ce5..edc77e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,7 @@ dependencies = [ "getrandom", "lazy_static", "markup5ever", + "rust-i18n", "selectors", "serde", "slotmap", @@ -33,7 +34,7 @@ dependencies = [ "ego-tree", "getopts", "html5ever", - "indexmap", + "indexmap 2.0.2", "once_cell", "selectors", "smallvec 0.6.14", @@ -50,7 +51,7 @@ dependencies = [ "dtoa 0.4.8", "euclid", "html5ever", - "itoa", + "itoa 0.4.8", "lazy_static", "lock_api 0.1.5", "num-traits", @@ -76,6 +77,30 @@ dependencies = [ "version_check", ] +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + [[package]] name = "arrayvec" version = "0.7.4" @@ -88,6 +113,17 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76f2bfe491d41d45507b8431da8274f7feeca64a49e86d980eed2937ec2ff020" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "0.1.8" @@ -109,6 +145,16 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bstr" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c79ad7fb2dd38f3dabd76b09c6a5a20c038fc0213ef1e9afd30eb777f120f019" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "bumpalo" version = "3.14.0" @@ -127,6 +173,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "2.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -188,7 +249,7 @@ dependencies = [ "autocfg 0.1.8", "cssparser-macros", "dtoa-short", - "itoa", + "itoa 0.4.8", "matches", "phf 0.7.24", "proc-macro2 1.0.69", @@ -271,6 +332,12 @@ dependencies = [ "syn 0.15.44", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "form_urlencoded" version = "1.2.0" @@ -327,12 +394,48 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + +[[package]] +name = "globset" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "759c97c1e17c55525b57192c06a267cda0ac5210b222d6b82189a2338fa1c13d" +dependencies = [ + "aho-corasick", + "bstr", + "fnv", + "log", + "regex", +] + +[[package]] +name = "globwalk" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93e3af942408868f6934a7b85134a3230832b9977cf66125df2f9edcfce4ddcc" +dependencies = [ + "bitflags", + "ignore", + "walkdir", +] + [[package]] name = "grid" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eec1c01eb1de97451ee0d60de7d81cf1e72aabefb021616027f3d1c3ec1c723c" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hashbrown" version = "0.14.1" @@ -345,6 +448,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "html5ever" version = "0.26.0" @@ -369,6 +481,33 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "ignore" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbe7873dab538a9a44ad79ede1faf5f30d49f9a5c883ddbab48bce81b64b7492" +dependencies = [ + "globset", + "lazy_static", + "log", + "memchr", + "regex", + "same-file", + "thread_local", + "walkdir", + "winapi-util", +] + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg 1.1.0", + "hashbrown 0.12.3", +] + [[package]] name = "indexmap" version = "2.0.2" @@ -376,7 +515,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.14.1", +] + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", ] [[package]] @@ -385,6 +533,12 @@ version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + [[package]] name = "js-sys" version = "0.3.64" @@ -406,6 +560,12 @@ version = "0.2.149" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "lock_api" version = "0.1.5" @@ -463,6 +623,12 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + [[package]] name = "memoffset" version = "0.9.0" @@ -490,6 +656,15 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" +[[package]] +name = "normpath" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec60c60a693226186f5d6edf073232bfb6464ed97eb22cf3b01c1e8198fd97f5" +dependencies = [ + "windows-sys", +] + [[package]] name = "num-traits" version = "0.2.17" @@ -848,12 +1023,131 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d119d7c7ca818f8a53c300863d4f87566aac09943aef5b355bb83969dae75d87" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "465c6fc0621e4abc4187a2bda0937bfd4f722c2730b29562e19689ea796c9a4b" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3cbb081b9784b07cceb8824c8583f86db4814d172ab043f3c23f7dc600bf83d" + +[[package]] +name = "rust-i18n" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "722f8b8849ad30f25c77c2b5822d3d13b6bde236b94c2187fc8ab4fb90d1efe6" +dependencies = [ + "anyhow", + "clap", + "globwalk", + "itertools", + "once_cell", + "quote 1.0.33", + "regex", + "rust-i18n-extract", + "rust-i18n-macro", + "rust-i18n-support", + "serde", + "serde_derive", + "toml", +] + +[[package]] +name = "rust-i18n-extract" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86c874262b7b84b4b8e2eb46a3a4ae3b2365ac3d136a431797283d7776261b7a" +dependencies = [ + "anyhow", + "ignore", + "proc-macro2 1.0.69", + "quote 1.0.33", + "regex", + "rust-i18n-support", + "serde", + "serde_json", + "serde_yaml", + "syn 2.0.38", + "toml", +] + +[[package]] +name = "rust-i18n-macro" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "612fa73a13dd7b6d3bd76f564d9632195c3e6a2fd3b52ba9fc99b1800d16f18f" +dependencies = [ + "glob", + "once_cell", + "proc-macro2 1.0.69", + "quote 1.0.33", + "rust-i18n-support", + "serde", + "serde_json", + "serde_yaml", + "syn 2.0.38", +] + +[[package]] +name = "rust-i18n-support" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1f0c8ae29b0544e1f4e16e9e7f7db25a695c8bd64e4bc2f56be2b0ce12734ec" +dependencies = [ + "globwalk", + "lazy_static", + "normpath", + "once_cell", + "proc-macro2 1.0.69", + "regex", + "serde", + "serde_json", + "serde_yaml", + "toml", +] + [[package]] name = "rustversion" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" +[[package]] +name = "ryu" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "scoped-tls" version = "1.0.1" @@ -911,6 +1205,38 @@ dependencies = [ "syn 2.0.38", ] +[[package]] +name = "serde_json" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" +dependencies = [ + "itoa 1.0.9", + "ryu", + "serde", +] + +[[package]] +name = "serde_spanned" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_yaml" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b" +dependencies = [ + "indexmap 1.9.3", + "ryu", + "serde", + "yaml-rust", +] + [[package]] name = "servo_arc" version = "0.1.1" @@ -995,6 +1321,12 @@ dependencies = [ "quote 1.0.33", ] +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "strum" version = "0.25.0" @@ -1070,12 +1402,31 @@ dependencies = [ "utf-8", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thin-slice" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8eaa81235c7058867fa8c0e7314f33dcce9c215f535d1913822a2b3f5e289f3c" +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1091,6 +1442,40 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "toml" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.19.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +dependencies = [ + "indexmap 2.0.2", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "unicode-bidi" version = "0.3.13" @@ -1141,6 +1526,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.9.4" @@ -1156,6 +1547,16 @@ dependencies = [ "syn 0.15.44", ] +[[package]] +name = "walkdir" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1278,12 +1679,30 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.48.5" @@ -1341,6 +1760,15 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" +[[package]] +name = "winnow" +version = "0.5.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711d82167854aff2018dfd193aa0fef5370f456732f0d5a0c59b0f1b4b907" +dependencies = [ + "memchr", +] + [[package]] name = "xi-unicode" version = "0.1.0" @@ -1352,3 +1780,12 @@ name = "xml-rs" version = "0.8.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fcb9cbac069e033553e8bb871be2fbdffcab578eb25bd0f7c508cedc6dcd75a" + +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] diff --git a/accessibility-rs/Cargo.toml b/accessibility-rs/Cargo.toml index 416e52b..ada8c46 100644 --- a/accessibility-rs/Cargo.toml +++ b/accessibility-rs/Cargo.toml @@ -29,6 +29,7 @@ cssparser = { workspace = true } slotmap = "1.0.6" strum = "0.25" strum_macros = "0.25" +rust-i18n = "2" [dev-dependencies] wasm-bindgen-test = "0.3.37" diff --git a/accessibility-rs/src/engine/audit/wcag.rs b/accessibility-rs/src/engine/audit/wcag.rs index 3c099d3..0b4e3dd 100644 --- a/accessibility-rs/src/engine/audit/wcag.rs +++ b/accessibility-rs/src/engine/audit/wcag.rs @@ -23,12 +23,20 @@ impl WCAG3AA { match rules { Some(rules) => { for rule in rules { - let (valid, section, selector) = (rule.validate)(&node.0, &node.1); + let validation = (rule.validate)(&node.0, &node.1); + let valid = validation.valid; + let section = validation.id; + let selector = validation.elements; + let message = validation.message; if !valid { // get locales prior or from document - let message = - get_message(&rule.rule_id, §ion, &Langs::En.as_str()); + let message = if !message.is_empty() { + message + } else { + get_message(&rule.rule_id, §ion, &Langs::En.as_str()) + }; + let issue = Issue::new( message, &node.0, diff --git a/accessibility-rs/src/engine/rules/rule.rs b/accessibility-rs/src/engine/rules/rule.rs index 65c3d5d..4b443ec 100644 --- a/accessibility-rs/src/engine/rules/rule.rs +++ b/accessibility-rs/src/engine/rules/rule.rs @@ -4,6 +4,44 @@ use crate::engine::rules::ids::Techniques; use crate::engine::rules::wcag_base::{Criteria, Guideline, Principle}; use crate::ElementRef; +/// the validation response +#[derive(Default)] +pub struct Validation { + /// is valid + pub valid: bool, + /// the sub-technique + pub id: &'static str, + /// elements that match the issue + pub elements: Vec<&'static str>, + /// the message of the error + pub message: &'static str, +} + +impl Validation { + /// helper to create validation + pub fn new( + valid: bool, + id: &'static str, + elements: Vec<&'static str>, + message: &'static str, + ) -> Self { + Self { + valid, + id, + elements, + message, + } + } + /// basic validation + pub fn new_issue(valid: bool, id: &'static str) -> Self { + Self { + valid, + id, + ..Default::default() + } + } +} + /// the rule validation method that should be performed. pub struct Rule { /// the message id of the rule to point to the locale @@ -11,10 +49,7 @@ pub struct Rule { /// the type of rule pub criteria: Criteria, /// validate a test returns (valid, rule, selectors) - pub validate: fn( - &str, - &Vec<(ElementRef<'_>, Option)>, - ) -> (bool, &'static str, Vec<&'static str>), + pub validate: fn(&str, &Vec<(ElementRef<'_>, Option)>) -> Validation, /// the principle type pub principle: Principle, /// the guideline to follow @@ -28,10 +63,7 @@ impl Rule { criteria: Criteria, principle: Principle, guideline: Guideline, - validate: fn( - &str, - &Vec<(ElementRef<'_>, Option)>, - ) -> (bool, &'static str, Vec<&'static str>), + validate: fn(&str, &Vec<(ElementRef<'_>, Option)>) -> Validation, ) -> Rule { Rule { rule_id, diff --git a/accessibility-rs/src/engine/rules/wcag_rule_map.rs b/accessibility-rs/src/engine/rules/wcag_rule_map.rs index 08a753c..098e31e 100644 --- a/accessibility-rs/src/engine/rules/wcag_rule_map.rs +++ b/accessibility-rs/src/engine/rules/wcag_rule_map.rs @@ -1,5 +1,5 @@ use crate::engine::rules::ids::Techniques; -use crate::engine::rules::rule::Rule; +use crate::engine::rules::rule::{Rule, Validation}; use crate::engine::rules::wcag_base::{Criteria, Guideline, Principle}; use accessibility_scraper::Selector; use std::collections::BTreeMap; @@ -11,12 +11,12 @@ lazy_static! { vec![ ("html", Vec::from([ Rule::new(Techniques::H57, Criteria::Error, Principle::Understandable, Guideline::Readable, |_rule, nodes| { - (!nodes[0].0.attr("lang").unwrap_or_default().is_empty(), "2", Default::default()) + Validation::new_issue(!nodes[0].0.attr("lang").unwrap_or_default().is_empty(), "2") }), Rule::new(Techniques::H57, Criteria::Error, Principle::Understandable, Guideline::Readable, |_rule, nodes| { let lang = nodes[0].0.attr("lang").unwrap_or_default(); // - (lang.chars().all(|x| x.is_alphanumeric()) && !lang.contains("_") && lang.len() < 12, "3.Lang", Default::default()) + Validation::new_issue(lang.chars().all(|x| x.is_alphanumeric()) && !lang.contains("_") && lang.len() < 12, "3.Lang") }), ])), ("meta", Vec::from([ @@ -34,7 +34,7 @@ lazy_static! { } } - (valid, "2", Default::default()) + Validation::new_issue(valid, "2") }), Rule::new(Techniques::F41, Criteria::Error, Principle::Understandable, Guideline::EnoughTime, |_rule, nodes| { let mut valid = true; @@ -50,30 +50,30 @@ lazy_static! { } } - (valid, "2", Default::default()) + Validation::new_issue(valid, "2") }), ])), ("title", Vec::from([ Rule::new(Techniques::H25, Criteria::Error, Principle::Operable, Guideline::Navigable, |_rule, nodes| { - (!nodes.is_empty(), "1.NoTitleEl", Default::default()) + Validation::new_issue(!nodes.is_empty(), "1.NoTitleEl") }), Rule::new(Techniques::H25, Criteria::Error, Principle::Understandable, Guideline::Predictable, |_rule, nodes| { - (nodes.is_empty() || nodes[0].0.html().is_empty(), "1.EmptyTitle", Default::default()) + Validation::new_issue(nodes.is_empty() || nodes[0].0.html().is_empty(), "1.EmptyTitle") }), ])), ("blink", Vec::from([ Rule::new(Techniques::F47, Criteria::Error, Principle::Operable, Guideline::EnoughTime, |_rule, nodes| { - (nodes.is_empty(), "", Default::default()) + Validation::new_issue(nodes.is_empty(), "") }), ])), ("iframe", Vec::from([ Rule::new(Techniques::H64, Criteria::Error, Principle::Operable, Guideline::Navigable, |_rule, nodes| { - (nodes.iter().all(|e| !e.0.attr("title").unwrap_or_default().is_empty()), "", Default::default()) + Validation::new_issue(nodes.iter().all(|e| !e.0.attr("title").unwrap_or_default().is_empty()), "") }), ])), ("frame", Vec::from([ Rule::new(Techniques::H64, Criteria::Error, Principle::Operable, Guideline::Navigable, |_rule, nodes| { - (nodes.iter().all(|e| !e.0.attr("title").unwrap_or_default().is_empty()), "", Default::default()) + Validation::new_issue(nodes.iter().all(|e| !e.0.attr("title").unwrap_or_default().is_empty()), "") }), ])), ("form", Vec::from([ @@ -90,7 +90,7 @@ lazy_static! { }; } - (valid, "2", Default::default()) + Validation::new_issue(valid, "2") }), ])) ] diff --git a/accessibility-rs/src/i18n/locales.rs b/accessibility-rs/src/i18n/locales.rs index 4ca4172..bcefe4c 100644 --- a/accessibility-rs/src/i18n/locales.rs +++ b/accessibility-rs/src/i18n/locales.rs @@ -82,7 +82,7 @@ impl Messages { } } -/// parse +/// parse message translation pub fn get_message(rule_id: &Techniques, section: &str, lang: &str) -> &'static str { let rule_id = rule_id.as_str(); let message = if section.is_empty() { @@ -101,6 +101,18 @@ pub fn get_message(rule_id: &Techniques, section: &str, lang: &str) -> &'static } } +/// get message config type +pub fn get_message_i18n(rule_id: &Techniques, section: &str, lang: &str) -> String { + let rule_id = rule_id.as_str(); + let message = if section.is_empty() { + rule_id.to_string() + } else { + [rule_id, section].join(".").to_string() + }; + + message +} + lazy_static! { /// message for an issue pub static ref LOCALES: BTreeMap<&'static str, Messages> = { diff --git a/accessibility-rs/src/lib.rs b/accessibility-rs/src/lib.rs index 5fe1628..803fa5d 100644 --- a/accessibility-rs/src/lib.rs +++ b/accessibility-rs/src/lib.rs @@ -1,5 +1,7 @@ #[macro_use] extern crate lazy_static; +#[macro_use] +extern crate rust_i18n; /// the main engine for audits. mod engine; @@ -12,6 +14,8 @@ pub use crate::engine::audit::auditor::Auditor; pub use crate::engine::issue::Issue; pub use accessibility_scraper::ElementRef; +i18n!(); + /// configs for the audit #[derive(Default)] pub struct AuditConfig { diff --git a/accessibility-rs/tests/integration_test.rs b/accessibility-rs/tests/integration_test.rs index 41acd9f..5b27699 100644 --- a/accessibility-rs/tests/integration_test.rs +++ b/accessibility-rs/tests/integration_test.rs @@ -6,5 +6,10 @@ use mocks::mock; #[test] fn _audit() { - let _ = accessibility_rs::audit(&AuditConfig::new(mock::MOCK_WEBSITE_HTML, &mock::MOCK_CSS_RULES, false, "en")); + let _ = accessibility_rs::audit(&AuditConfig::new( + mock::MOCK_WEBSITE_HTML, + &mock::MOCK_CSS_RULES, + false, + "en", + )); }