auditmysite 1.0.0

Resource-efficient WCAG 2.1 Accessibility Checker in Rust
Documentation
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! Structured data (Schema.org) detection
//!
//! Detects JSON-LD, Microdata, and RDFa structured data.

use chromiumoxide::Page;
use serde::{Deserialize, Serialize};
use tracing::info;

use crate::error::{AuditError, Result};

/// Structured data analysis
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StructuredData {
    /// JSON-LD scripts found
    pub json_ld: Vec<JsonLdSchema>,
    /// Schema types detected
    pub types: Vec<SchemaType>,
    /// Has any structured data
    pub has_structured_data: bool,
    /// Rich snippets potential
    pub rich_snippets_potential: Vec<String>,
    /// Validation issues: missing required properties per schema block
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub schema_issues: Vec<SchemaIssue>,
}

/// A required-property validation issue for a JSON-LD block
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaIssue {
    /// The @type of the affected schema block
    pub schema_type: String,
    /// Whether the missing property is required or recommended for rich-result quality
    pub severity: SchemaIssueSeverity,
    /// Machine-readable issue key
    pub issue_type: String,
    /// Human-readable description
    pub message: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SchemaIssueSeverity {
    Required,
    Recommended,
}

/// JSON-LD schema data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonLdSchema {
    /// Schema @type
    pub schema_type: String,
    /// All schema @type values found on the root object
    #[serde(default)]
    pub schema_types: Vec<String>,
    /// Raw JSON content
    pub content: serde_json::Value,
    /// Is valid JSON-LD
    pub is_valid: bool,
}

/// Known schema types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SchemaType {
    Organization,
    LocalBusiness,
    Person,
    Article,
    BlogPosting,
    NewsArticle,
    Product,
    Offer,
    Event,
    Recipe,
    VideoObject,
    WebPage,
    WebSite,
    BreadcrumbList,
    FAQPage,
    HowTo,
    Review,
    AggregateRating,
    Other(String),
}

impl std::str::FromStr for SchemaType {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        Ok(match s {
            "Organization" => SchemaType::Organization,
            "LocalBusiness" => SchemaType::LocalBusiness,
            "Person" => SchemaType::Person,
            "Article" => SchemaType::Article,
            "BlogPosting" => SchemaType::BlogPosting,
            "NewsArticle" => SchemaType::NewsArticle,
            "Product" => SchemaType::Product,
            "Offer" => SchemaType::Offer,
            "Event" => SchemaType::Event,
            "Recipe" => SchemaType::Recipe,
            "VideoObject" => SchemaType::VideoObject,
            "WebPage" => SchemaType::WebPage,
            "WebSite" => SchemaType::WebSite,
            "BreadcrumbList" => SchemaType::BreadcrumbList,
            "FAQPage" => SchemaType::FAQPage,
            "HowTo" => SchemaType::HowTo,
            "Review" => SchemaType::Review,
            "AggregateRating" => SchemaType::AggregateRating,
            other => SchemaType::Other(other.to_string()),
        })
    }
}

impl SchemaType {
    pub fn as_str(&self) -> &str {
        match self {
            Self::Organization => "Organization",
            Self::LocalBusiness => "LocalBusiness",
            Self::Person => "Person",
            Self::Article => "Article",
            Self::BlogPosting => "BlogPosting",
            Self::NewsArticle => "NewsArticle",
            Self::Product => "Product",
            Self::Offer => "Offer",
            Self::Event => "Event",
            Self::Recipe => "Recipe",
            Self::VideoObject => "VideoObject",
            Self::WebPage => "WebPage",
            Self::WebSite => "WebSite",
            Self::BreadcrumbList => "BreadcrumbList",
            Self::FAQPage => "FAQPage",
            Self::HowTo => "HowTo",
            Self::Review => "Review",
            Self::AggregateRating => "AggregateRating",
            Self::Other(s) => s,
        }
    }

    pub fn is_organization_like(&self) -> bool {
        matches!(self, Self::Organization | Self::LocalBusiness)
            || matches!(
                self.as_str(),
                "Airline"
                    | "Consortium"
                    | "Corporation"
                    | "EducationalOrganization"
                    | "FundingScheme"
                    | "GovernmentOrganization"
                    | "LibrarySystem"
                    | "LocalBusiness"
                    | "MedicalOrganization"
                    | "NGO"
                    | "NewsMediaOrganization"
                    | "OnlineBusiness"
                    | "PerformingGroup"
                    | "Project"
                    | "ResearchOrganization"
                    | "SearchRescueOrganization"
                    | "SportsOrganization"
                    | "WorkersUnion"
                    | "LegalService"
                    | "ProfessionalService"
            )
    }

    pub fn rich_snippet_type(&self) -> Option<&'static str> {
        match self {
            SchemaType::Article | SchemaType::BlogPosting | SchemaType::NewsArticle => {
                Some("Article Rich Snippet")
            }
            SchemaType::Product => Some("Product Rich Snippet"),
            SchemaType::Recipe => Some("Recipe Rich Snippet"),
            SchemaType::Event => Some("Event Rich Snippet"),
            SchemaType::FAQPage => Some("FAQ Rich Snippet"),
            SchemaType::HowTo => Some("How-To Rich Snippet"),
            SchemaType::Review | SchemaType::AggregateRating => Some("Review Rich Snippet"),
            SchemaType::BreadcrumbList => Some("Breadcrumb Rich Snippet"),
            SchemaType::VideoObject => Some("Video Rich Snippet"),
            SchemaType::LocalBusiness => Some("Local Business Rich Snippet"),
            _ => None,
        }
    }
}

/// Detect structured data on a page
pub async fn detect_structured_data(page: &Page) -> Result<StructuredData> {
    info!("Detecting structured data...");

    let js_code = r#"
    (() => {
        const result = { jsonLd: [], microdata: false, rdfa: false };

        // Find JSON-LD scripts
        document.querySelectorAll('script[type="application/ld+json"]').forEach(script => {
            try {
                const content = JSON.parse(script.textContent);
                result.jsonLd.push(content);
            } catch (e) {
                result.jsonLd.push({ error: 'Invalid JSON', raw: script.textContent.substring(0, 200) });
            }
        });

        // Check for microdata
        result.microdata = document.querySelectorAll('[itemscope]').length > 0;

        // Check for RDFa
        result.rdfa = document.querySelectorAll('[typeof], [property]').length > 0;

        return JSON.stringify(result);
    })()
    "#;

    let js_result = page
        .evaluate(js_code)
        .await
        .map_err(|e| AuditError::CdpError(format!("Structured data detection failed: {}", e)))?;

    let json_str = js_result.value().and_then(|v| v.as_str()).unwrap_or("{}");

    let parsed: serde_json::Value = serde_json::from_str(json_str).unwrap_or_default();

    let mut json_ld = Vec::new();
    let mut types = Vec::new();
    let mut rich_snippets_potential = Vec::new();

    // Parse JSON-LD schemas
    if let Some(schemas) = parsed["jsonLd"].as_array() {
        for schema in schemas {
            let is_valid = schema.get("error").is_none();

            // Extract @type (can be string or array)
            let schema_types = extract_types(schema);

            for type_str in &schema_types {
                let schema_type: SchemaType = type_str
                    .parse()
                    .unwrap_or(SchemaType::Other(type_str.to_string()));

                if let Some(rich_snippet) = schema_type.rich_snippet_type() {
                    if !rich_snippets_potential.contains(&rich_snippet.to_string()) {
                        rich_snippets_potential.push(rich_snippet.to_string());
                    }
                }

                if !types.contains(&schema_type) {
                    types.push(schema_type);
                }
            }

            json_ld.push(JsonLdSchema {
                schema_type: schema_types.first().cloned().unwrap_or_default(),
                schema_types,
                content: schema.clone(),
                is_valid,
            });

            // Expand @graph items into separate entries so property validation
            // can check each typed item individually
            if is_valid {
                if let Some(graph) = schema["@graph"].as_array() {
                    for graph_item in graph {
                        let item_types: Vec<String> = if let Some(s) = graph_item["@type"].as_str()
                        {
                            vec![s.to_string()]
                        } else if let Some(arr) = graph_item["@type"].as_array() {
                            arr.iter()
                                .filter_map(|v| v.as_str().map(String::from))
                                .collect()
                        } else {
                            vec![]
                        };
                        if item_types.is_empty() {
                            continue;
                        }
                        json_ld.push(JsonLdSchema {
                            schema_type: item_types.first().cloned().unwrap_or_default(),
                            schema_types: item_types,
                            content: graph_item.clone(),
                            is_valid: true,
                        });
                    }
                }
            }
        }
    }

    let has_structured_data = !json_ld.is_empty()
        || parsed["microdata"].as_bool().unwrap_or(false)
        || parsed["rdfa"].as_bool().unwrap_or(false);

    info!(
        "Structured data: {} JSON-LD schemas, {} types, {} rich snippet opportunities",
        json_ld.len(),
        types.len(),
        rich_snippets_potential.len()
    );

    let schema_issues = json_ld
        .iter()
        .flat_map(validate_schema_properties)
        .collect();

    Ok(StructuredData {
        json_ld,
        types,
        has_structured_data,
        rich_snippets_potential,
        schema_issues,
    })
}

fn validate_schema_properties(schema: &JsonLdSchema) -> Vec<SchemaIssue> {
    if !schema.is_valid {
        return vec![];
    }

    let required: &[(&str, &[&str])] = &[
        ("Article", &["headline", "image", "author", "datePublished"]),
        (
            "BlogPosting",
            &["headline", "image", "author", "datePublished"],
        ),
        (
            "NewsArticle",
            &["headline", "image", "author", "datePublished"],
        ),
        ("Product", &["name", "image", "offers"]),
        ("FAQPage", &["mainEntity"]),
        ("BreadcrumbList", &["itemListElement"]),
        ("LocalBusiness", &["name", "address"]),
        ("Organization", &["name", "url"]),
        ("Event", &["name", "startDate", "location"]),
        (
            "VideoObject",
            &["name", "description", "thumbnailUrl", "uploadDate"],
        ),
        (
            "Recipe",
            &["name", "image", "recipeIngredient", "recipeInstructions"],
        ),
        ("HowTo", &["name"]),
        ("Review", &["author"]),
    ];
    let recommended: &[(&str, &[&str])] = &[
        (
            "Article",
            &[
                "dateModified",
                "publisher",
                "description",
                "mainEntityOfPage",
            ],
        ),
        (
            "BlogPosting",
            &[
                "dateModified",
                "publisher",
                "description",
                "mainEntityOfPage",
            ],
        ),
        (
            "NewsArticle",
            &[
                "dateModified",
                "publisher",
                "description",
                "mainEntityOfPage",
            ],
        ),
        (
            "Product",
            &["description", "sku", "brand", "aggregateRating", "review"],
        ),
        ("FAQPage", &[]),
        ("BreadcrumbList", &[]),
        (
            "LocalBusiness",
            &["telephone", "url", "openingHours", "geo", "aggregateRating"],
        ),
        ("Organization", &["logo", "sameAs", "contactPoint"]),
        (
            "Event",
            &[
                "endDate",
                "image",
                "description",
                "offers",
                "performer",
                "eventStatus",
                "eventAttendanceMode",
            ],
        ),
        (
            "Recipe",
            &[
                "author",
                "datePublished",
                "prepTime",
                "cookTime",
                "totalTime",
                "nutrition",
                "aggregateRating",
                "video",
            ],
        ),
    ];

    let types_to_check: Vec<String> = if schema.schema_types.is_empty() {
        schema
            .schema_type
            .split('/')
            .next_back()
            .map(|s| vec![s.to_string()])
            .unwrap_or_default()
    } else {
        schema
            .schema_types
            .iter()
            .map(|t| t.split('/').next_back().unwrap_or(t).to_string())
            .collect()
    };

    let mut issues = Vec::new();
    for type_name in types_to_check {
        if let Some((_, props)) = required.iter().find(|(name, _)| *name == type_name) {
            for prop in *props {
                if !has_schema_property(&schema.content, prop) {
                    issues.push(SchemaIssue {
                        schema_type: type_name.to_string(),
                        severity: SchemaIssueSeverity::Required,
                        issue_type: format!("schema_missing_{}", prop),
                        message: format!(
                            "{}: Pflichtfeld \"{}\" fehlt im JSON-LD",
                            type_name, prop
                        ),
                    });
                }
            }
        }
        if let Some((_, props)) = recommended.iter().find(|(name, _)| *name == type_name) {
            for prop in *props {
                if !has_schema_property(&schema.content, prop) {
                    issues.push(SchemaIssue {
                        schema_type: type_name.to_string(),
                        severity: SchemaIssueSeverity::Recommended,
                        issue_type: format!("schema_recommended_missing_{}", prop),
                        message: format!(
                            "{}: Empfohlenes Feld \"{}\" fehlt im JSON-LD",
                            type_name, prop
                        ),
                    });
                }
            }
        }
    }
    issues
}

fn has_schema_property(content: &serde_json::Value, prop: &str) -> bool {
    match content.get(prop) {
        Some(serde_json::Value::Null) | None => false,
        Some(serde_json::Value::String(s)) => !s.trim().is_empty(),
        Some(serde_json::Value::Array(items)) => !items.is_empty(),
        Some(serde_json::Value::Object(map)) => !map.is_empty(),
        Some(_) => true,
    }
}

fn extract_types(schema: &serde_json::Value) -> Vec<String> {
    let mut types = Vec::new();

    if let Some(type_str) = schema["@type"].as_str() {
        types.push(type_str.to_string());
    } else if let Some(type_arr) = schema["@type"].as_array() {
        for t in type_arr {
            if let Some(s) = t.as_str() {
                types.push(s.to_string());
            }
        }
    }

    // Also check @graph
    if let Some(graph) = schema["@graph"].as_array() {
        for item in graph {
            types.extend(extract_types(item));
        }
    }

    types
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_schema_type_from_str() {
        assert_eq!(
            "Article".parse::<SchemaType>().unwrap(),
            SchemaType::Article
        );
        assert_eq!(
            "Product".parse::<SchemaType>().unwrap(),
            SchemaType::Product
        );
        assert!(matches!(
            "CustomType".parse::<SchemaType>().unwrap(),
            SchemaType::Other(_)
        ));
    }

    #[test]
    fn test_rich_snippet_type() {
        assert_eq!(
            SchemaType::Article.rich_snippet_type(),
            Some("Article Rich Snippet")
        );
        assert_eq!(
            SchemaType::Product.rich_snippet_type(),
            Some("Product Rich Snippet")
        );
        assert_eq!(SchemaType::Organization.rich_snippet_type(), None);
    }

    #[test]
    fn organization_like_accepts_schema_subtypes() {
        assert!(SchemaType::Organization.is_organization_like());
        assert!(SchemaType::LocalBusiness.is_organization_like());
        assert!(SchemaType::Other("LegalService".into()).is_organization_like());
        assert!(SchemaType::Other("MedicalOrganization".into()).is_organization_like());
        assert!(!SchemaType::Person.is_organization_like());
        assert!(!SchemaType::WebSite.is_organization_like());
    }

    #[test]
    fn product_schema_requires_offers_for_rich_result_eligibility() {
        let schema = JsonLdSchema {
            schema_type: "Product".to_string(),
            schema_types: vec!["Product".to_string()],
            content: serde_json::json!({
                "@type": "Product",
                "name": "Audit",
                "image": "https://example.com/audit.png"
            }),
            is_valid: true,
        };

        let issues = validate_schema_properties(&schema);

        assert!(issues.iter().any(|issue| {
            issue.schema_type == "Product"
                && issue.severity == SchemaIssueSeverity::Required
                && issue.issue_type == "schema_missing_offers"
        }));
    }

    #[test]
    fn article_schema_reports_recommended_properties_separately() {
        let schema = JsonLdSchema {
            schema_type: "Article".to_string(),
            schema_types: vec!["Article".to_string()],
            content: serde_json::json!({
                "@type": "Article",
                "headline": "Accessibility audit",
                "image": "https://example.com/article.png",
                "author": { "@type": "Person", "name": "Ada" },
                "datePublished": "2026-01-01"
            }),
            is_valid: true,
        };

        let issues = validate_schema_properties(&schema);

        assert!(!issues
            .iter()
            .any(|issue| issue.severity == SchemaIssueSeverity::Required));
        assert!(issues.iter().any(|issue| {
            issue.severity == SchemaIssueSeverity::Recommended
                && issue.issue_type == "schema_recommended_missing_dateModified"
        }));
    }

    #[test]
    fn empty_schema_properties_count_as_missing() {
        let schema = JsonLdSchema {
            schema_type: "BreadcrumbList".to_string(),
            schema_types: vec!["BreadcrumbList".to_string()],
            content: serde_json::json!({
                "@type": "BreadcrumbList",
                "itemListElement": []
            }),
            is_valid: true,
        };

        let issues = validate_schema_properties(&schema);

        assert_eq!(issues.len(), 1);
        assert_eq!(issues[0].issue_type, "schema_missing_itemListElement");
        assert_eq!(issues[0].severity, SchemaIssueSeverity::Required);
    }
}