auditmysite 1.7.0

Resource-efficient WCAG 2.2 AA 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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
//! Page-type fit for structured data.
//!
//! This layer deliberately starts from visible-page intent and URL structure,
//! not from the schema it is judging. Existing schema types are consulted only
//! after the page kind has been classified.

use serde::{Deserialize, Serialize};

use crate::journey::PageIntent;
use crate::seo::schema::StructuredData;
use crate::seo::schema_parity::VisibleSchemaFacts;
use crate::seo::schema_rules::ProductRuleContext;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SchemaPageKind {
    MerchantProductDetail,
    ProductCollection,
    ServiceDetail,
    SoftwareDetail,
    JobDetail,
    JobListing,
    EventDetail,
    EventListing,
    FaqPage,
    PersonProfile,
    LocationDetail,
    EditorialProductReview,
    EditorialContent,
    MarketingLandingPage,
    CorporatePage,
    HubPage,
    LeadGenerationPage,
    Indeterminate,
}

impl SchemaPageKind {
    pub fn label(self, en: bool) -> &'static str {
        match self {
            Self::MerchantProductDetail => {
                if en {
                    "Purchasable product detail"
                } else {
                    "Kaufbare Produktdetailseite"
                }
            }
            Self::ProductCollection => {
                if en {
                    "Product collection"
                } else {
                    "Produktübersicht"
                }
            }
            Self::ServiceDetail => {
                if en {
                    "Service detail"
                } else {
                    "Leistungsdetailseite"
                }
            }
            Self::SoftwareDetail => "Software / SaaS",
            Self::JobDetail => {
                if en {
                    "Job detail"
                } else {
                    "Stellendetailseite"
                }
            }
            Self::JobListing => {
                if en {
                    "Job listing"
                } else {
                    "Stellenübersicht"
                }
            }
            Self::EventDetail => {
                if en {
                    "Event detail"
                } else {
                    "Veranstaltungsdetailseite"
                }
            }
            Self::EventListing => {
                if en {
                    "Event listing"
                } else {
                    "Veranstaltungsübersicht"
                }
            }
            Self::FaqPage => "FAQ",
            Self::PersonProfile => {
                if en {
                    "Person profile"
                } else {
                    "Personenprofil"
                }
            }
            Self::LocationDetail => {
                if en {
                    "Location detail"
                } else {
                    "Standortdetailseite"
                }
            }
            Self::EditorialProductReview => {
                if en {
                    "Editorial product review"
                } else {
                    "Redaktioneller Produkttest"
                }
            }
            Self::EditorialContent => {
                if en {
                    "Editorial content"
                } else {
                    "Redaktioneller Inhalt"
                }
            }
            Self::MarketingLandingPage => "Marketing / Landing Page",
            Self::CorporatePage => {
                if en {
                    "Corporate page"
                } else {
                    "Unternehmensseite"
                }
            }
            Self::HubPage => "Hub / Portal",
            Self::LeadGenerationPage => {
                if en {
                    "Lead-generation page"
                } else {
                    "Lead-Generierungsseite"
                }
            }
            Self::Indeterminate => {
                if en {
                    "Not determined"
                } else {
                    "Nicht bestimmt"
                }
            }
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SchemaFitStatus {
    Matched,
    Plausible,
    Mismatch,
    Indeterminate,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SchemaCoverageStatus {
    Complete,
    Opportunity,
    ManualReview,
    NotApplicable,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchemaFitAssessment {
    pub page_kind: SchemaPageKind,
    pub confidence: u8,
    pub fit_status: SchemaFitStatus,
    pub coverage_status: SchemaCoverageStatus,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub expected_primary_types: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub detected_primary_types: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub evidence: Vec<String>,
}

impl SchemaFitAssessment {
    pub fn product_context(&self) -> ProductRuleContext {
        match self.page_kind {
            SchemaPageKind::MerchantProductDetail => ProductRuleContext::MerchantProductDetail,
            SchemaPageKind::EditorialContent | SchemaPageKind::EditorialProductReview
                if self
                    .detected_primary_types
                    .iter()
                    .any(|schema_type| schema_type == "Product") =>
            {
                ProductRuleContext::EditorialProduct
            }
            _ => ProductRuleContext::Indeterminate,
        }
    }

    pub fn fit_text(&self, en: bool) -> &'static str {
        match self.fit_status {
            SchemaFitStatus::Matched => {
                if en {
                    "Primary schema matches the detected page type"
                } else {
                    "Haupt-Schema passt zum erkannten Seitentyp"
                }
            }
            SchemaFitStatus::Plausible => {
                if en {
                    "Schema is plausible; page type is not specific enough for a strict requirement"
                } else {
                    "Schema ist plausibel; der Seitentyp ist für eine strikte Anforderung nicht spezifisch genug"
                }
            }
            SchemaFitStatus::Mismatch => {
                if en {
                    "Primary schema does not match the strongly indicated page type"
                } else {
                    "Haupt-Schema passt nicht zum deutlich erkannten Seitentyp"
                }
            }
            SchemaFitStatus::Indeterminate => {
                if en {
                    "Page-to-schema fit requires manual review"
                } else {
                    "Seitentyp und Schema-Abdeckung müssen manuell geprüft werden"
                }
            }
        }
    }
}

pub fn assess_schema_fit(
    url: &str,
    page_intent: PageIntent,
    structured_data: &StructuredData,
) -> SchemaFitAssessment {
    assess_schema_fit_with_facts(
        url,
        page_intent,
        structured_data,
        &VisibleSchemaFacts::default(),
    )
}

pub fn assess_schema_fit_with_facts(
    url: &str,
    page_intent: PageIntent,
    structured_data: &StructuredData,
    facts: &VisibleSchemaFacts,
) -> SchemaFitAssessment {
    let path = url_path(url);
    let product_path = contains_any(
        &path,
        &["/product/", "/produkt/", "/p/", "/dp/", "/artikel/"],
    );
    let collection_path = contains_any(
        &path,
        &[
            "/category/",
            "/kategorie/",
            "/collection",
            "/collections/",
            "/shop/",
            "/kategorien/",
        ],
    );
    let visible_heading = facts
        .h1
        .as_ref()
        .or(facts.document_title.as_ref())
        .map(|value| value.to_lowercase())
        .unwrap_or_default();
    let job_path = contains_any(&path, &["/job/", "/jobs/", "/karriere/", "/career/"]);
    let job_listing = route_ends_with(
        &path,
        &[
            "/jobs",
            "/jobs/",
            "/karriere",
            "/karriere/",
            "/career",
            "/career/",
        ],
    );
    let event_path = contains_any(
        &path,
        &[
            "/event/",
            "/events/",
            "/veranstaltung/",
            "/veranstaltungen/",
        ],
    );
    let event_listing = route_ends_with(
        &path,
        &[
            "/events",
            "/events/",
            "/veranstaltungen",
            "/veranstaltungen/",
        ],
    );
    let service_path = contains_any(
        &path,
        &["/service/", "/services/", "/leistung/", "/leistungen/"],
    );
    let software_path = contains_any(&path, &["/software/", "/app/", "/saas/", "/plattform/"])
        || contains_any(
            &visible_heading,
            &["software", " saas", "web-app", "plattform"],
        );
    let faq_page = facts.faq_questions.len() >= 2
        || route_ends_with(&path, &["/faq", "/faq/", "/fragen", "/fragen/"]);
    let person_path = contains_any(&path, &["/person/", "/personen/", "/team/"])
        && !route_ends_with(&path, &["/team", "/team/", "/personen", "/personen/"]);
    let location_path = contains_any(&path, &["/standort/", "/standorte/", "/location/"])
        && !route_ends_with(&path, &["/standorte", "/standorte/"]);
    let editorial_review = page_intent == PageIntent::Editorial
        && (contains_any(&path, &["/test/", "/review/", "/vergleich/"])
            || contains_any(&visible_heading, &["test", "review", "vergleich"]));

    let (page_kind, confidence, expected, mut evidence): (
        SchemaPageKind,
        u8,
        &[&str],
        Vec<String>,
    ) = if faq_page {
        (
            SchemaPageKind::FaqPage,
            90,
            &["FAQPage"],
            vec!["Visible FAQ questions or a dedicated FAQ route were detected".to_string()],
        )
    } else if job_path && !job_listing {
        (
            SchemaPageKind::JobDetail,
            90,
            &["JobPosting"],
            vec!["URL path indicates a single job posting".to_string()],
        )
    } else if job_listing {
        (
            SchemaPageKind::JobListing,
            90,
            &["CollectionPage", "ItemList"],
            vec!["URL path indicates a job listing".to_string()],
        )
    } else if event_path && !event_listing {
        (
            SchemaPageKind::EventDetail,
            90,
            &["Event"],
            vec!["URL path indicates a single event".to_string()],
        )
    } else if event_listing {
        (
            SchemaPageKind::EventListing,
            90,
            &["CollectionPage", "ItemList"],
            vec!["URL path indicates an event listing".to_string()],
        )
    } else if editorial_review {
        (
            SchemaPageKind::EditorialProductReview,
            85,
            &["Article", "BlogPosting", "Product"],
            vec![
                "Visible editorial intent and review-specific route or heading were detected"
                    .to_string(),
            ],
        )
    } else if person_path {
        (
            SchemaPageKind::PersonProfile,
            85,
            &["ProfilePage", "Person"],
            vec!["URL path indicates a single person profile".to_string()],
        )
    } else if location_path {
        (
            SchemaPageKind::LocationDetail,
            85,
            &["LocalBusiness", "Organization"],
            vec!["URL path indicates a single business location".to_string()],
        )
    } else if software_path && matches!(page_intent, PageIntent::Marketing | PageIntent::LeadGen) {
        (
            SchemaPageKind::SoftwareDetail,
            80,
            &["SoftwareApplication", "WebApplication"],
            vec![
                "Visible page intent and route or heading indicate a software offering".to_string(),
            ],
        )
    } else if service_path && matches!(page_intent, PageIntent::Marketing | PageIntent::LeadGen) {
        (
            SchemaPageKind::ServiceDetail,
            80,
            &["Service", "ProfessionalService"],
            vec!["Visible page intent and URL path indicate a service detail page".to_string()],
        )
    } else {
        match page_intent {
            PageIntent::Shop if product_path => (
                SchemaPageKind::MerchantProductDetail,
                90,
                &["Product"],
                vec![
                    "Visible page intent indicates commerce".to_string(),
                    "URL path indicates a product-detail route".to_string(),
                ],
            ),
            PageIntent::Shop if collection_path => (
                SchemaPageKind::ProductCollection,
                90,
                &["CollectionPage", "ItemList"],
                vec![
                    "Visible page intent indicates commerce".to_string(),
                    "URL path indicates a product collection route".to_string(),
                ],
            ),
            PageIntent::Shop => (
                SchemaPageKind::Indeterminate,
                65,
                &[],
                vec![
                    "Visible page intent indicates commerce, but no specific route was established"
                        .to_string(),
                ],
            ),
            PageIntent::Editorial => (
                SchemaPageKind::EditorialContent,
                85,
                &["Article", "BlogPosting", "NewsArticle"],
                vec!["Visible page structure indicates editorial content".to_string()],
            ),
            PageIntent::Marketing => (
                SchemaPageKind::MarketingLandingPage,
                65,
                &["WebPage"],
                vec!["Visible page structure indicates a marketing landing page".to_string()],
            ),
            PageIntent::Corporate => (
                SchemaPageKind::CorporatePage,
                70,
                &["WebPage", "Organization", "LocalBusiness"],
                vec!["Visible page structure indicates corporate information".to_string()],
            ),
            PageIntent::Hub => (
                SchemaPageKind::HubPage,
                70,
                &["WebPage", "CollectionPage", "ItemList"],
                vec!["Visible page structure indicates a hub or portal".to_string()],
            ),
            PageIntent::LeadGen => (
                SchemaPageKind::LeadGenerationPage,
                65,
                &["WebPage", "Service", "ProfessionalService"],
                vec!["Visible page structure indicates lead generation".to_string()],
            ),
            PageIntent::Unknown => (
                SchemaPageKind::Indeterminate,
                0,
                &[],
                vec!["Visible page type could not be determined".to_string()],
            ),
        }
    };

    let expected_primary_types = expected.iter().map(|value| (*value).to_string()).collect();
    let detected_primary_types = structured_data
        .types
        .iter()
        .map(|schema_type| schema_type.as_str().to_string())
        .filter(|schema_type| !is_supporting_type(schema_type))
        .collect::<Vec<_>>();
    let has_expected = detected_primary_types
        .iter()
        .any(|detected| expected.contains(&detected.as_str()));

    let (fit_status, coverage_status) = if expected.is_empty() {
        (
            SchemaFitStatus::Indeterminate,
            SchemaCoverageStatus::ManualReview,
        )
    } else if has_expected {
        if confidence >= 80 {
            (SchemaFitStatus::Matched, SchemaCoverageStatus::Complete)
        } else {
            (
                SchemaFitStatus::Plausible,
                SchemaCoverageStatus::ManualReview,
            )
        }
    } else if confidence >= 80 {
        if matches!(
            page_kind,
            SchemaPageKind::ProductCollection
                | SchemaPageKind::JobListing
                | SchemaPageKind::EventListing
        ) && detected_primary_types
            .iter()
            .any(|schema_type| matches!(schema_type.as_str(), "Product" | "JobPosting" | "Event"))
        {
            let single_item_types = detected_primary_types
                .iter()
                .filter(|schema_type| {
                    matches!(schema_type.as_str(), "Product" | "JobPosting" | "Event")
                })
                .cloned()
                .collect::<Vec<_>>()
                .join(", ");
            evidence.push(format!(
                "Single-item schema ({single_item_types}) was found on a collection route"
            ));
            (SchemaFitStatus::Mismatch, SchemaCoverageStatus::Opportunity)
        } else {
            (
                SchemaFitStatus::Indeterminate,
                SchemaCoverageStatus::Opportunity,
            )
        }
    } else {
        (
            SchemaFitStatus::Indeterminate,
            SchemaCoverageStatus::ManualReview,
        )
    };

    SchemaFitAssessment {
        page_kind,
        confidence,
        fit_status,
        coverage_status,
        expected_primary_types,
        detected_primary_types,
        evidence,
    }
}

fn url_path(url: &str) -> String {
    let after_scheme = url.split_once("://").map(|(_, rest)| rest).unwrap_or(url);
    after_scheme
        .find('/')
        .map(|index| after_scheme[index..].to_lowercase())
        .unwrap_or_default()
}

fn contains_any(value: &str, needles: &[&str]) -> bool {
    needles.iter().any(|needle| value.contains(needle))
}

fn route_ends_with(path: &str, endings: &[&str]) -> bool {
    let path = path.split(['?', '#']).next().unwrap_or(path);
    endings.iter().any(|ending| path.ends_with(ending))
}

fn is_supporting_type(schema_type: &str) -> bool {
    matches!(
        schema_type,
        "WebSite" | "BreadcrumbList" | "Person" | "Offer" | "Review" | "AggregateRating"
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::seo::schema::analyze_structured_data_payloads;

    fn structured_data(value: serde_json::Value) -> StructuredData {
        analyze_structured_data_payloads(&[value.to_string()], false, false)
    }

    #[test]
    fn merchant_product_route_expects_product_and_enables_merchant_context() {
        let data = structured_data(serde_json::json!({
            "@context": "https://schema.org",
            "@type": "Product",
            "name": "Product",
            "offers": {"@type": "Offer", "price": "10", "priceCurrency": "EUR"}
        }));
        let fit = assess_schema_fit(
            "https://example.com/produkt/widget",
            PageIntent::Shop,
            &data,
        );

        assert_eq!(fit.page_kind, SchemaPageKind::MerchantProductDetail);
        assert_eq!(fit.fit_status, SchemaFitStatus::Matched);
        assert_eq!(
            fit.product_context(),
            ProductRuleContext::MerchantProductDetail
        );
    }

    #[test]
    fn generic_landing_page_never_requires_product() {
        let data = StructuredData::default();
        let fit = assess_schema_fit("https://example.com/angebot", PageIntent::Marketing, &data);

        assert_eq!(fit.page_kind, SchemaPageKind::MarketingLandingPage);
        assert_eq!(fit.coverage_status, SchemaCoverageStatus::ManualReview);
        assert_eq!(fit.expected_primary_types, vec!["WebPage"]);
    }

    #[test]
    fn product_markup_on_collection_route_is_a_fit_mismatch() {
        let data = structured_data(serde_json::json!({
            "@context": "https://schema.org",
            "@type": "Product",
            "name": "Teaser",
            "offers": {"@type": "Offer", "price": "10", "priceCurrency": "EUR"}
        }));
        let fit = assess_schema_fit(
            "https://example.com/kategorie/widgets",
            PageIntent::Shop,
            &data,
        );

        assert_eq!(fit.page_kind, SchemaPageKind::ProductCollection);
        assert_eq!(fit.fit_status, SchemaFitStatus::Mismatch);
    }

    #[test]
    fn single_item_schema_is_rejected_on_job_and_event_listings() {
        for (url, schema_type) in [
            ("https://example.com/jobs/", "JobPosting"),
            ("https://example.com/events/", "Event"),
        ] {
            let data = structured_data(serde_json::json!({
                "@context": "https://schema.org",
                "@type": schema_type,
                "name": "Teaser"
            }));
            let fit = assess_schema_fit(url, PageIntent::Corporate, &data);
            assert_eq!(fit.fit_status, SchemaFitStatus::Mismatch, "{url}");
        }
    }

    #[test]
    fn specific_routes_classify_supported_page_subtypes() {
        let empty = StructuredData::default();
        let cases = [
            (
                "https://example.com/leistungen/audit",
                PageIntent::Marketing,
                SchemaPageKind::ServiceDetail,
            ),
            (
                "https://example.com/software/platform",
                PageIntent::Marketing,
                SchemaPageKind::SoftwareDetail,
            ),
            (
                "https://example.com/jobs/engineer",
                PageIntent::Corporate,
                SchemaPageKind::JobDetail,
            ),
            (
                "https://example.com/jobs/",
                PageIntent::Corporate,
                SchemaPageKind::JobListing,
            ),
            (
                "https://example.com/events/conference",
                PageIntent::Marketing,
                SchemaPageKind::EventDetail,
            ),
            (
                "https://example.com/events/",
                PageIntent::Marketing,
                SchemaPageKind::EventListing,
            ),
            (
                "https://example.com/team/ada",
                PageIntent::Corporate,
                SchemaPageKind::PersonProfile,
            ),
            (
                "https://example.com/standorte/berlin",
                PageIntent::Corporate,
                SchemaPageKind::LocationDetail,
            ),
            (
                "https://example.com/test/widget",
                PageIntent::Editorial,
                SchemaPageKind::EditorialProductReview,
            ),
        ];

        for (url, intent, expected) in cases {
            assert_eq!(
                assess_schema_fit(url, intent, &empty).page_kind,
                expected,
                "{url}"
            );
        }

        let faq_facts = VisibleSchemaFacts {
            faq_questions: vec!["One?".into(), "Two?".into()],
            ..Default::default()
        };
        assert_eq!(
            assess_schema_fit_with_facts(
                "https://example.com/help",
                PageIntent::Corporate,
                &empty,
                &faq_facts,
            )
            .page_kind,
            SchemaPageKind::FaqPage
        );
    }

    #[test]
    fn english_fit_labels_have_no_german_characters() {
        for page_kind in [
            SchemaPageKind::MerchantProductDetail,
            SchemaPageKind::ProductCollection,
            SchemaPageKind::ServiceDetail,
            SchemaPageKind::SoftwareDetail,
            SchemaPageKind::JobDetail,
            SchemaPageKind::JobListing,
            SchemaPageKind::EventDetail,
            SchemaPageKind::EventListing,
            SchemaPageKind::FaqPage,
            SchemaPageKind::PersonProfile,
            SchemaPageKind::LocationDetail,
            SchemaPageKind::EditorialProductReview,
            SchemaPageKind::EditorialContent,
            SchemaPageKind::MarketingLandingPage,
            SchemaPageKind::CorporatePage,
            SchemaPageKind::HubPage,
            SchemaPageKind::LeadGenerationPage,
            SchemaPageKind::Indeterminate,
        ] {
            assert!(!page_kind
                .label(true)
                .contains(['ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß']));
        }
    }
}