Skip to content

Commit

Permalink
feat(cli): add clip options
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Feb 19, 2024
1 parent 588608f commit a473b75
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 17 deletions.
2 changes: 1 addition & 1 deletion kayle_cli/Cargo.lock

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

2 changes: 1 addition & 1 deletion kayle_cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kayle_cli"
version = "0.0.3"
version = "0.0.5"
edition = "2021"
description = "The kayle CLI for web accessibility auditing"
repository = "https://github.com/a11ywatch/kayle"
Expand Down
31 changes: 30 additions & 1 deletion kayle_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,40 @@ Options:
- htmlcs: htmlcs
- axe: axe

-w, --wait-for <WAIT_FOR>
WaitFor event for content to exist

Possible values:
- load: Waits till the window load event
- domcontent-loaded: The dom loaded content first
- commit: Wait for the commit event. Playwright only
- network-idle: Waits till there are no more network connections for at least 500 ms. Playwright only
- network-idle1: Waits till there are no more network connections for at least 500 ms. Puppeteer only
- network-idle2: Waits till there are no more than 2 network connections for at least 500 ms. Puppeteer only

--allow-images <ALLOW_IMAGES>
Allow images to render, useful when setting clip option for bounding box

[possible values: true, false]

--clip <CLIP>
Get the bounding box of an element

[possible values: true, false]

--clip-dir <CLIP_DIR>
The directory to store clip images

--clip-2-base64 <CLIP_2_BASE64>
Convert the clip to a base64 image

[possible values: true, false]

--automation-lib <AUTOMATION_LIB>
The automation lib to use either puppeteer or playwright

Possible values:
- puppeteer: The puppeteer library. Defaults to this
- puppeteer: The puppeteer library. The Default
- playwright: The playwright library by microsoft

-h, --help
Expand Down
120 changes: 114 additions & 6 deletions kayle_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod puppeteer_script;
use serde::{Deserialize, Serialize};

#[derive(Debug, Subcommand, Serialize, Deserialize)]
/// The CLI commands to run.
enum Commands {
/// Upgrade kayle and the dependencies required.
Upgrade,
Expand All @@ -27,7 +28,7 @@ enum Commands {
#[derive(Debug, Default, Clone, PartialEq, ValueEnum, Serialize, Deserialize)]
enum AutomationLib {
#[default]
/// The puppeteer library. Defaults to this.
/// The puppeteer library. The Default.
Puppeteer,
/// The playwright library by microsoft
Playwright,
Expand Down Expand Up @@ -72,7 +73,7 @@ enum AccessibilityRunner {
}

impl AccessibilityRunner {
/// get the standard to string
/// get the runner to string
pub fn to_str(&self) -> &'static str {
match self {
AccessibilityRunner::Kayle => "kayle",
Expand All @@ -82,6 +83,56 @@ impl AccessibilityRunner {
}
}

/// Wait for events.
#[derive(Debug, Default, Clone, PartialEq, ValueEnum, Serialize, Deserialize)]
enum WaitFor {
#[default]
/// Waits till the window load event.
Load,
/// The dom loaded content first
DomcontentLoaded,
/// Wait for the commit event. Playwright only.
Commit,
/// Waits till there are no more network connections for at least 500 ms. Playwright only.
NetworkIdle,
/// Waits till there are no more network connections for at least 500 ms. Puppeteer only.
NetworkIdle1,
/// Waits till there are no more than 2 network connections for at least 500 ms. Puppeteer only.
NetworkIdle2,
}

impl WaitFor {
/// get the wait_for event to string
pub fn to_str(&self, puppeteer: bool) -> &'static str {
match self {
WaitFor::Load => "load",
WaitFor::DomcontentLoaded => "domcontentloaded",
WaitFor::Commit => if puppeteer { "networkidle2" } else { "commit" },
WaitFor::NetworkIdle => {
if puppeteer {
"networkidle1"
} else {
"networkidle"
}
}
WaitFor::NetworkIdle1 => {
if puppeteer {
"networkidle1"
} else {
"networkidle"
}
}
WaitFor::NetworkIdle2 => {
if puppeteer {
"networkidle2"
} else {
"networkidle"
}
}
}
}
}

/// Web Accessibility Auditing using the kayle engine
#[derive(Parser, Serialize, Deserialize, Debug)]
#[command(version, about, long_about = None)]
Expand All @@ -98,9 +149,25 @@ struct Args {
/// The accessibility runner to use htmlcs, axecore, or kayle.
#[arg(short, long)]
runners: Option<Vec<AccessibilityRunner>>,
/// WaitFor event for content to exist.
#[arg(short, long, value_enum)]
wait_for: Option<WaitFor>,
#[arg(long)]
/// Allow images to render, useful when setting clip option for bounding box.
allow_images: Option<bool>,
#[arg(long)]
/// Get the bounding box of an element.
clip: Option<bool>,
#[arg(long)]
/// The directory to store clip images.
clip_dir: Option<String>,
#[arg(long)]
/// Convert the clip to a base64 image.
clip_2_base64: Option<bool>,
#[arg(long, value_enum)]
/// The automation lib to use either puppeteer or playwright.
automation_lib: Option<AutomationLib>,
/// The commands for the CLI.
#[command(subcommand)]
command: Commands,
}
Expand Down Expand Up @@ -197,6 +264,40 @@ fn main() -> io::Result<()> {
.map(|r| r.to_str())
.collect::<Vec<&'static str>>()
.join(",");
let wait_for = if args.wait_for.is_some() {
args.wait_for
} else {
loaded_config.wait_for
}
.unwrap_or_default();

let allow_images = if args.allow_images.is_some() {
args.allow_images
} else {
loaded_config.allow_images
}
.unwrap_or_default();

let clip = if args.clip.is_some() {
args.clip
} else {
loaded_config.clip
}
.unwrap_or_default();

let clip_dir = if args.clip_dir.is_some() {
args.clip_dir
} else {
loaded_config.clip_dir
}
.unwrap_or_default();

let clip_2_base64 = if args.clip_2_base64.is_some() {
args.clip_2_base64
} else {
loaded_config.clip_2_base64
}
.unwrap_or_default();

let headless_script = if puppeteer {
puppeteer_script::SCRIPT_EXECUTION
Expand All @@ -208,12 +309,19 @@ fn main() -> io::Result<()> {
Command::new("node")
.args([
"-e",
headless_script,
u.to_str().unwrap(),
&accessibility_standard.to_str(),
headless_script,
u.to_str().unwrap(),
&accessibility_standard.to_str(),
if include_notices { "true"} else { "false" },
if include_warnings { "true"} else { "false" },
&runners])
&runners,
&wait_for.to_str(puppeteer),
if allow_images { "true"} else { "false" },
if clip { "true"} else { "false" },
&clip_dir,
if clip_2_base64 { "true"} else { "false" },

])
.status()
.expect(if puppeteer {
"Failed to execute node command - make sure node and puppeteer is installed."
Expand Down
13 changes: 9 additions & 4 deletions kayle_cli/src/playwright_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ const args = argv.slice(1);
const audit = await kayle({
page,
browser,
runners: runners.length && runners[0] ? runners : ["htmlcs"],
includeWarnings: args[3] === "true",
includeNotices: args[2] === "true",
standard: args[1] || Standard.WCAG2AA,
origin: args[0],
standard: args[1] || Standard.WCAG2AA,
includeNotices: args[2] === "true",
includeWarnings: args[3] === "true",
runners: runners.length && runners[0] ? runners : ["htmlcs"],
waitUntil: args[5],
allowImages: args[6] === "true",
clip: args[7] === "true",
clipDir: args[8],
clip2Base64: args[9],
});
stdout.write(JSON.stringify(audit));
Expand Down
13 changes: 9 additions & 4 deletions kayle_cli/src/puppeteer_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ const args = argv.slice(1);
const audit = await kayle({
page,
browser,
runners: runners.length && runners[0] ? runners : ["htmlcs"],
includeWarnings: args[3] === "true",
includeNotices: args[2] === "true",
standard: args[1] || Standard.WCAG2AA,
origin: args[0],
standard: args[1] || Standard.WCAG2AA,
includeNotices: args[2] === "true",
includeWarnings: args[3] === "true",
runners: runners.length && runners[0] ? runners : ["htmlcs"],
waitUntil: args[5],
allowImages: args[6] === "true",
clip: args[7] === "true",
clipDir: args[8],
clip2Base64: args[9],
});
stdout.write(JSON.stringify(audit));
Expand Down

0 comments on commit a473b75

Please sign in to comment.